Hacker News new | ask | show | jobs
by schmidtc 3878 days 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.
1 comments

Or 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)}