以下代码:
a = list(range(10))
remove = False
for b in a:
if remove:
a.remove(b)
remove = not remove
print(a)
使用 Python 时输出 [0, 2, 3, 5, 6, 8, 9]
,而不是 [0, 2, 4, 6, 8]
3.2.
Outputs [0, 2, 3, 5, 6, 8, 9]
, instead of [0, 2, 4, 6, 8]
when using Python 3.2.
请注意,我并不是要解决这种行为,而是要理解它.
Note that I am not looking to work around the behaviour, but to understand it.
我争论了一段时间来回答这个问题,因为类似的问题在这里已经被问过很多次了.但它的独特性足以让人们从怀疑中受益.(不过,如果其他人投票结束,我不会反对.)这是对正在发生的事情的直观解释.
I debated answering this for a while, because similar questions have been asked many times here. But it's just unique enough to be given the benefit of the doubt. (Still, I won't object if others vote to close.) Here's a visual explanation of what is happening.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <- b = 0; remove? no
^
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <- b = 1; remove? yes
^
[0, 2, 3, 4, 5, 6, 7, 8, 9] <- b = 3; remove? no
^
[0, 2, 3, 4, 5, 6, 7, 8, 9] <- b = 4; remove? yes
^
[0, 2, 3, 5, 6, 7, 8, 9] <- b = 6; remove? no
^
[0, 2, 3, 5, 6, 7, 8, 9] <- b = 7; remove? yes
^
[0, 2, 3, 5, 6, 8, 9] <- b = 9; remove? no
^
由于没有其他人有,我将尝试回答您的其他问题:
Since no one else has, I'll attempt to answer your other questions:
为什么没有给出错误指示底层迭代器正在被修改?
Why is no error given to indicate that underlying iterator is being modified?
要在不禁止许多完全有效的循环构造的情况下抛出错误,Python 必须很多了解正在发生的事情,并且它可能必须在运行时获取该信息.所有这些信息都需要时间来处理.它会让 Python 慢很多,只是在速度真正重要的地方——一个循环.
To throw an error without prohibiting many perfectly valid loop constructions, Python would have to know a lot about what's going on, and it would probably have to get that information at runtime. All that information would take time to process. It would make Python a lot slower, in just the place where speed really counts -- a loop.
在这种行为方面,与早期版本的 Python 相比,机制是否发生了变化?
Have the mechanics changed from earlier versions of Python with respect to this behaviour?
简而言之,没有.或者至少我高度对此表示怀疑,而且自从我学习 Python (2.4) 以来它的表现肯定是这样的.坦率地说,我希望可变序列的任何直接实现都以这种方式运行.哪位知道的好,请指正.(实际上,快速文档查找确认 从 version 1.4!)
In short, no. Or at least I highly doubt it, and certainly it has behaved this way since I learned Python (2.4). Frankly I would expect any straightforward implementation of a mutable sequence to behave in just this way. Anyone who knows better, please correct me. (Actually, a quick doc lookup confirms that the text that Mikola cited has been in the tutorial since version 1.4!)
这篇关于在迭代列表时从列表中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!