如何获得按输入顺序排序的python Counter输出? [英] How to get the python Counter output ordered by order of inputs?

查看:789
本文介绍了如何获得按输入顺序排序的python Counter输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力获取计数(频率),然后对其进行图形表示.

I have been working on getting the count(frequency) and then making the graph representation of it.

我正在使用python从collections中使用Counter类.我希望按先到对象的顺序输出Counter.

I am using Counter class from collections using python. I want the output of the Counter in the order of the first come object.

例如:

offset=['a','b','c','a','b','b','b','c','c','c','c','c']
counts = Counter(offset)
print counts

输出为:

Counter({'c': 6, 'b': 4, 'a': 2})

我希望输出如下,按照第一个出现的对象的顺序:

As I want the output to be as follows, in the order of the first come object:

Counter({'a': 2, 'b': 4, 'c': 6})

有可能吗?

谢谢

推荐答案

通过多继承(长2行)的OrderedCounter

以下精彩演讲超级被认为是超级 Raymond Hettinger我们可以使用多重继承:

OrderedCounter by (2 lines long) multiple inheritance

Following wonderful speech Super considered super by Raymond Hettinger we can use multiple inheritance:

from collections import OrderedDict, Counter

class OrderedCounter(Counter, OrderedDict):
    pass

并使用它:

>>> counter = OrderedCounter("abracadabra")
>>> for key, value in counter.items():
...    print key, value
a 5
b 2
r 2
c 1
d 1

这就是您完成任务所需要的.

This is all you need for your task.

让我们做更多的测试,看看我们得到的结果":

Let us do a bit more tests and see, what "result" we get:

>>> counter = OrderedCounter("cbaaa")
>>> counter
OrderedCounter({'a': 3, 'c': 1, 'b': 1})

哦-这看起来不对,预期顺序为"c","b","a".让我们对其进行打印键和值的测试:

Oh - this looks wrong, expected order is "c", "b", "a". Let us test it printing the keys and values:

>>> for key, value in counter.items():
...    print key, value
c 1
b 1
a 3

这看起来是正确的(这正是在您的代码中真正使用的内容).

This looks correct (and it is exactly what really counts for using in your code).

事实证明,我们创建的类只是产生了一些令人困惑的表示形式.

It turns out, that the class we have created is just producing a bit confusing representation.

此问题可以解决:

class OrderedCounter(Counter, OrderedDict):
    def __repr__(self):
        return "%s(%r)" % (self.__class__.__name__, OrderedDict(self))

以及使用时:

>>> counter = OrderedCounter("cbaaa")
>>> counter
OrderedCounter({'c': 1, 'b': 1, 'a': 1})

Raymond Hettinger演讲的完整版(增加了酸洗)

演讲中提供的完整版本正在添加另一种方法__reduce__,该方法允许腌制 正确的对象.

Full version from the Raymond Hettinger speech (added pickling)

Full version provided in the speech is adding one more method __reduce__ which allows pickling the object properly.

from collections import OrderedDict, Counter

class OrderedCounter(Counter, OrderedDict):
    """Counter that remembers the order elements are first seen"""
    def __repr__(self):
        return "%s(%r)" % (self.__class_.__name__, OrderedDict(self))

    def __reduce__(self):
        return self.__class__, (OrderedDict(self),)

无论如何,在大多数情况下,您将使用最简单版本的OrderedCounter类进行管理.

Anyway, in most cases you will manage with the simplest version of the OrderedCounter class.

这篇关于如何获得按输入顺序排序的python Counter输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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