如果没有修改,是否可以保留迭代顺序? [英] Do dicts preserve iteration order if they are not modified?
问题描述
这是什么 dict.items()
文档说:
dict.items()返回一个字典的(key,value)对列表的副本。
如果调用items(),keys(),values(),iteritems(),iterkeys()和itervalues()),而不会对字典进行任何修改,则列表将直接对应。 / p>
我认为如果您所做的是迭代,项目排序不会改变是合理的。
If I have a dictionary in Python, and I iterate through it once, and then again later, is the iteration order guaranteed to be preserved given that I didn't insert, delete, or update any items in the dictionary? (But I might have done look-ups).
Here is what dict.items()
documentation says:
dict.items() return a copy of the dictionary’s list of (key, value) pairs.
If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.
I think it's reasonable to assume that item ordering won't change if all you do is iteration.
这篇关于如果没有修改,是否可以保留迭代顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!