列表的增删查改 续
删
pop按索引去删
l1 = ['das', 'dfsdf',[1,23,],'asda'] name = l1.pop(1)#返回被删除的值,按索引去删除 print(name,l1)
若不写删除元素索引,默认删除最后一个
remove 按元素值去删
l1 = ['das', 'dfsdf',[1,23,],'asda'] l1.remove('asda') print(l1)
clear 清空 del 删除列表
l1 = ['das', 'dfsdf',[1,23,],'asda'] l1.clear() print(l1) del l1 print(l1)
del 的用法
l1 = ['das', 'dfsdf',[1,23,],'asda'] del l1[1:2] print(l1)
改
l1 = ['das', 'dfsdf',[1,23,],'asda'] l1[1] = 'shdiau' l1[2] = [2,4,5] print(l1)
查
l1 = ['das', 'dfsdf',[1,23,],'asda'] for i in l1:#查列表中的每一元素 print(i)
l1 = ['das', 'dfsdf',[1,23,],'asda'] for i in l1:#查列表中的每一元素 print(i) print(l1[0:2])#切片查,查列表前两个元素