如果我在 Python 中有一个字典,并且我遍历它一次,然后再一次,如果我没有插入、删除或更新字典中的任何项目,是否可以保证保留迭代顺序? (但我可能已经查过了)。
最佳答案
这里是 dict.items()
documentation说:
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.
我认为如果您所做的只是迭代,那么假设项目排序不会改变是合理的。
关于python - 如果不修改,dicts 会保留迭代顺序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1849324/