简体   繁体   English

OrderedDict(Counter(s)) 它会一直给有序字典吗?

[英]OrderedDict(Counter(s)) will it give ordered dictionay always?

I have code like the following, I want to get the order in which the keys are inserted我有如下代码,我想获取插入键的顺序

from collections import Counter, OrderedDict
s = 'abcdabcd'
d = OrderedDict(Counter(s))
print(d)

output: OrderedDict([('a', 2), ('b', 2), ('c', 2), ('d', 2)]) output: OrderedDict([('a', 2), ('b', 2), ('c', 2), ('d', 2)])

Is the right way to do that, can we get same order for every time run the code?这样做是正确的方法吗,我们每次运行代码都能得到相同的顺序吗?

Yes, if you repeatedly run your code, it'll produce the same order provided you use Python 3.6 or newer .是的,如果您重复运行代码,只要您使用 Python 3.6 或更新版本,它将产生相同的顺序。

That's because iterating over Counter() objects will always give you the items in insertion order, and that order is determined by the input.那是因为遍历Counter()对象将始终按插入顺序为您提供项目,并且该顺序由输入确定。 If the s input doesn't change, neither will the output of OrderedDict(Counter(s)) .如果s输入没有改变, OrderedDict(Counter(s))的 output 也不会改变。

Note that this is the insertion order of keys, not the order produced by Counter.most_common() ;请注意,这是键的插入顺序,而不是Counter.most_common()产生的顺序; the items are not sorted when passed from Counter() to OrderedDict .Counter()传递到OrderedDict时,项目未排序。 The order remains stable because as of Python 3.6, the Python implementation of dictionaries was changed to use less memory, and that in turn had the side effect of the insertion order being recorded.订单保持稳定,因为从 Python 3.6 开始,字典的 Python 实现已更改为使用较少的 memory,并记录了插入的副作用。 As of Python 3.7, maintaining insertion order in dictionaries became part of the language specification.从 Python 3.7 开始,维护字典中的插入顺序成为语言规范的一部分。 See How to keep keys/values in same order as declared?请参阅如何保持键/值与声明的顺序相同?

So keys are listed in order of first appearance:因此键按首次出现的顺序列出:

>>> from collections import Counter, OrderedDict
>>> OrderedDict(Counter("aaabbc"))
OrderedDict([('a', 3), ('b', 2), ('c', 1)])
>>> OrderedDict(Counter("abbccc"))
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> OrderedDict(Counter("cba"))
OrderedDict([('c', 1), ('b', 1), ('a', 1)])

If you want to capture a different ordering, you need to sort the items before passing them to the OrderedDict() object.如果要捕获不同的排序,则需要先对项目进行排序,然后再将它们传递给OrderedDict() object。 Eg for most common ordering (highest count first), pass in the result of Counter.most_common() :例如,对于最常见的排序(最高计数优先),传入Counter.most_common()的结果:

>>> s = "abbcaaacbbbbc"
>>> OrderedDict(Counter(s).most_common())
OrderedDict([('b', 6), ('a', 4), ('c', 3)])

If people are wondering why you still might use an OrderedDict : with this type you can re-order keys (via OrderedDict.move_to_end() ).如果人们想知道为什么您仍然可以使用OrderedDict :使用这种类型,您可以重新排序键(通过OrderedDict.move_to_end() )。 You can also use reversed() on an ordered dict (as well as its dictionary views).您还可以在有序字典(以及它的字典视图)上使用reversed() )。

(Note: I got this wrong on first draft, after verification I adjusted this answer). (注意:我在初稿中弄错了,在验证后我调整了这个答案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM