d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10}
将生成一个字典,其中的键按源代码中列出的顺序排列。
这是通过在稀疏散列表中使用一个带有整数的简单数组来实现的,其中这些整数索引到另一个存储键值对(加上计算出的散列)的数组中。后一个数组恰好按插入顺序存储项,而整个组合实际使用的内存比Python3.5和之前使用的实现少。有关详细信息,请参见original idea post by Raymond Hettinger。
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).
从Python 3.6开始,标准的
dict
类型默认保持插入顺序。定义
将生成一个字典,其中的键按源代码中列出的顺序排列。
这是通过在稀疏散列表中使用一个带有整数的简单数组来实现的,其中这些整数索引到另一个存储键值对(加上计算出的散列)的数组中。后一个数组恰好按插入顺序存储项,而整个组合实际使用的内存比Python3.5和之前使用的实现少。有关详细信息,请参见original idea post by Raymond Hettinger。
在3.6中,这仍然被认为是一个实现细节;请参见What's New in Python 3.6 documentation:
Python 3.7将这个实现细节提升到一个语言规范,因此现在必须
dict
在所有与该版本或更新版本兼容的Python实现中保持顺序。请参阅pronouncement by the BDFL。在某些情况下,您可能仍然希望使用^{} class ,因为它在标准} method )。
dict
类型的基础上提供了一些附加功能。例如成为reversible(这扩展到view objects),支持重新排序(通过^{我将举一个简单的例子,而不是解释理论部分。
包含
如果值是
True
(或任何其他不可变对象),也可以使用:相关问题 更多 >
编程相关推荐