我试图使用键值对的字典来存储和访问基于适当的键列表。以下是我正在使用的。Python 2.7字典与键值对列表重新排序
newDict = {'mod':[0,2], 'pro':[2,3], 'st':[6,10]}
newDict2 = {'a':[0,2], 'b':[2,3], 'c':[6,10]}
我建立这个字典在一个特定的顺序,所以我可以用for
循环遍历它。但无论什么原因,当我打印或通过for
循环时,键的顺序被重新排序。
print "the newDict is: " + str(newDict)
print "the newDict2 is: " + str(newDict2)
for key in newDict:
print "the key is: " + str(key)
这个的输出如下。
newDict is: {'pro': [2, 3], 'mod': [0, 2], 'st': [6, 10]}
newDict2 is: {'a': [0, 2], 'c': [6, 10], 'b': [2, 3]}
the key is: pro
the key is: mod
the key is: st
我更关心键的顺序而不是列表的值。所以我不想按照列表的值排序。我一直在做研究,但不知道为什么它会改变顺序。我试着改变列表中的值来尝试找出它,但我一直无法找到一个模式。我认为在命令不是问题时使用了字典。也许点滴不是这样的。任何帮助是极大的赞赏。
除了在您的情况下命令*是一个问题。 –
有一个orderedDict这样的事情。我相信你想要使用那个 – bnjmn