这三种从列表中删除元素的方法有什么区别吗?
>>> 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主要作用于值。 删除和弹出工作在索引上
Remove基本上删除了第一个匹配的值。 Delete从指定索引中删除项 Pop基本上接受一个索引并返回该索引的值。下次打印列表时,该值不会出现。
其他回答
不同数据结构上的任何操作/函数都是为特定操作定义的。这里在你的情况下,即删除一个元素,删除,弹出和删除。(如果你考虑集合,添加另一个操作-丢弃) 另一种令人困惑的情况是加法。插入/追加。 为了演示,让我们实现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.
下面是一个详细的答案。
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将索引作为参数,并删除该索引处的元素
与del不同,当在list object上调用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获取参数值并从列表中删除该值。
如果存在多个值,则删除第一个值
注意:如果该值不存在,将抛出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]
希望这个答案对你有帮助。
而pop和delete都采用索引来删除上面注释中所述的元素。一个关键的区别是它们的时间复杂度。没有索引的pop()的时间复杂度是O(1),但删除最后一个元素的情况不同。
如果您的用例总是删除最后一个元素,那么使用pop()而不是delete()总是更好的。有关时间复杂性的更多解释,请参阅https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt
从列表中删除元素的三种不同方法的效果:
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
Remove主要作用于值。 删除和弹出工作在索引上
Remove基本上删除了第一个匹配的值。 Delete从指定索引中删除项 Pop基本上接受一个索引并返回该索引的值。下次打印列表时,该值不会出现。