这三种从列表中删除元素的方法有什么区别吗?
>>> 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]
这三种从列表中删除元素的方法有什么区别吗?
>>> 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]
当前回答
pop
获取index(如果给定,则获取last),删除该索引处的值,并返回值
删除
获取值,删除第一次出现的内容,并不返回任何内容
删除
获取索引,删除该索引处的值,并不返回任何值
其他回答
从列表中删除元素的三种不同方法的效果:
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()按值删除元素。最后一个需要搜索列表,如果列表中没有这样的值,则引发ValueError。
当从一个包含n个元素的列表中删除索引i时,这些方法的计算复杂度为
del O(n - i)
pop O(n - i)
remove O(n)
这里有很多很好的解释,但我会尽量简化。
其中,remove和pop是后缀,delete是前缀。
remove():用于删除第一次出现的元素。 删除(n) =>第一次出现在列表中的n。
>>> a = [0, 2, 3, 2, 1, 4, 6, 5, 7]
>>> a.remove(2) # where i = 2
>>> a
[0, 3, 2, 1, 4, 6, 5, 7]
pop():用于删除元素…
如果没有指定索引: Pop() =>从列表结束
>>> a.pop()
>>> a
[0, 3, 2, 1, 4, 6, 5]
如果指定了索引: Pop (index) = index的>
>>> a.pop(2)
>>> a
[0, 3, 1, 4, 6, 5]
警告:前方有危险
del():这是一个前缀方法。
注意相同方法的两种不同语法:带[]和不带。它有能力:
删除索引 Del a[index] =>用于删除索引和它的关联值,就像pop一样。
>>> del a[1]
>>> a
[0, 1, 4, 6, 5]
删除范围[index_1:index_N]中的值: 删除a[0:3] =>多个范围内的值。
>>> del a[0:3]
>>> a
[6, 5]
最后但并非最不重要的,删除整个列表在一个镜头。 如上所述,Del (a) =>。
>>> del (a)
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
希望这能澄清困惑。
Remove主要作用于值。 删除和弹出工作在索引上
Remove基本上删除了第一个匹配的值。 Delete从指定索引中删除项 Pop基本上接受一个索引并返回该索引的值。下次打印列表时,该值不会出现。