python循环似乎不遵循序列? [英] python looping seems to not follow sequence?
本文介绍了python循环似乎不遵循序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我觉得我在这里缺少一些明显的东西!
seq = {'a':['1'] ,'aa':['2'],'aaa':['3'],'aaaa':['4'],'aaaaa':['5']}
for se in seq:
打印s
输出:
aa
aaaa
aaaaa
aaa
wheras肯定应该输出:
a
aa
aaa
aaaa
aaaaa
这里出了什么问题?
谢谢:)
解决方案
字典没有订购。如果您需要依赖订购,您需要一个OrderedDict - 在Python 2.7中的集合
模块中有一个,或者您可以使用许多配方之一。 p>
i feel like i'm missing something obvious here!
seq = {'a': ['1'], 'aa': ['2'], 'aaa': ['3'], 'aaaa': ['4'], 'aaaaa': ['5']}
for s in seq:
print s
outputs:
a
aa
aaaa
aaaaa
aaa
wheras surely it should output:
a
aa
aaa
aaaa
aaaaa
what's going wrong here?
thanks :)
解决方案
Dictionaries are not ordered. If you need to rely on the ordering, you need an OrderedDict - there's one in the collections
module in Python 2.7, or you can use one of the many recipes around.
这篇关于python循环似乎不遵循序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文