使用其构造函数初始化OrderedDict以便保留初始数据的顺序的正确方法? [英] Right way to initialize an OrderedDict using its constructor such that it retains order of initial data?

查看:150
本文介绍了使用其构造函数初始化OrderedDict以便保留初始数据的顺序的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初始化有序字典(OD)的正确方法是什么,以保留初始数据的顺序?

What's the correct way to initialize an ordered dictionary (OD) so that it retains the order of initial data?

from collections import OrderedDict

# Obviously wrong because regular dict loses order
d = OrderedDict({'b':2, 'a':1}) 

# An OD is represented by a list of tuples, so would this work?
d = OrderedDict([('b',2), ('a', 1)])

# What about using a list comprehension, will 'd' preserve the order of 'l'
l = ['b', 'a', 'c', 'aa']
d = OrderedDict([(i,i) for i in l])

问题:


  • 一个 OrderedDict 保留初始化时传递的元组列表的顺序,或元组元组或元组元素或列表等列表(第二和第三个示例

  • Will an OrderedDict preserve the order of a list of tuples, or tuple of tuples or tuple of lists or list of lists etc. passed at the time of initialization (2nd & 3rd example above)?

如何确认 OrderedDict 实际上是否维护订单?由于 dict 具有不可预测的顺序,如果我的测试向量幸运地具有与dict不可预测的顺序相同的初始顺序?例如,如果代替 d = OrderedDict({'b':2,'a':1})我写 d = OrderedDict({ 'a':1,'b':2}),我可以错误地断定订单被保留。在这种情况下,我发现一个 dict 按字母顺序排列,但可能并不总是如此。使用反例来验证数据结构是否保留顺序是一个可靠的方法,不要反复尝试测试向导,直到一个中断?

How does one go about verifying if OrderedDict actually maintains an order? Since a dict has an unpredictable order, what if my test vectors luckily have the same initial order as the unpredictable order of a dict? For example, if instead of d = OrderedDict({'b':2, 'a':1}) I write d = OrderedDict({'a':1, 'b':2}), I can wrongly conclude that the order is preserved. In this case, I found out that a dict is ordered alphabetically, but that may not be always true. What's a reliable way to use a counterexample to verify whether a data structure preserves order or not, short of trying test vectors repeatedly until one breaks?

PS我将在这里离开参考:有序的构造函数和update()方法都接受关键字参数,但是它们的顺序丢失,因为Python的函数使用常规无序字典来调用语义传入关键字参数

P.S. I'll just leave this here for reference: "The OrderedDict constructor and update() method both accept keyword arguments, but their order is lost because Python’s function call semantics pass-in keyword arguments using a regular unordered dictionary"

PPS:希望在未来,OrderedDict也将保留kwargs的顺序(示例1): http://bugs.python.org/ issue16991

P.P.S : Hopefully, in future, OrderedDict will preserve the order of kwargs also (example 1): http://bugs.python.org/issue16991

推荐答案

OrderedDict将保留其有权访问的任何订单。将订单数据传递给它进行初始化的唯一方法是传递键值对的列表(或更一般地,可迭代的),如最后两个示例所示。根据您链接的文档,OrderedDict在传递关键字参数或dict参数时无法访问任何订单,因为在OrderedDict构造函数看到之前,已删除任何订单。

The OrderedDict will preserve any order that it has access to. The only way to pass ordered data to it to initialize is to pass a list (or, more generally, an iterable) of key-value pairs, as in your last two examples. As the documentation you linked to says, the OrderedDict does not have access to any order when you pass in keyword arguments or a dict argument, since any order there is removed before the OrderedDict constructor sees it.

请注意,在上一个示例中使用列表解析不会更改任何内容。 OrderedDict([(i,i)for i in l]) OrderedDict([('b','b')) ,('a','a'),('c','c'),('aa','aa')])。列表理解被评估并创建列表,并传入; OrderedDict不知道如何创建它。

Note that using a list comprehension in your last example doesn't change anything. There's no difference between OrderedDict([(i,i) for i in l]) and OrderedDict([('b', 'b'), ('a', 'a'), ('c', 'c'), ('aa', 'aa')]). The list comprehension is evaluated and creates the list and it is passed in; OrderedDict knows nothing about how it was created.

这篇关于使用其构造函数初始化OrderedDict以便保留初始数据的顺序的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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