将字典传递给OrderedDict有什么问题? [英] What's wrong with passing a dict to OrderedDict?

查看:77
本文介绍了将字典传递给OrderedDict有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读@Martijn Pieters对将字典转换为OrderedDict 的回复。他回答的重点是,将常规词典传递给 OrderedDict()不会保留所需的顺序,因为您传递的词典已经丢失了任何内容外观相似。他的解决方案是传递组成dict的键/值对的元组。

I'm reading @Martijn Pieters' response to Converting dict to OrderedDict. The main point of his answer is that passing a regular dict to OrderedDict() will not retain the order as desired, because the dict that you are passing has already "lost" any semblance of order. His solution is to pass tuples that make up the dict's key/value pairs instead.

但是,我在文档


在3.6版中进行了更改:在接受PEP 468的情况下,保留了传递给OrderedDict的关键字参数的订单

Changed in version 3.6: With the acceptance of PEP 468, order is retained for keyword arguments passed to the OrderedDict

这是否会使Martijn指出的问题无效(您现在可以将字典传递给OrderedDict),还是我误解了?

Does this invalidate the issue that Martijn points out (can you now pass a dict to OrderedDict), or am I misinterpreting?

from collections import OrderedDict

ship = {'NAME': 'Albatross',
         'HP':50,
         'BLASTERS':13,
         'THRUSTERS':18,
         'PRICE':250}
print(ship) # order lost as expected
{'BLASTERS': 13, 'HP': 50, 'NAME': 'Albatross', 'PRICE': 250, 'THRUSTERS': 18}
print(OrderedDict(ship)) # order preserved even though a dict is passed?
OrderedDict([('NAME', 'Albatross'),
             ('HP', 50),
             ('BLASTERS', 13),
             ('THRUSTERS', 18),
             ('PRICE', 250)])

我也得到了相同的结果(正确)的顺序,如果我也在OrderedDict上的... 循环中运行 for键,似乎暗示可以通过dict本身。

I get this same (correct) order if I run a for key in ... loop over the OrderedDict as well, seeming to imply it's OK to pass the dict itself.

编辑:这也加剧了我的困惑:是否在Python 3.6+中订购了字典?

Edit: this was also contributing a bit to my confusion: Are dictionaries ordered in Python 3.6+?

推荐答案


保留传递给OrderedDict的关键字参数的顺序

Order is retained for keyword arguments passed to the OrderedDict

这意味着以下内容是 guaranteed 保留订单:

What this means is that the following is guaranteed to preserve the order:

od = OrderedDict(a=20, b=30, c=40, d=50)

即关键字参数的顺序传递的保留在 ** kwargs 中。在Python 3.6中,这是语言功能;所有其他实现都需要跟进。

that is, the order in which the keyword arguments are passed is retained in **kwargs. This, in Python 3.6, is a language feature; all other implementations need to follow suit.

工作原理是,为了执行此调用,将创建一个包含关键字参数的字典。作为 dict ,在 3.6 之前,它丢失了有关提供顺序的信息。

How this works is, in order for this call to be performed, a dictionary is created that holds the keyword arguments. Being a dict, prior to 3.6, it lost information about the order in which these were supplied.

随着PEP 468在3.6中被接受,现在可以保证使用保持此信息的有序映射(在CPython中, 有序映射碰巧是 dict ,但这是一个实现细节-更新: Python 3.7起的语言功能。)

With PEP 468 getting accepted in 3.6, this is guaranteed to now use an ordered mapping that holds on to this information (in CPython, the "ordered mapping" happens to be a dict but, that's an implementation detail -- Update: A language feature as of Python 3.7.).

使用 OrderedDict(ship)也是一样将订单保留在 3.6 中,因为 dict 现在具有该实现,而不是由于PEP468。这是您不应该做的事情它取决于CPython实现的实现细节;将来可能会改变(看起来会改变),但是在那之前,您不应该依赖它。

Using OrderedDict(ship), as you currently do, also preserves the order in 3.6 because dict has that implementation now, not due to PEP 468. This is something you shouldn't depend on as it is considered an implementation detail of the CPython implementation; in the future this might change (and it looks like it will) but, until then, you shouldn't depend on it.

从python 3.7开始/ em>,现在可以保证以前的版本可以保留实现中的顺序,因为 dict 插入顺序现在是一种语言功能。

As of Python 3.7, the previous is now guaranteed to preserve the order across implementations as dict insertion order is now a language feature.

这篇关于将字典传递给OrderedDict有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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