Python pop() 与 pop(0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24153511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python pop() vs pop(0)
提问by PerryDaPlatypus
So the following is confusing me.
所以下面的内容让我很困惑。
#!/usr/bin/python
test = [0, 0, 0, 1, 2, 3, 4, 5, 6]
test1 = [0, 0, 0, 1, 2, 3, 4, 5, 6]
for _dummy in test:
if(_dummy == 0):
test.pop()
for _dummy in test1:
if(_dummy == 0):
test1.pop(0)
print test
print test1
Results
结果
ubuntu-vm:~/sandbox$ ./test.py
[0, 0, 0, 1, 2, 3]
[0, 1, 2, 3, 4, 5, 6]
Perhaps, I'm fundamentally misunderstanding how pop is implemented. But my understanding is that it removes the item at the given index in the list, and returns it. If no index is specified, it defaults to the last item. So it would seem that in the first loop it should remove 3 items from the left of the list, and in the second loop it should remove 3 items from the end of the list.
也许,我从根本上误解了 pop 是如何实现的。但我的理解是它删除列表中给定索引处的项目,并返回它。如果未指定索引,则默认为最后一项。所以看起来在第一个循环中它应该从列表的左侧删除 3 个项目,在第二个循环中它应该从列表的末尾删除 3 个项目。
采纳答案by nneonneo
The first test isn't surprising; three elements are removed off the end.
第一次测试并不奇怪。三个元素从末尾删除。
The second test is a bit surprising. Only two elements are removed. Why?
第二个测试有点令人惊讶。只删除了两个元素。为什么?
List iteration in Python essentially consists of an incrementing index into the list. When you delete an element you shift all the elements on the right over. This may cause the index to point to a different element.
Python 中的列表迭代本质上由列表中的递增索引组成。当你删除一个元素时,你会将右边的所有元素都移过去。这可能会导致索引指向不同的元素。
Illustratively:
举例说明:
start of loop
[0,0,0,1,2,3,4,5,6]
^ <-- position of index
delete first element (since current element = 0)
[0,0,1,2,3,4,5,6]
^
next iteration
[0,0,1,2,3,4,5,6]
^
delete first element (since current element = 0)
[0,1,2,3,4,5,6]
^
and from now on no zeros are encountered, so no more elements are deleted.
从现在开始不会遇到零,因此不再删除元素。
To avoid confusion in the future, try not to modify lists while you're iterating over them. While Python won't complain (unlike dictionaries, which cannot be modified during iteration), it will result in weird and usually counterintuitive situations like this one.
为避免将来混淆,请尽量不要在迭代列表时修改列表。虽然 Python 不会抱怨(不像字典,它不能在迭代过程中修改),但它会导致像这样的奇怪且通常违反直觉的情况。
回答by sth
You are modifying the lists while you are iterating over them, causing confusion. If you look at the first element, remove it and then continue with looking at the second element, then you missed one element.
您在迭代列表时正在修改列表,从而导致混淆。如果您查看第一个元素,将其删除,然后继续查看第二个元素,则您错过了一个元素。
The element that was originally in second place was never inspected, because it "changed places" during iteration.
最初排在第二位的元素从未被检查过,因为它在迭代过程中“改变了位置”。
回答by sundar nataraj
since in list or Stack works in last in first out[LIFO] so pop()
is used it removes last element in your list
由于 in 列表或 Stack 以后进先出 [LIFO] 的方式工作,因此pop()
使用它会删除列表中的最后一个元素
where as pop(0)
means it removes the element in the index that is first element of the list
其中 aspop(0)
意味着它删除索引中的元素,即列表的第一个元素
as per the Docs
根据文档
list.pop([i]):
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)