这三种从列表中删除元素的方法有什么区别吗?

>>> a = [1, 2, 3]
>>> a.remove(2)
>>> a
[1, 3]

>>> a = [1, 2, 3]
>>> del a[1]
>>> a
[1, 3]

>>> a = [1, 2, 3]
>>> a.pop(1)
2
>>> a
[1, 3]

当前回答

Remove (), del和pop()都很慢…“None”呢?

在这么多的回复中,我没有看到任何人谈论性能。所以我有一个性能建议:

Remove (), del和pop()在删除后将所有剩余值移到左边…

1, 2, 3, 4, 5, 6
remove(3)
1, 2, <- 4, 5, 6

...使处理变慢!

将期望的值更改为null,以便进一步处理删除,这只能为您的程序增加很多速度,特别是在处理大量数据时:

my_array[2] = None

当然,设置空值与删除空值是不同的,但是如果您想了解更多关于删除的知识,考虑一下这个操作的性能对我来说也很有趣。

其他回答

使用del按索引删除元素,使用pop()按索引删除元素(如果需要返回值),使用remove()按值删除元素。最后一个需要搜索列表,如果列表中没有这样的值,则引发ValueError。

当从一个包含n个元素的列表中删除索引i时,这些方法的计算复杂度为

del     O(n - i)
pop     O(n - i)
remove  O(n)

从列表中删除元素的三种不同方法的效果:

Remove删除第一个匹配的值,而不是特定的索引:

>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]

Del删除特定索引处的项:

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]

pop删除特定索引处的项并返回它。

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]

它们的错误模式也不同:

>>> 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

列表上的删除操作给定一个要删除的值。它搜索列表以查找具有该值的项,并删除找到的第一个匹配项。如果没有匹配项,则是一个错误,引发ValueError。

>>> x = [1, 0, 0, 0, 3, 4, 5]
>>> x.remove(4)
>>> x
[1, 0, 0, 0, 3, 5]
>>> del x[7]
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    del x[7]
IndexError: list assignment index out of range

del语句可用于删除整个列表。如果你有一个特定的列表项作为del的参数(例如listname[7]专门引用列表中的第8项),它会删除该项。甚至可以从列表中删除“slice”。如果索引超出范围,则会引发IndexError。

>>> x = [1, 2, 3, 4]
>>> del x[3]
>>> x
[1, 2, 3]
>>> del x[4]
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    del x[4]
IndexError: list assignment index out of range

The usual use of pop is to delete the last item from a list as you use the list as a stack. Unlike del, pop returns the value that it popped off the list. You can optionally give an index value to pop and pop from other than the end of the list (e.g listname.pop(0) will delete the first item from the list and return that first item as its result). You can use this to make the list behave like a queue, but there are library routines available that can provide queue operations with better performance than pop(0) does. It is an error if there index out of range, raises a IndexError.

>>> x = [1, 2, 3] 
>>> x.pop(2) 
3 
>>> x 
[1, 2]
>>> x.pop(4)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    x.pop(4)
IndexError: pop index out of range

有关更多细节,请参阅collections.deque。

del、pop和remove在执行速度上的差异:

当移除任何中间项目时:

import timeit
print(timeit.timeit("a=[1,2,3,4,5]\ndel a[3]",number=100000))
print(timeit.timeit("a=[1,2,3,4,5]\na.pop(3)",number=100000))
print(timeit.timeit("a=[1,2,3,4,5]\na.remove(3)",number=100000))

Del vs pop vs remove:

0.019387657986953855
0.02506213402375579
0.033232167130336165

Del()似乎比其他两个快得多,而remove()是最慢的。

同时删除最后一项:

print(timeit.timeit("a=[1,2,3,4,5]\ndel a[-1]",number=100000))
print(timeit.timeit("a=[1,2,3,4,5]\na.pop()",number=100000))
print(timeit.timeit("a=[1,2,3,4,5]\na.remove(5)",number=100000))

Del vs pop vs remove:

0.01974551402963698
0.020333584863692522
0.03434014297090471

Del()和pop()删除最后一项所需的时间相似。

因为没有人提到它,请注意del(不像pop)允许删除一系列索引,因为列表切片:

>>> lst = [3, 2, 2, 1]
>>> del lst[1:]
>>> lst
[3]

这也允许避免IndexError如果索引不在列表中:

>>> lst = [3, 2, 2, 1]
>>> del lst[10:]
>>> lst
[3, 2, 2, 1]