小编nei*_*kin的帖子

Python OrderedDict迭代

为什么我的python OrderedDict被初始化为'乱序'?

这里的解决方案不如解释那么有趣.这里有些东西,我只是没有得到,也许扩张会帮助别人和我.

>>> from collections import OrderedDict

>>> spam = OrderedDict(s = (1, 2), p = (3, 4), a = (5, 6), m = (7, 8))

>>> spam
OrderedDict([('a', (5, 6)), ('p', (3, 4)), ('s', (1, 2)), ('m', (7, 8))])

>>> for key in spam.keys():
...    print key    
...
#  this is 'ordered' but not the order I wanted....
a
p
s
m

# I was expecting (and wanting):
s
p
a
m
Run Code Online (Sandbox Code Playgroud)

python ordereddictionary

30
推荐指数
2
解决办法
4万
查看次数

标签 统计

ordereddictionary ×1

python ×1