【发布时间】:2013-12-19 14:39:58
【问题描述】:
我是 Python 新手。
这里我定义了一个字典:
dic = {"hello":"is_done", "bye":'is_gone', "things":'thinking'};
然后我遍历它以使其按预期返回数据:
for item in dic :
print("this is an item from our dictionary: "+dic[item]);
这是输出:
this is an item from our dictionary: is_gone
this is an item from our dictionary: thinking
this is an item from our dictionary: is_done
为什么生成输出的迭代顺序与字典对象中 key:value 对的初始化顺序不同?
【问题讨论】:
-
同意重复,但这个答案可以改进:)。记住字典中的键是一个集合,而不是一个列表。
-
字典没有以任何明显的方式排序,这就是为什么这些值打印的顺序与您预期的不符。查看 Eric 提供的链接以获取更多信息。
-
谢谢我现在知道了。非常感谢
标签: python dictionary iteration