by schmidtc · 11y ago · 0 comments
import itertools
colors = ["brown", "red", "green", "yellow", "yellow", "brown", "brown", "black"]
dict([(color, len(list(grp))) for color, grp in itertools.groupby(sorted(colors))])
or dict([(color, len(filter(lambda c: c==color, colors))) for color in set(colors)])
...because sometimes job security is important too. {a: sum(a == b for b in colors) for a in set(colors)}