【发布时间】:2013-01-04 22:08:30
【问题描述】:
我对从以下得到的输出有点困惑。 我不明白执行循环的顺序。
domains = { "de": "Germany", "sk": "Slovakia", "hu": "Hungary",
"us": "United States", "no": "Norway" }
for key in domains:
print key
这里的输出是
sk
de
no
us
hu
但不是
de
sk
hu
us
no
同样,这里
num = {1:"one",4:"two",23:"three",10:"four"}
for key in num:
print key
output is
1
10
4
23
但不是
1
4
23
10
感谢您的帮助
【问题讨论】:
-
如果你真的感兴趣,请看我对这个问题的回答:stackoverflow.com/questions/12165200/…
-
字典中的插入顺序从 Python3.7 开始保留。参见python文档中的information on iterator, iter(), for dictionaries,也可以在堆栈溢出中找到more detailed explanation
标签: python dictionary for-loop