所以下面的内容让我感到困惑。
#!/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]
也许我基本上误解了pop的实现方式。但我的理解是,它会删除列表中给定索引处的项目,并返回该项目。如果没有指定索引,它将默认删除最后一个项目。因此,在第一次循环中,它应该从列表左侧删除3个项目,在第二次循环中,它应该从列表末尾删除3个项目。