Skip to content

Instantly share code, notes, and snippets.

@SegFaultAX
Created May 8, 2014 18:23
Show Gist options
  • Save SegFaultAX/3f2657c6c338691d812c to your computer and use it in GitHub Desktop.
Save SegFaultAX/3f2657c6c338691d812c to your computer and use it in GitHub Desktop.

Revisions

  1. SegFaultAX created this gist May 8, 2014.
    14 changes: 14 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    def categorize(fn, coll):
    """Like group_by, but fn returns multiple keys"""

    acc = {}
    for e in coll:
    keys = fn(e)
    for key in keys:
    if key not in acc:
    acc[key] = []
    acc[key].append(e)
    return acc

    def group_by(fn, l):
    return categorize(lambda e: [fn(e)], l)