This commit is contained in:
Angelos Chalaris
2019-09-22 15:41:44 +03:00
committed by GitHub
parent 9b6cec690f
commit efeabbc40e

View File

@ -12,13 +12,12 @@ Iterate over the map and increase the element count each time it occurs.
def count_by(arr, fn=lambda x: x): def count_by(arr, fn=lambda x: x):
key = {} key = {}
for el in map(fn, arr): for el in map(fn, arr):
key[el] = 0 if el not in key else key[el] key[el] = 1 if el not in key else key[el] + 1
key[el] += 1
return key return key
``` ```
```py ```py
from math import floor from math import floor
count_by([6.1, 4.2, 6.3], floor) # {4: 1, 6: 2} count_by([6.1, 4.2, 6.3], floor) # {6: 2, 4: 1}
count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1} count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
``` ```