其他分享
首页 > 其他分享> > #每日一读 数组归类,并返回数组中每个元素的个数

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

作者:互联网

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}
 

 

标签:count,defaultdict,val,floor,lists,归类,数组,import,一读
来源: https://www.cnblogs.com/ai594ai/p/15649144.html