如何保持键/值与声明的顺序相同? [英] How to keep keys/values in same order as declared?

查看:50
本文介绍了如何保持键/值与声明的顺序相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按特定顺序声明的字典,并希望一直保持该顺序.键/值实际上不能根据它们的值按顺序保留,我只希望按声明的顺序保留.

I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.

所以,如果我有字典:

d = {'ac': 33, 'gw': 20, 'ap': 102, 'za': 321, 'bs': 10}

如果我查看或遍历它的顺序不是这样,是否有任何方法可以确保Python保持我声明键/值的显式顺序?

It isn't in that order if I view it or iterate through it, is there any way to make sure Python will keep the explicit order that I declared the keys/values in?

推荐答案

从Python 3.6开始,默认情况下,标准dict类型保持插入顺序.

From Python 3.6 onwards, the standard dict type maintains insertion order by default.

定义

d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10}

将生成一个字典,其中的键按源代码中列出的顺序排列.

will result in a dictionary with the keys in the order listed in the source code.

这是通过将一个带有整数的简单数组用于稀疏哈希表来实现的,其中这些整数索引到另一个存储键-值对(加上计算得出的哈希)的数组中.后一个数组恰好按插入顺序存储项目,实际上,整个组合使用的内存少于Python 3.5及之前版本中使用的实现.有关详细信息,请参见Raymond Hettinger的原始创意帖子.

This was achieved by using a simple array with integers for the sparse hash table, where those integers index into another array that stores the key-value pairs (plus the calculated hash). That latter array just happens to store the items in insertion order, and the whole combination actually uses less memory than the implementation used in Python 3.5 and before. See the original idea post by Raymond Hettinger for details.

在3.6中,这仍然被视为实现细节;请参阅 Python 3.6的新功能文档:

In 3.6 this was still considered an implementation detail; see the What's New in Python 3.6 documentation:

此新实现的保留顺序方面被认为是实现细节,因此不应依赖(将来可能会更改,但是希望在更改前先使用该语言的新dict实现几个发行版,然后再进行更改规范所有当前和将来的Python实现的顺序保留语义的语言规范;这还有助于保持与仍旧有效的随机迭代顺序的旧版本语言(例如Python 3.5)的向后兼容性.

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.7将实现细节提升为语言规范,因此现在必须在所有与该版本或更高版本兼容的Python实现中保留dict的顺序.参见BDFL的发音.

Python 3.7 elevates this implementation detail to a language specification, so it is now mandatory that dict preserves order in all Python implementations compatible with that version or newer. See the pronouncement by the BDFL.

您可能仍要使用 collections.OrderedDict()在某些情况下,因为它在标准dict类型的基础上提供了一些附加功能.例如被可逆(此扩展为查看对象),并支持重新排序(通过 move_to_end()方法).

You may still want to use the collections.OrderedDict() class in certain cases, as it offers some additional functionality on top of the standard dict type. Such as as being reversible (this extends to the view objects), and supporting reordering (via the move_to_end() method).

这篇关于如何保持键/值与声明的顺序相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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