删除,删除和弹出列表之间的区别

本文详细比较了Python中用于从列表删除元素的三种方法:remove、del和pop。remove按值删除第一个匹配项,del按索引删除,pop不仅按索引删除还返回被删除的值。每种方法的错误模式和计算复杂度也有所不同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Difference between del, remove and pop on lists

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

Is there any difference between the above three methods to remove an element from a list? 以上三种从列表中删除元素的方法之间有什么区别吗?


#1楼

参考:https://stackoom.com/question/mL0O/删除-删除和弹出列表之间的区别


#2楼

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

#3楼

Use del to remove an element by index, pop() to remove it by index if you need the returned value, and remove() to delete an element by value. 使用del可以按索引删除元素,使用pop()可以按索引删除元素,如果需要返回值,则可以使用remove()可以按值删除元素。 The latter requires searching the list, and raises ValueError if no such value occurs in the list. 后者需要搜索列表,并且如果列表中没有这样的值,则会引发ValueError

When deleting index i from a list of n elements, the computational complexities of these methods are n元素的列表中删除索引i ,这些方法的计算复杂度为

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

#4楼

You can also use remove to remove a value by index as well. 您也可以使用remove来删除索引值。

n = [1, 3, 5]

n.remove(n[1])

n would then refer to [1, 5] n然后将指代[1,5]


#5楼

pop - Takes Index and returns Value pop-获取索引并返回值

remove - Takes value, removes first occurrence, and returns nothing remove-取值,删除第一个匹配项,不返回任何内容

delete - Takes index, removes value at that index, and returns nothing delete-获取索引,删除该索引处的值,并且不返回任何内容


#6楼

While pop and delete both take indices to remove an element as stated in above comments. pop和delete都带有索引,以删除元素,如上面的注释所述。 A key difference is the time complexity for them. 一个关键的区别是它们的时间复杂度。 The time complexity for pop() with no index is O(1) but is not the same case for deletion of last element. 没有索引的pop()的时间复杂度为O(1),但删除最后一个元素的时间复杂度不同。

If your use case is always to delete the last element, it's always preferable to use pop() over delete(). 如果您的用例始终是删除最后一个元素,则始终首选使用pop()而不是delete()。 For more explanation on time complexities, you can refer to https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt 有关时间复杂度的更多说明,请参阅https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值