Why is it not safe to modify sequence being iterated on?
It is not safe to modify the sequence being iterated over in the loop (this can only happen for mutable sequence types, such as lists). If you need to modify the list you are iterating over (for example, to duplicate selected items) you must iterate over a copy. The slice notation makes this particularly convenient:
>>> for x in a[:]: # make a slice copy of the entire list ... if len(x) > 6: a.insert(0, x) ... >>> a ['defenestrate', 'cat', 'w开发者_开发百科indow', 'defenestrate']
why is it not safe to just do for x in a
??
Without getting too technical:
If you're iterating through a mutable sequence in Python and the sequence is changed while it's being iterated through, it is not always entirely clear what will happen. If you insert an element in the sequence while iterating through it, what would now reasonably be considered the "next" element in the sequence? What if you delete the next object?
For this reason, iterating through a mutable sequence while you're changing it leads to unspecified behaviour. Anything might happen, depending on exactly how the list is implemented. :-)
This is a common problem in many languages. If you have a linear data structure, and you are iterating over it, something must keep track of where you are in the structure. It might be a current index, or a pointer, but it's some kind of finger pointing to the "current place".
If you modify the list while the iteration is happening, that cursor will likely be incorrect.
A common problem is that you remove the item under the cursor, everything slides down one, the next iteration of the loop increments the cursor, and you've inadvertently skipped an item.
Some data structure implementations offer the ability to delete items while iterating, but most do not.
When you modify a collection that you are iterating over the iterator can behave unexpectedly, for example missing out items or returning the same item twice.
This code loops indefinitely when I run it:
>>> a = [ 'foo', 'bar', 'baz' ]
>>> for x in a:
... if x == 'bar': a.insert(0, 'oops')
This is because the iterator uses a numeric index to keep track of where it is in the list. Adding an item at the beginning of the list results in the item 'bar' being returned again instead of the iterator advancing to the next item, because it has been shifted forward by the insertion.
精彩评论