Skip to content

Instantly share code, notes, and snippets.

@miketery
Created March 1, 2018 00:57
Show Gist options
  • Save miketery/030e10d09d7f761679a9d26007f6256c to your computer and use it in GitHub Desktop.
Save miketery/030e10d09d7f761679a9d26007f6256c to your computer and use it in GitHub Desktop.
# http://30secondsofpythoncode.ml/#count_by
def count_by(arr, fn=lambda x: x):
key = {}
for el in map(fn, arr):
key[el] = 0 if el not in key else key[el]
key[el] += 1
return key
from math import floor
count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2}
count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment