8

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

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • 1
    Related question [Counter.most_common(n) how to override arbitrary ordering](https://stackoverflow.com/questions/43076195/counter-most-commonn-how-to-override-arbitrary-ordering/) – smci Sep 10 '18 at 09:42
  • Note that starting from Python 3.7, Counter is guaranteed to preserve insertion order. – rerx Nov 12 '20 at 15:08

5 Answers5

10

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

dom1310df
  • 7
  • 2
  • 9
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
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)
Hexoul
  • 161
  • 2
  • 13
1

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)]
Keatinge
  • 4,330
  • 6
  • 25
  • 44
1

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)])
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
1

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.

EbraHim
  • 2,279
  • 2
  • 16
  • 28