python字典中的keys()和values()的顺序是否保证相同?

Are order of keys() and values() in python dictionary guaranteed to be the same?

本问题已经有最佳答案,请猛点这里访问。

本地内置的python dict是否保证keys()values()列表的排序方式相同?

1
2
d = {'A':1, 'B':2, 'C':3, 'D':4 } # or any other content
otherd = dict(zip(d.keys(), d.values()))

我一直都有d == otherd吗?

不管是对还是错,我对主题上的任何引用指针都感兴趣。

PS:我知道上面的属性对于每个像字典一样的对象都不是真的,我只是想知道内置的dict。当我测试它时,它看起来好像是真的,这并不奇怪,因为对keys()values()使用相同的顺序可能是最简单的实现。但我想知道这种行为是否被明确定义。


Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary's history of insertions and deletions. If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.

From the documentation for dict


Python 2.7 and above have ordered dictionaries,for which your statement:

1
d == dict(zip(d.keys(), d.values()))

会应用