将 dict 转换为 OrderedDict [英] Converting dict to OrderedDict

查看:74
本文介绍了将 dict 转换为 OrderedDict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 collections.OrderedDict 类时遇到了一些问题.我在 Raspbian(Raspberry Pi 的 Debian 发行版)上使用 Python 2.7.我正在尝试打印两个字典,以便对文本冒险进行比较(并排).顺序对于准确比较至关重要.无论我尝试什么,字典都以通常的无序方式打印.

I am having some trouble using the collections.OrderedDict class. I am using Python 2.7 on Raspbian, the Debian distro for Raspberry Pi. I am trying to print two dictionaries in order for comparison (side-by-side) for a text-adventure. The order is essential to compare accurately. No matter what I try the dictionaries print in their usual unordered way.

这是我在 RPi 上执行此操作时得到的结果:

Here's what I get when I do it on my RPi:

import collections

ship = {"NAME": "Albatross",
         "HP":50,
         "BLASTERS":13,
         "THRUSTERS":18,
         "PRICE":250}

ship = collections.OrderedDict(ship)

print ship
# OrderedDict([('PRICE', 250), ('HP', 50), ('NAME', 'Albatross'), ('BLASTERS', 13), ('THRUSTERS', 18)])

显然有些不对劲,因为它正在打印函数调用并将键和值组放入嵌套列表中...

Obviously there is something not right because it is printing the function call and putting the keys and value groups into a nested list...

这是我通过在我的 PC 上运行类似的东西得到的:

This is what I got by running something similar on my PC:

import collections

Joe = {"Age": 28, "Race": "Latino", "Job": "Nurse"}
Bob = {"Age": 25, "Race": "White", "Job": "Mechanic", "Random": "stuff"}

#Just for clarity:
Joe = collections.OrderedDict(Joe)
Bob = collections.OrderedDict(Bob)

print Joe
# OrderedDict([('Age', 28), ('Race', 'Latino'), ('Job', 'Nurse')])
print Bob
# OrderedDict([('Age', 25), ('Race', 'White'), ('Job', 'Mechanic'), ('Random', 'stuff')])

这一次,它是有序的,但它应该不会打印其他东西吧?(将其放入列表并显示函数调用.)

This time, it is in order, but it shouldn't be printing the other things though right? (The putting it into list and showing function call.)

我在哪里犯了错误?应该和 Python 的 pi 版本没有任何关系,因为它只是 Linux 版本.

Where am I making my error? It shouldn't be anything to do with the pi version of Python because it is just the Linux version.

推荐答案

您要首先创建一个字典,然后将该字典传递给一个 OrderedDict.对于 Python 版本 <3.6 (*),当你这样做的时候,顺序就不再正确了.dict 本质上是无序的.

You are creating a dictionary first, then passing that dictionary to an OrderedDict. For Python versions < 3.6 (*), by the time you do that, the ordering is no longer going to be correct. dict is inherently not ordered.

改为传入一系列元组:

ship = [("NAME", "Albatross"),
        ("HP", 50),
        ("BLASTERS", 13),
        ("THRUSTERS", 18),
        ("PRICE", 250)]
ship = collections.OrderedDict(ship)

当你打印OrderedDict 时,你看到的是它的表示,它是完全正确的.OrderedDict([('PRICE', 250), ('HP', 50), ('NAME', 'Albatross'), ('BLASTERS', 13), ('THRUSTERS', 18)]) 只是以可重现的 表示形式向您展示OrderedDict 的内容.

What you see when you print the OrderedDict is it's representation, and it is entirely correct. OrderedDict([('PRICE', 250), ('HP', 50), ('NAME', 'Albatross'), ('BLASTERS', 13), ('THRUSTERS', 18)]) just shows you, in a reproducable representation, what the contents are of the OrderedDict.

(*):在 CPython 3.6 实现中,dict 类型被更新为使用内存效率更高的内部结构,它具有保留插入顺序的快乐副作用,并且通过扩展,问题中显示的代码可以正常工作.从 Python 3.7 开始,Python 语言规范已更新,要求所有 Python 实现都必须遵循此行为.请参阅我的另一个答案 了解详细信息以及为什么在某些情况下您仍可能要使用 OrderedDict().

(*): In the CPython 3.6 implementation, the dict type was updated to use a more memory efficient internal structure that has the happy side effect of preserving insertion order, and by extension the code shown in the question works without issues. As of Python 3.7, the Python language specification has been updated to require that all Python implementations must follow this behaviour. See this other answer of mine for details and also why you'd still may want to use an OrderedDict() for certain cases.

这篇关于将 dict 转换为 OrderedDict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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