python删除元素&插入元素

删除

remove 是删除首个符合条件的元素,并不是删除特定的索引。

a = [0, 2, 2, 3]
a.remove(2)
print(a) # [0, 2, 3],删除指定元素

del 是根据索引(元素所在位置)来删除的 。

del删除整个列表。

# del[1:3]删除指定区间
L2 = [1,2,3,4,5]
del L2[1:3]
print(L2) # [1, 4, 5],删除1,2下标
del L2[0]
print(L2) # [4, 5],删除0下标
del L2
print(L2) # NameError: name 'L2' is not defined

 pop返回的是弹出的那个数值,是下标定位。

b = [4, 3, 5]
print(b.pop(1)) # 3
print(b) # [4, 5]

转:http://novell.me/master-diary/2014-06-05/difference-between-del-remove-and-pop-on.html 

插入

https://blog.csdn.net/hanshanyeyu/article/details/78839266 

猜你喜欢

转载自blog.csdn.net/weixin_31866177/article/details/83178984