字典的字符串表示形式在Python 3.4中是否有顺序? [英] Do string representations of dictionaries have order in Python 3.4?

查看:128
本文介绍了字典的字符串表示形式在Python 3.4中是否有顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Python中的字典本身没有顺序。但是,如果您总是按相同的顺序在字典上调用 str(),我会很好奇。无论我添加项目的顺序如何,它似乎都是按键排序的:

I know dictionaries themselves in Python do not have order. However, I'm rather curious if when you call str() on a dictionary if it is always in the same order. It appears to be sorted (by key), no matter which order I add items:

d={}
d[5]=5
d[1]=1
d["z"]="z"
d["a"]="a"
s=str(d)
print(s)

我知道很多人会很想说它没有排序,但是请尝试通过获取未排序的结果来证明我是错误的。

I know a lot of people will be tempted to say it's not sorted, but please try to prove me wrong by getting unsorted results.

因此,默认情况下,在Python 3.4中,字典是否已转换为排序的字符串?

So, are dictionaries converted to strings sorted, by default, in Python 3.4?

推荐答案


注意: Python 3.6引入了新的 dict 的订单保留实​​现 ,这使以下内容从3.6开始不再适用。

Note: Python 3.6 introduces a new, order-preserving implementation of dict, which makes the following obsolete from 3.6 onwards.




这是您的示例在三个不同的Python 3.4解释器中的三个迭代会话:


Here are three iterations of your example in three different Python 3.4 interpreter sessions:

Python 3.4.1 (default, Aug  8 2014, 15:05:42) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d={}
>>> d[5]=5
>>> d[1]=1
>>> d["z"]="z"
>>> d["a"]="a"
>>> s=str(d)
>>> print(s)
{1: 1, 'z': 'z', 'a': 'a', 5: 5}



Python 3.4.1 (default, Aug  8 2014, 15:05:42) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d={}
>>> d[5]=5
>>> d[1]=1
>>> d["z"]="z"
>>> d["a"]="a"
>>> s=str(d)
>>> print(s)
{1: 1, 'a': 'a', 5: 5, 'z': 'z'}



Python 3.4.1 (default, Aug  8 2014, 15:05:42) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d={}
>>> d[5]=5
>>> d[1]=1
>>> d["z"]="z"
>>> d["a"]="a"
>>> s=str(d)
>>> print(s)
{1: 1, 5: 5, 'z': 'z', 'a': 'a'}

因此,不,字符串表示没有排序,甚至在解释器的调用中没有相同的顺序。在3.2及以下版本的Python中,字典(及其字符串表示形式)的顺序是任意的,但是是一致的-但是,由于安全修复程序

So, no, the string representation is not sorted, or even in the same order across invocations of the interpreter. In versions of Python up to and including 3.2, the order of dictionaries (and their string representations) was arbitrary but consistent - however, this changed in Python 3.3 as a result of a security fix:


默认情况下, str,字节和日期时间对象的 __ hash __()值会以不可预测的随机值成盐。尽管它们在单个Python进程中保持不变,但在重复调用Python之间是不可预测的。

By default, the __hash__() values of str, bytes and datetime objects are "salted" with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.

这是为了防止由于精心选择的输入而导致的拒绝服务利用dict插入的最坏情况的性能O(n ^ 2)复杂度。参见 http://www.ocert.org/advisories/ocert-2011-003 .html

This is intended to provide protection against a denial-of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details.

更改哈希值会影响字典,集合和其他映射的迭代顺序。 Python从未保证过这种顺序(通常在32位和64位版本之间有所不同)。

Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).

这篇关于字典的字符串表示形式在Python 3.4中是否有顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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