Python:dict的元素顺序是错误的

时间:2016-04-19 01:06:36

标签: python dictionary

我有以下代码:

tweet = {"user" : "joelgrus", "text" : "Data Science is Awesome", "retweet_count" : 100}

print "tweet:", tweet

运行脚本后的输出是:

tweet: {'text': 'Data Science is Awesome', 'retweet_count': 100, 'user': 'joelgrus'}

为什么元素的顺序(看似)是错误的?我认为输出应该是:

tweet: {'user': 'joelgrus', 'text': 'Data Science is Awesome', 'retweet_count': 100}

1 个答案:

答案 0 :(得分:4)

词典不会按顺序保留其列表:https://docs.python.org/2/library/stdtypes.html#typesmapping

该页面上的说明解释了:

  

CPython实现细节:键和值列在   非随机的任意顺序因Python而异   实现,并取决于字典的插入历史   和删除。

相关问题