这三种从列表中删除元素的方法有什么区别吗?
>>> 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
删除 first 匹配 value,而不是特定索引:
>>> 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
使用 del
按索引删除元素,如果您需要返回值,使用 pop()
按索引删除它,使用 remove()
按值删除元素。最后一个需要搜索列表,如果列表中没有出现这样的值,则引发 ValueError
。
从 n
元素列表中删除索引 i
时,这些方法的计算复杂度为
del O(n - i)
pop O(n - i)
remove O(n)
del
稍微快一点,但出于不同的原因:在 C 中实现的类型上查找 __delitem__
是按索引而不是按名称发生的,而 pop
需要在整个之后查找描述符协议。函数本身的执行应该花费相同的时间。两者都返回一个指针——一个指向被移除的对象,另一个指向 None
。
由于没有其他人提到它,请注意 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]
其他人已经回答得很好。这是我的最后一个:)
https://i.stack.imgur.com/9Yzgv.png
显然,pop
是唯一返回值的,而 remove
是唯一搜索对象的,而 del
将自身限制为简单的删除。
这里有很多很好的解释,但我会尽力简化更多。
在所有这些方法中,remove
& pop
是 postfix,而 delete 是 prefix。
remove()
: 用于删除第一次出现的元素。
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() => from end of list
>>> a.pop()
>>> a
[0, 3, 2, 1, 4, 6, 5]
如果指定了索引:pop(index) => of 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]: del 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
希望这能澄清困惑。
postfix
和 prefix
有什么区别?
流行音乐
获取索引(给定时,否则最后获取),删除该索引处的值,并返回值
消除
取值,删除第一次出现,不返回任何内容
删除
获取索引,删除该索引处的值,并且不返回任何内容
不同数据结构上的任何操作/功能都是为特定操作定义的。在您的情况下,即删除元素、删除、弹出和删除。 (如果您考虑集合,请添加另一个操作 - 丢弃)其他令人困惑的情况是在添加时。插入/追加。为了演示,让我们实现双端队列。 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 - 有和没有索引。如果我不想返回值:del self.items[0]
按值而不是索引删除:
删除:list_ez=[1,2,3,4,5,6,7,8] for i in list_ez: if i%2==0: list_ez.remove(i) print 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.
这是一个详细的答案。
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]
希望这个答案是有帮助的。
列表上的 remove 操作被赋予一个要删除的值。它搜索列表以查找具有该值的项目并删除它找到的第一个匹配项目。如果没有匹配项,则为错误,引发 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 项),它只会删除该项。甚至可以从列表中删除“切片”。如果索引超出范围,则会出现错误,引发 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
pop 通常用于从列表中删除最后一项,因为您将列表用作堆栈。与 del 不同,pop 返回它从列表中弹出的值。您可以选择为从列表末尾以外的位置弹出和弹出的索引值(例如,listname.pop(0) 将删除列表中的第一项并返回该第一项作为其结果)。您可以使用它来使列表表现得像一个队列,但是有一些可用的库例程可以提供比 pop(0) 更好的性能的队列操作。如果索引超出范围,则会出现错误,引发 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 和 delete 都使用索引来删除上面评论中所述的元素。一个关键的区别是它们的时间复杂度。没有索引的 pop() 的时间复杂度为 O(1),但删除最后一个元素的情况不同。
如果您的用例总是要删除最后一个元素,则最好使用 pop() 而不是 delete()。有关时间复杂度的更多说明,您可以参考 https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt
delete
这样的方法。不同之处在于 pop
返回值,而 del
适用于切片。在 pop
有效的情况下,del
具有完全相同的计算复杂度(并且在常数项上稍快一些)。
Remove 基本上适用于 value 。删除和弹出索引上的工作
删除基本上删除了第一个匹配值。 Delete 从特定索引中删除项目 Pop 基本上采用索引并返回该索引处的值。下次打印列表时,该值不会出现。
https://i.stack.imgur.com/ExsEy.png
您也可以使用 remove 按索引删除值。
n = [1, 3, 5]
n.remove(n[1])
然后 n 将引用 [1, 5]
n = [5, 3, 5]
,然后尝试 n.remove(n[2])
。
n.remove(n[2])
删除 n[0]
,而不是 n[2]
。所以这不仅仅是无缘无故的线性时间(当N = 3时可能没什么大不了的),它也是错误的(不管N是什么都是大问题)
不定期副业成功案例分享
del
是类似于print
的 python 2 语法保留,但它仍然适用于 python 3。del
不是语法保留,不是。语法不变,就像return
或if
或while
。