列表的增删查改 续

pop按索引去删

l1 = ['das', 'dfsdf',[1,23,],'asda']
name = l1.pop(1)#返回被删除的值,按索引去删除
print(name,l1)
View Code

若不写删除元素索引,默认删除最后一个

remove 按元素值去删

l1 = ['das', 'dfsdf',[1,23,],'asda']
l1.remove('asda')
print(l1)
View Code

 clear 清空  del 删除列表

l1 = ['das', 'dfsdf',[1,23,],'asda']
l1.clear()
print(l1)
del l1
print(l1)
View Code

 del 的用法

l1 = ['das', 'dfsdf',[1,23,],'asda']
 
del l1[1:2]
print(l1)
View Code

 

l1 = ['das', 'dfsdf',[1,23,],'asda']
l1[1] = 'shdiau'
l1[2] = [2,4,5]
print(l1)
View Code、

 查

l1 = ['das', 'dfsdf',[1,23,],'asda']
for i in l1:#查列表中的每一元素
    print(i)
View Code

l1 = ['das', 'dfsdf',[1,23,],'asda']
for i in l1:#查列表中的每一元素
    print(i)
print(l1[0:2])#切片查,查列表前两个元素
View Code

posted @ 2019-07-19 22:45  套你大象  阅读(132)  评论(0编辑  收藏  举报