字典是否在 Python 3.6+ 中排序? [英] Are dictionaries ordered in Python 3.6+?

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

问题描述

字典在 Python 3.6 中排序(至少在 CPython 实现下)与以前的化身不同.这似乎是一个重大变化,但它只是 中的一小段文档.它被描述为 CPython 实现细节而不是语言特性,但也暗示这可能在未来成为标准.

新的字典实现如何在保持元素顺序的同时比旧的执行得更好?

以下是文档中的文字:

<块引用>

dict() 现在使用紧凑"表示 由 PyPy 开创.与 Python 3.5 相比,新 dict() 的内存使用量减少了 20% 到 25%.PEP 468(在函数中保留 **kwargs 的顺序.)已实现这样.这个新实现的顺序保留方面被认为是一个实现细节,不应依赖(这可能会在未来发生变化,但在更改语言规范之前,希望在几个版本的语言中使用这个新的 dict 实现为所有当前和未来的 Python 实现强制要求保留顺序的语义;这也有助于保持与旧版本的语言的向后兼容性,其中随机迭代顺序仍然有效,例如 Python 3.5).(由 INADA Naoki 在 issue 27350 中贡献.想法 最初由 Raymond Hettinger 建议.)

2017 年 12 月更新:dict 的保留插入顺序是 保证适用于 Python 3.7

解决方案

字典是否在 Python 3.6+ 中排序?

它们是插入顺序[1].从 Python 3.6 开始,对于 Python 的 CPython 实现,字典记住插入项目的顺序.这被认为是 Python 3.6 中的一个实现细节;如果您希望在其他 Python 实现中保证的插入排序(以及其他有序行为[1]<),则需要使用 OrderedDict/sup>).

从 Python 3.7 开始,这不再是实现细节,而是成为一种语言功能.来自 GvR 的 python-dev 消息::><块引用>

就这样吧.Dict 保持插入顺序"是裁决.谢谢!

这只是意味着您可以依赖它.Python 的其他实现如果希望成为 Python 3.7 的符合实现,也必须提供插入顺序字典.


<块引用>

Python 3.6 字典实现如何在保持元素顺序的同时比旧的字典实现更好[2]?

本质上,通过保留两个数组.

  • 第一个数组,dk_entries,保存条目(of按插入顺序为字典键入 PyDictKeyEntry).保持顺序是通过这是一个仅附加数组来实现的,其中新项目总是插入到最后(插入顺序).

  • 第二个,dk_indices,保存dk_entries 数组的索引(即,表示dk_entries 中相应条目位置的值).该数组充当哈希表.当一个键被散列时,它会导致存储在 dk_indices 中的索引之一,并且通过索引 dk_entries 获取相应的条目.由于仅保留索引,因此该数组的类型取决于字典的整体大小(从 type int8_t(1 字节) 到 int32_t/int64_t (4/8 字节)32/64 位构建)

在之前的实现中,必须分配一个类型为 PyDictKeyEntry 和大小为 dk_size 的稀疏数组;不幸的是,它也导致了很多空白空间,因为该数组不允许超过 2/3 * dk_size出于性能原因.(并且空白区域仍然具有PyDictKeyEntry大小!).

现在情况并非如此,因为只存储了必需的条目(那些已插入的条目)和一个 intX_t 类型的稀疏数组(X 取决于 dict 大小) 2/3 * dk_sizes 保持完整.空格从类型 PyDictKeyEntry 更改为 intX_t.

因此,显然,创建 PyDictKeyEntry 类型的稀疏数组比用于存储 int s 的稀疏数组需要更多内存.

您可以在 Python-Dev 上查看完整对话 关于这个功能,如果有兴趣,这是一个很好的阅读.


在 Raymond Hettinger 提出的原始提案中,可以看到所用数据结构的可视化,它抓住了这个想法的要点.

<块引用>

例如字典:

d = {'timmy': 'red', 'barry': 'green', 'guido': 'blue'}

当前存储为[keyhash, key, value]:

entries = [['--', '--', '--'],[-8522787127447073495,'巴里','绿色'],['--', '--', '--'],['--', '--', '--'],['--', '--', '--'],[-9092791511155847987,'蒂米','红色'],['--', '--', '--'],[-6480567542315338377,'guido','蓝色']]

相反,数据应按如下方式组织:

indices = [None, 1, None, None, None, 0, None, 2]条目 = [[-9092791511155847987,'蒂米','红色'],[-8522787127447073495,'巴里','绿色'],[-6480567542315338377,'guido','蓝色']]

正如您现在可以直观地看到的那样,在最初的提案中,很多空间基本上是空的,以减少冲突并加快查找速度.使用新方法,您可以通过在索引中将稀疏移动到真正需要的地方来减少所需的内存.


