Is it possible to print in order as in list?
lis=['c','b','c','a','b','b']
collections.Counter(lis)
Counter{'b':3,'c':2,'a':1}
But what I want is:
Counter{'c':2,'b':3,'a':1}
Is it possible to print in order as in list?
lis=['c','b','c','a','b','b']
collections.Counter(lis)
Counter{'b':3,'c':2,'a':1}
But what I want is:
Counter{'c':2,'b':3,'a':1}
This could do the trick:
from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
'Counter that remembers the order elements are first encountered'
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
def __reduce__(self):
return self.__class__, (OrderedDict(self),)
lis=['c','b','c','a','b','b']
print(OrderedCounter(lis))
#OrderedCounter(OrderedDict([('c', 2), ('b', 3), ('a', 1)]))