可能重复:
为什么python会这样排序我的字典?
为什么迭代这个词典
d = {'tors':None,
'head':None,
'armr':None,
'arml':None,
'legl':None,
'legr':None}
for k in d.keys():
print k
Run Code Online (Sandbox Code Playgroud)
将以不同的顺序输出键:
head
legl
armr
arml
tors
legr
Run Code Online (Sandbox Code Playgroud)
在python dict中实现为哈希映射,没有人不能保证键顺序.从文档
CPython实现细节:键和值以任意顺序列出,这是非随机的,在Python实现中各不相同,并且取决于字典的插入和删除历史.
如果您需要继续订购,请collections.OrderedDict改用.