How to get the python Counter output ordered by order of inputs?

為{幸葍}努か 提交于 2020-01-01 14:38:22

问题


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

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

for example:

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

output is:

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})

Is that possible?

Thanks


回答1:


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

and use it:

>>> 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.

Fixing a bit confusiong representation

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

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

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.

This can be fixed:

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

and when used:

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

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),)

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




回答2:


EDIT

sorry, I misunderstood. try this. I implemented that using queue concept.

q = []
ret = []
for i in offset:
  if q.count(i)==0: q.insert(i, 0)
while len(q):
  item = q.pop()
  ret.append((item, d.get(item)))
print(ret)



回答3:


Dictionaries in python are just hash tables so they don't have any sort of order. You can never print out a sorted list. But you can convert them to a list of tuples and sort that.

from collections import Counter
import operator
offset=['a','b','c','a','b','b','b','c','c','c','c','c']
counts = Counter(offset)
print(sorted(counts.items(), key=operator.itemgetter(0)))
#[('a', 2), ('b', 4), ('c', 6)]



回答4:


You will have to do it "the old fashion way" using an ordered dictionary that guarantees to retain the order in which elements are first inserted:

from collections import OrderedDict

offset = ['a','b','c','a','b','b','b','c','c','c','c','c']
counts = OrderedDict()
for elt in offset:
    try:
        counts[elt] += 1
    except KeyError:
        counts[elt] = 1

print(counts)

Result:

OrderedDict([('a', 2), ('b', 4), ('c', 6)])



回答5:


You can use such a methodology:

from collections import Counter
offset=['a','b','c','a','b','b','b','c','c','c','c','c']
counts = Counter(offset)

for letter in offset:
    if letter in counts:
        print (letter + ": " + str(counts[letter]))
        counts.pop(letter)

That works as below:

>>> ================================ RESTART ================================
>>> 
a: 2
b: 4
c: 6

You can store the pairs in a list instead of printing.



来源:https://stackoverflow.com/questions/36807701/how-to-get-the-python-counter-output-ordered-by-order-of-inputs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!