#每日一练 数组归类,并返回数组中每个元素的个数

from collections import defaultdict
from math import floor

def count_by(lists,  fn = lambda x: x):
    count = defaultdict(int)
    for val in map(fn, lists):
        count[val] += 1
    return dict(count)

count_by([6.1, 4.2, 6.3], floor) # {6: 2, 4: 1}
count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
 
原文地址:https://www.cnblogs.com/ai594ai/p/15649144.html