我有一个字典,我按特定顺序声明,并希望始终保持该顺序.键/值不能根据它们的值按顺序保存,我只是按照我声明的顺序想要它.
所以,如果我有字典:
d = {'ac': 33, 'gw': 20, 'ap': 102, 'za': 321, 'bs': 10}
Run Code Online (Sandbox Code Playgroud)
如果我查看它或迭代它,它是不是按顺序,有没有办法确保Python将保持我声明键/值的显式顺序?
这是我的字典
propertyList = {
"id": "int",
"name": "char(40)",
"team": "int",
"realOwner": "int",
"x": "int",
"y": "int",
"description": "char(255)",
"port": "bool",
"secret": "bool",
"dead": "bool",
"nomadic": "bool",
"population": "int",
"slaves": "int",
}
Run Code Online (Sandbox Code Playgroud)
但是当我用"\n".join(myDict)打印出来时,我得到了这个
name
nomadic
dead
port
realOwner
secret
slaves
team
y
x
population
id
description
Run Code Online (Sandbox Code Playgroud)
我知道字典是无序的,但每次出现都是一样的,我不知道为什么.