Are order of keys() and values() in python dictionary guaranteed to be the same?
本问题已经有最佳答案,请猛点这里访问。
本地内置的python dict是否保证
1 2 | d = {'A':1, 'B':2, 'C':3, 'D':4 } # or any other content otherd = dict(zip(d.keys(), d.values())) |
我一直都有
不管是对还是错,我对主题上的任何引用指针都感兴趣。
PS:我知道上面的属性对于每个像字典一样的对象都不是真的,我只是想知道内置的dict。当我测试它时,它看起来好像是真的,这并不奇怪,因为对
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() , anditervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.
From the documentation for
Python 2.7 and above have ordered dictionaries,for which your statement:
1 | d == dict(zip(d.keys(), d.values())) |
会应用