有的情况下,只是想要简单地对list中数据,进行分组,查看,可以考虑使用groupby
代码:
# groupby需要排序后才能使用
def gb(num):
if 0 <= num < 0.5:
return '0~0.5'
elif 0.5 <= num < 1:
return '0.5~1'
elif 1 <= num < 2:
return '1~2'
elif 2 <= num < 3:
return '2~3'
elif 3 <= num < 4:
return '3~4'
else:
return '4~5'
summary_dict = [1.3357068035002726,
0.5057801653990588,
0.6080922133581622,
0.5905691758469916,
0.5652849576459099,
0.7592879753368657,
0.6826851249892935,
0.6952457829111574,
1.1777971206456959,
0.9245340151342968]
summary_dict.sort()
result = [(k, list(g)) for k, g in groupby(summary_dict, key=gb)]
结果:
原文地址:https://www.cnblogs.com/shiwanghualuo/p/11723887.html
时间: 2024-10-12 09:55:15