views:

117

answers:

1

I am looking for a fast and efficient way to calculate the frequency of list items in python:

list = ['a','b','a','b', ......]

I want a frequency counter which would give me an output like this:

[ ('a', 10),('b', 8) ...]

The items should be arranged in descending order of frequency as shown above.

+5  A: 

Python2.7+

>>> from collections import Counter
>>> L=['a','b','a','b']
>>> print(Counter(L))
Counter({'a': 2, 'b': 2})
>>> print(Counter(L).items())
dict_items([('a', 2), ('b', 2)])

python2.5/2.6

>>> from collections import defaultdict
>>> L=['a','b','a','b']
>>> d=defaultdict(int)
>>> for item in L:
>>>     d[item]+=1
>>>     
>>> print d
defaultdict(<type 'int'>, {'a': 2, 'b': 2})
>>> print d.items()
[('a', 2), ('b', 2)]
gnibbler
Any solution for Python 2.5? I am using this with Google App Engine
demos
Sure, you can use defaultdict. I will add to my answer
gnibbler
See http://code.activestate.com/recipes/576611/ for a 2.5 version of Counter.
sdolan
Thanks for the really quick reply. Appreciate it.
demos
`Counter` is not the most efficient way; see performance comparison http://stackoverflow.com/questions/2522152/python-is-a-dictionary-slow-to-find-frequency-of-each-character
J.F. Sebastian
[this](http://pypi.python.org/pypi/data-structures/0.1.2#bag) could be interesting too, it says counting is O(1) ...
ShinTakezou