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.