python列表(list)
列表的增删改查
列表的查找
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/9 12:53
print('------列表的查找--------------')
#创建列表的方式:
#可以存储相同数据,不同的数字类型 list是有序的
#正向: 0 1 2 3
lst1 = ['hello','world',98,'hello']
# 反向 -4 -3 -2 -1
print(lst1)
print(lst1[1:3])
print(lst1[:3])
print(lst1[0])
print(lst1[-4])#从后往前数,依次是-1,-2,-3,-4这样数
print(lst1.index('hello'))#如果列表中有相同的元素,则只返回第一个元素的索引。
print(lst1.index('hello',1,4))#不包括4.在指定范围内查找索引。
print(lst1[1:4:2])#从第二个开始,步长为2,
print('--------step步长为负数的情况,相当于逆向输出列表-----------------')
print(lst1[::-1])
#start=7,stop 省略,step=1
print(lst1[3::-1])
for item in lst1:
print(item)
运行结果;
------列表的查找--------------
['hello', 'world', 98, 'hello']
['world', 98]
['hello', 'world', 98]
hello
hello
0
3
['world', 'hello']
--------step步长为负数的情况,相当于逆向输出列表-----------------
['hello', 98, 'world', 'hello']
['hello', 98, 'world', 'hello']
hello
world
98
hello
列表的增删改
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/9 13:45
print('---------列表元素的增删改查---------')
lst1=[10,20,30]
print(lst1,id(lst1))
#list.append只能添加一个元素
lst1.append(100)
print(lst1,id(lst1))
#lst2作为一个元素直接添加在list1的后面
# append只是添加整个列表
lst2=['hello','world']
lst1.append(lst2)
print(lst1)
#list.extend 是添加列表里面的所有元素
# 相当于连接两个列表
lst3=['hello','world']
lst1.extend(lst3)
print(lst1)
#list.insert 是在任意位置添一个元素
lst1.insert(1,90)
print(lst1)
#在任意位置上添加N多个元素
#切片操作,将后面的所有元素切掉
lst4=[True,False,'hello']
lst1[5:]=lst4
print(lst1)
#list.remove是从列表中移去一个元素,如果存在相同元素,只移去第一个元素
lst1.remove(100)
print(lst1)
#list.pop根据索引移除元素
lst1.pop(5)
print(lst1)
#如果不写参数,则直接删除最后一个元素
lst1.pop()
print(lst1)
#切片操作 实际上是产生一个新的列表
new_lst1=lst1[1:4]
print(new_lst1)
#不产生一个新列表
lst1[1:3]=[]
print(lst1)
#list.clear 清除列表中的所有元素
lst1.clear()
print(lst1)
#list.del 是删除列表,在输出这个列表会报错
print('----列表的改---------')
lst1=[10,20,30,40]
print(lst1)
lst1[2]=100
print(lst1)
lst1[-4]=200
print(lst1)
lst1[1:3]=[300,400,500,600]
print(lst1)
运行结果:
---------列表元素的增删改查---------
[10, 20, 30] 1673301371904
[10, 20, 30, 100] 1673301371904
[10, 20, 30, 100, ['hello', 'world']]
[10, 20, 30, 100, ['hello', 'world'], 'hello', 'world']
[10, 90, 20, 30, 100, ['hello', 'world'], 'hello', 'world']
[10, 90, 20, 30, 100, True, False, 'hello']
[10, 90, 20, 30, True, False, 'hello']
[10, 90, 20, 30, True, 'hello']
[10, 90, 20, 30, True]
[90, 20, 30]
[10, 30, True]
[]
----列表的改---------
[10, 20, 30, 40]
[10, 20, 100, 40]
[200, 20, 100, 40]
[200, 300, 400, 500, 600, 40]
列表的排序
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/9 14:22
print('-------列表的排序---------')
lst1=[20,40,32,92,54]
print(lst1,id(lst1))
#list.sort()是升序
#id一样则说明没有创建新的列表,只是在原有的列表进行排序
lst1.sort()
print(lst1,id(lst1))
#对列表进行降序(没有创建新的列表)
lst1.sort(reverse=True)
print(lst1)
#对列表进行升序 不指定reverse 则默认reverse为False
lst1.sort(reverse=False)
print(lst1)
#对列表进行升序排序并创建一个新的列表
new_list=sorted(lst1)
print(lst1)
print(lst1)
#对列表进行降序排序并创建一个新的列表
new_list1=sorted(lst1,reverse=True)
print(lst1)
print(new_list1)
运行结果:
-------列表的排序---------
[20, 40, 32, 92, 54] 1982050839552
[20, 32, 40, 54, 92] 1982050839552
[92, 54, 40, 32, 20]
[20, 32, 40, 54, 92]
[20, 32, 40, 54, 92]
[20, 32, 40, 54, 92]
[20, 32, 40, 54, 92]
[92, 54, 40, 32, 20]
列表的生成式
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/9 14:53
print('-----------列表生成式-----------')
lst1=[i*i for i in range(1,10)]
print(lst1)
lst1=[j for j in range(2,11,2)]
print(lst1)
lst1=[i*2 for i in range(1,6)]
print(lst1)
运行结果:
-----------列表生成式-----------
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]