<子>[1]:我说插入有序"而不是有序",因为随着 OrderedDict 的存在,有序"暗示了`dict` 对象*不提供*的进一步行为.OrderedDicts 是可逆的,提供顺序敏感的方法,并且主要提供顺序敏感的相等性测试(`==`、`!=`).`dict`s 目前不提供任何这些行为/方法.
<子>[2]:通过更紧凑的设计,新的字典实现在**内存方面**性能更好;这是这里的主要好处.速度明智,差异并不那么大,新字典可能会引入轻微回归的地方(key-lookups,例如),而在其他情况下(想到迭代和调整大小),应该存在性能提升.<子>总体而言,由于引入了紧凑性,字典的性能,尤其是在现实生活中的性能有所提高.

Dictionaries are ordered in Python 3.6 (under the CPython implementation at least) unlike in previous incarnations. This seems like a substantial change, but it's only a short paragraph in the documentation. It is described as a CPython implementation detail rather than a language feature, but also implies this may become standard in the future.

How does the new dictionary implementation perform better than the older one while preserving element order?

Here is the text from the documentation:

dict() now uses a "compact" representation pioneered by PyPy. The memory usage of the new dict() is between 20% and 25% smaller compared to Python 3.5. PEP 468 (Preserving the order of **kwargs in a function.) is implemented by this. The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5). (Contributed by INADA Naoki in issue 27350. Idea originally suggested by Raymond Hettinger.)

Update December 2017: dicts retaining insertion order is guaranteed for Python 3.7

解决方案

Are dictionaries ordered in Python 3.6+?

They are insertion ordered[1]. As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. This is considered an implementation detail in Python 3.6; you need to use OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python (and other ordered behavior[1]).

As of Python 3.7, this is no longer an implementation detail and instead becomes a language feature. From a python-dev message by GvR:

Make it so. "Dict keeps insertion order" is the ruling. Thanks!

This simply means that you can depend on it. Other implementations of Python must also offer an insertion ordered dictionary if they wish to be a conforming implementation of Python 3.7.


How does the Python 3.6 dictionary implementation perform better[2] than the older one while preserving element order?

Essentially, by keeping two arrays.

  • The first array, dk_entries, holds the entries (of type PyDictKeyEntry) for the dictionary in the order that they were inserted. Preserving order is achieved by this being an append only array where new items are always inserted at the end (insertion order).

  • The second, dk_indices, holds the indices for the dk_entries array (that is, values that indicate the position of the corresponding entry in dk_entries). This array acts as the hash table. When a key is hashed it leads to one of the indices stored in dk_indices and the corresponding entry is fetched by indexing dk_entries. Since only indices are kept, the type of this array depends on the overall size of the dictionary (ranging from type int8_t(1 byte) to int32_t/int64_t (4/8 bytes) on 32/64 bit builds)

In the previous implementation, a sparse array of type PyDictKeyEntry and size dk_size had to be allocated; unfortunately, it also resulted in a lot of empty space since that array was not allowed to be more than 2/3 * dk_size full for performance reasons. (and the empty space still had PyDictKeyEntry size!).

This is not the case now since only the required entries are stored (those that have been inserted) and a sparse array of type intX_t (X depending on dict size) 2/3 * dk_sizes full is kept. The empty space changed from type PyDictKeyEntry to intX_t.

So, obviously, creating a sparse array of type PyDictKeyEntry is much more memory demanding than a sparse array for storing ints.

You can see the full conversation on Python-Dev regarding this feature if interested, it is a good read.


In the original proposal made by Raymond Hettinger, a visualization of the data structures used can be seen which captures the gist of the idea.

For example, the dictionary:

d = {'timmy': 'red', 'barry': 'green', 'guido': 'blue'}

is currently stored as [keyhash, key, value]:

entries = [['--', '--', '--'],
           [-8522787127447073495, 'barry', 'green'],
           ['--', '--', '--'],
           ['--', '--', '--'],
           ['--', '--', '--'],
           [-9092791511155847987, 'timmy', 'red'],
           ['--', '--', '--'],
           [-6480567542315338377, 'guido', 'blue']]

Instead, the data should be organized as follows:

indices =  [None, 1, None, None, None, 0, None, 2]
entries =  [[-9092791511155847987, 'timmy', 'red'],
            [-8522787127447073495, 'barry', 'green'],
            [-6480567542315338377, 'guido', 'blue']]

As you can visually now see, in the original proposal, a lot of space is essentially empty to reduce collisions and make look-ups faster. With the new approach, you reduce the memory required by moving the sparseness where it's really required, in the indices.


[1]: I say "insertion ordered" and not "ordered" since, with the existence of OrderedDict, "ordered" suggests further behavior that the `dict` object *doesn't provide*. OrderedDicts are reversible, provide order sensitive methods and, mainly, provide an order-sensive equality tests (`==`, `!=`). `dict`s currently don't offer any of those behaviors/methods.
[2]: The new dictionary implementations performs better **memory wise** by being designed more compactly; that's the main benefit here. Speed wise, the difference isn't so drastic, there's places where the new dict might introduce slight regressions (key-lookups, for example) while in others (iteration and resizing come to mind) a performance boost should be present. Overall, the performance of the dictionary, especially in real-life situations, improves due to the compactness introduced.

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

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