我是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
对的顺序不同?
答案 0 :(得分:1)
正如一些评论已经指出的那样:python中的内置词是无序的,人们可以对迭代中出现的东西的顺序做出假设。如果您需要订单,请使用Ordered Dicts。
保持dict顺序涉及插入的额外成本,并且通常不需要将它用于大多数应用程序。这就是为什么基本人不关心它。