Created
March 1, 2018 00:57
-
-
Save miketery/030e10d09d7f761679a9d26007f6256c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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