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

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}
 

 

上一篇:028.PGSQL-用户创建、表空间创建、数据库创建


下一篇:sql数值计算函数ceil(x)、sign(X)、sqrt(X)、truncate(X,D)、floor(x)、pi()、mod(x,y)