这三种从列表中删除元素的方法有什么区别吗?
>>> 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]
当前回答
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
当然,设置空值与删除空值是不同的,但是如果您想了解更多关于删除的知识,考虑一下这个操作的性能对我来说也很有趣。
其他回答
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
当然,设置空值与删除空值是不同的,但是如果您想了解更多关于删除的知识,考虑一下这个操作的性能对我来说也很有趣。
而pop和delete都采用索引来删除上面注释中所述的元素。一个关键的区别是它们的时间复杂度。没有索引的pop()的时间复杂度是O(1),但删除最后一个元素的情况不同。
如果您的用例总是删除最后一个元素,那么使用pop()而不是delete()总是更好的。有关时间复杂性的更多解释,请参阅https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt
列表上的删除操作给定一个要删除的值。它搜索列表以查找具有该值的项,并删除找到的第一个匹配项。如果没有匹配项,则是一个错误,引发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。
pop
获取index(如果给定,则获取last),删除该索引处的值,并返回值
删除
获取值,删除第一次出现的内容,并不返回任何内容
删除
获取索引,删除该索引处的值,并不返回任何值
不同数据结构上的任何操作/函数都是为特定操作定义的。这里在你的情况下,即删除一个元素,删除,弹出和删除。(如果你考虑集合,添加另一个操作-丢弃) 另一种令人困惑的情况是加法。插入/追加。 为了演示,让我们实现deque。Deque是一种混合线性数据结构,可以在两端添加/删除元素。(前后端)
class Deque(object):
def __init__(self):
self.items=[]
def addFront(self,item):
return self.items.insert(0,item)
def addRear(self,item):
return self.items.append(item)
def deleteFront(self):
return self.items.pop(0)
def deleteRear(self):
return self.items.pop()
def returnAll(self):
return self.items[:]
在这里,查看操作:
def deleteFront(self):
return self.items.pop(0)
def deleteRear(self):
return self.items.pop()
操作必须返回一些东西。pop -带索引和不带索引。 如果我不想返回值: 德尔self.items [0]
不按值删除:
删除: list_ez = [1, 2, 3, 4, 5, 6, 7, 8) 对于list_ez中的I: 如果我% 2 = = 0: list_ez.remove(我) 打印list_ez
返回(1、3、5、7)
让我们考虑集合的情况。
set_ez=set_ez=set(range(10))
set_ez.remove(11)
# Gives Key Value Error.
##KeyError: 11
set_ez.discard(11)
# Does Not return any errors.