Counter for items in lists in lists?
Michael Hoffman
m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Sat Sep 25 05:33:37 EDT 2004
If you want to be able to use any iterable, you can use itertools.chain().
>>> import itertools
>>> myList=[['a','b','c','d'],['a','f','g','h'],['a','b','x','y']]
>>> counts = {}
>>> for item in itertools.chain(*myList):
... counts[item] = counts.get(item, 0) + 1
...
>>> counts
{'a': 3, 'c': 1, 'b': 2, 'd': 1, 'g': 1, 'f': 1, 'h': 1, 'y': 1, 'x': 1}
--
Michael Hoffman
More information about the Python-list
mailing list