Yes, remove
removes the first matching value , not a specific index:
(是的, remove
删除第一个匹配值 ,而不是特定索引:)
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
del
removes the item at a specific index:
(del
删除指定索引处的项目:)
>>> a = [3, 2, 2, 1]
>>> del a[1]
>>> a
[3, 2, 1]
and pop
removes the item at a specific index and returns it.
(然后pop
会删除指定索引处的项目并返回它。)
>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
Their error modes are different too:
(它们的错误模式也不同:)
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…