不同Python dict()中的键顺序 [英] Order of keys in a different Python dict()
本文介绍了不同Python dict()中的键顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
Python字典,按照声明的顺序保持键/值。
Possible Duplicate:
Python dictionary, keep keys/values in same order as declared
如果我在一个特定的程序中有两个不同的字典,具有相同的键(但是不同的值),那么.keys()是否是相同的顺序?我做了几个测试,似乎是这样,但是不知道如何这个内部人物,我不知道这是否得到保证。
If I have in a particular program two different dictionaries with the same keys (but different values), will .keys() be in the same order? I made a few tests and it seems to be the case, but without knowing are how the internals of the dict I am not sure if this is guaranteed.
谢谢,
推荐答案
你不能依赖于全部:
>>> {1: None, 9: None}
{1: None, 9: None}
>>> {9: None, 1: None}
{9: None, 1: None}
>>> {1: None, 2: None}
{1: None, 2: None}
>>> {2: None, 1: None}
{1: None, 2: None}
< >字典无序在Python 2.7中,存在 collections.OrderedDict
。
这篇关于不同Python dict()中的键顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文