所以下面是令我困惑的。Python pop()vs pop(0)
#!/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
结果
ubuntu-vm:~/sandbox$ ./test.py
[0, 0, 0, 1, 2, 3]
[0, 1, 2, 3, 4, 5, 6]
也许,我从根本上误解的流行是如何实现的。但我的理解是,它会删除列表中给定索引处的项目,并将其返回。如果没有指定索引,则默认为最后一个项目。所以看起来,在第一个循环中它应该从列表的左边移除3个项目,而在第二个循环中它应该从列表的末尾移除3个项目。
这在我的机器上完美地工作。我正在使用Python 2.7.6 – XrXrXr
在遍历它时不能对列表进行变异。 – univerio
我在两个不同的系统上试过这个。其一,是由运行python 2.6的大学提供的IDE。另一个是在我的虚拟机ubuntu上运行2.7.4。嗯。 – PerryDaPlatypus