我試圖使用鍵值對的字典來存儲和訪問基於適當的鍵列表。以下是我正在使用的。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