循环似乎不遵循顺序 [英] Looping seems to not follow sequence

查看:68
本文介绍了循环似乎不遵循顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感觉这里缺少明显的东西!

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)

输出:

a
aa
aaaa
aaaaa
aaa

当然可以应该输出:

a
aa
aaa
aaaa
aaaaa

这里出了什么问题?

推荐答案

词典没有命令。如果需要依赖顺序,则需要一个OrderedDict-Python 2.7的 collections 模块中有一个,或者您可以使用多种食谱之一。

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.

这篇关于循环似乎不遵循顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