4 ms·
just for fun... import itertools colors = ["brown", "red", "green", "yellow", "yellow", "brown", "brown", "black"] dict([(color, len(list(grp))) for colo
by schmidtc 11y ago
just for fun...
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.
- aldanor 11y agoOr this (not efficient but fun, and using a dict comprehension and no itertools): {a: sum(a == b for b in colors) for a in set(colors)}