#每日一读 根据给定的函数对列表的元素进行分组
作者:互联网
from collections import defaultdict def group_by(lst, fn): d = defaultdict(list) for el in lst: d[fn(el)].append(el) return dict(d) from math import floor group_by([6.1, 4.2, 6.3], floor) # {4: [4.2], 6: [6.1, 6.3]} group_by(['one', 'two', 'three'], len) # {3: ['one', 'two'], 5: ['three']}
标签:el,group,three,6.3,lst,给定,分组,6.1,一读 来源: https://www.cnblogs.com/ai594ai/p/15654320.html