Skip to content

Instantly share code, notes, and snippets.

@amalloy
Forked from SegFaultAX/gist:3f2657c6c338691d812c
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalloy/2bd94b4bd6b4dc0796ec to your computer and use it in GitHub Desktop.
Save amalloy/2bd94b4bd6b4dc0796ec to your computer and use it in GitHub Desktop.
def categorize(fn, coll):
"""Like group_by, but fn returns multiple keys"""
acc = {}
for e in coll:
for key in fn(e):
acc.setdefault(key, []).append(e)
return acc
def group_by(fn, l):
return categorize(lambda e: [fn(e)], l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment