【python-列表/字典处理】使用groupby将列表元素分类
作者:
SS_c2e5 | 来源:发表于
2020-02-21 11:10 被阅读0次from itertools import groupby
def height_class(h):
if h> 180:
return "tall"
elif h<160:
return "short"
else:
return "middle"
friends = [191,144,142,170,177,188,293]
friends = sorted(friends, key = height_class)
for m, n in itertools.groupby(friends, key=height_class):
print(m)
print(list(n))
#middle
#[170, 177]
#short
#[144, 142]
#tall
#[191, 188, 293]
本文标题:【python-列表/字典处理】使用groupby将列表元素分类
本文链接:https://www.haomeiwen.com/subject/ghzqqhtx.html
网友评论