这是一个详细的答案。
del 可用于任何类对象,而 pop 和 remove 并绑定到特定类。
给del
这里有一些例子
>>> a = 5
>>> b = "this is string"
>>> c = 1.432
>>> d = myClass()
>>> del c
>>> del a, b, d # we can use comma separated objects
我们可以在用户创建的类中覆盖__del__ 方法。
列表的具体用途
>>> a = [1, 4, 2, 4, 12, 3, 0]
>>> del a[4]
>>> a
[1, 4, 2, 4, 3, 0]
>>> del a[1: 3] # we can also use slicing for deleting range of indices
>>> a
[1, 4, 3, 0]
给pop
pop 将索引作为参数并删除该索引处的元素
与del 不同,pop 在列表对象上调用时返回该索引处的值
>>> a = [1, 5, 3, 4, 7, 8]
>>> a.pop(3) # Will return the value at index 3
4
>>> a
[1, 5, 3, 7, 8]
给remove
remove 获取参数值并从列表中删除该值。
如果存在多个值,将删除第一个出现的值
Note: 如果该值不存在,将抛出 ValueError
>>> a = [1, 5, 3, 4, 2, 7, 5]
>>> a.remove(5) # removes first occurence of 5
>>> a
[1, 3, 4, 2, 7, 5]
>>> a.remove(5)
>>> a
[1, 3, 4, 2, 7]
希望这个答案有帮助。