详解Python中的分组函数groupby和itertools)
作者:爱做梦的鱼 时间:2022-08-30 15:03:18
具体代码如下所示:
from operator import itemgetter #itemgetter用来去dict中的key,省去了使用lambda函数
from itertools import groupby #itertool还包含有其他很多函数,比如将多个list联合起来。。
d1={'name':'zhangsan','age':20,'country':'China'}
d2={'name':'wangwu','age':19,'country':'USA'}
d3={'name':'lisi','age':22,'country':'JP'}
d4={'name':'zhaoliu','age':22,'country':'USA'}
d5={'name':'pengqi','age':22,'country':'USA'}
d6={'name':'lijiu','age':22,'country':'China'}
lst=[d1,d2,d3,d4,d5,d6]
#通过country进行分组:
lst.sort(key=itemgetter('country')) #需要先排序,然后才能groupby。lst排序后自身被改变
lstg = groupby(lst,itemgetter('country'))
#lstg = groupby(lst,key=lambda x:x['country']) 等同于使用itemgetter()
for key,group in lstg:
for g in group: #group是一个迭代器,包含了所有的分组列表
print key,g
返回:
China {'country': 'China', 'age': 20, 'name': 'zhangsan'}
China {'country': 'China', 'age': 22, 'name': 'lijiu'}
JP {'country': 'JP', 'age': 22, 'name': 'lisi'}
USA {'country': 'USA', 'age': 19, 'name': 'wangwu'}
USA {'country': 'USA', 'age': 22, 'name': 'zhaoliu'}
USA {'country': 'USA', 'age': 22, 'name': 'pengqi'}
print [key for key,group in lstg] #返回:['China', 'JP', 'USA']
print [(key,list(group)) for key,group in lstg]
#返回的list中包含着三个元组:
[('China', [{'country': 'China', 'age': 20, 'name': 'zhangsan'}, {'country': 'China', 'age': 22, 'name': 'lijiu'}]), ('JP', [{'country': 'JP', 'age': 22, 'name': 'lisi'}]), ('USA', [{'country': 'USA', 'age': 19, 'name': 'wangwu'}, {'country': 'USA', 'age': 22, 'name': 'zhaoliu'}, {'country': 'USA', 'age': 22, 'name': 'pengqi'}])]
print dict([(key,list(group)) for key,group in lstg])
#返回的是一个字典:
{'JP': [{'country': 'JP', 'age': 22, 'name': 'lisi'}], 'China': [{'country': 'China', 'age': 20, 'name': 'zhangsan'}, {'country': 'China', 'age': 22, 'name': 'lijiu'}], 'USA': [{'country': 'USA', 'age': 19, 'name': 'wangwu'}, {'country': 'USA', 'age': 22, 'name': 'zhaoliu'}, {'country': 'USA', 'age': 22, 'name': 'pengqi'}]}
print dict([(key,len(list(group))) for key,group in lstg])
#返回每个分组的个数:
{'JP': 1, 'China': 2, 'USA': 3}
#返回包含有2个以上元素的分组
print [key for key,group in groupby(sorted(lst,key=itemgetter('country')),itemgetter('country')) if len(list(group))>=2]
#返回:['China', 'USA']
lstg = groupby(sorted(lst,key=itemgetter('country')),key=itemgetter('country'))
lstgall=[(key,list(group)) for key,group in lstg ]
print dict(filter(lambda x:len(x[1])>2,lstgall))
#过滤出分组后的元素个数大于2个的分组,返回:
{'USA': [{'country': 'USA', 'age': 19, 'name': 'wangwu'}, {'country': 'USA', 'age': 22, 'name': 'zhaoliu'}, {'country': 'USA', 'age': 22, 'name': 'pengqi'}]}
自定义分组:
from itertools import groupby
lst=[2,8,11,25,43,6,9,29,51,66]
def gb(num):
if num <= 10:
return 'less'
elif num >=30:
return 'great'
else:
return 'middle'
print [(k,list(g))for k,g in groupby(sorted(lst),key=gb)]
返回:
[('less', [2, 6, 8, 9]), ('middle', [11, 25, 29]), ('great', [43, 51, 66])]
总结
以上所述是小编给大家介绍的Python中的分组函数groupby和itertools)网站的支持!
来源:https://www.cnblogs.com/dreamer-fish/p/5522687.html
标签:python,分组函数,itertools,groupby
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
有关Server.Mappath详细接触
2010-07-07 11:35:00
![](https://img.aspxhome.com/file/UploadPic/20107/7/2004351085047387-42s.gif)
python列表去重的二种方法
2022-06-02 05:21:41
使用OpenCV对运动员的姿势进行检测功能实现
2022-06-08 03:23:14
![](https://img.aspxhome.com/file/2023/2/68312_0s.png)
浅谈python的elementtree模块处理中文注意事项
2021-06-17 12:02:46
Python configparser模块常用方法解析
2021-07-08 17:31:11
楼层数横排比竖排好
2008-04-26 07:28:00
![](https://img.aspxhome.com/file/UploadPic/20084/26/200842673425574s.jpg)
Python虽然很火找工作为什么这么难
2022-10-29 11:33:03
![](https://img.aspxhome.com/file/2023/2/132352_0s.jpg)
Python中人脸图像特征提取方法(HOG、Dlib、CNN)简述
2021-09-16 21:38:59
![](https://img.aspxhome.com/file/2023/7/101457_0s.png)
javascript实现表格增删改操作实例详解
2024-04-29 13:24:36
python实现合并多个list及合并多个django QuerySet的方法示例
2023-11-25 06:43:13
python根据unicode判断语言类型实例代码
2021-08-22 18:24:04
Python之字典添加元素的几种方法
2022-11-13 13:37:23
Python实战之自动发送邮件的实现
2023-10-18 15:09:43
![](https://img.aspxhome.com/file/2023/0/90360_0s.png)
python使用jieba实现中文分词去停用词方法示例
2021-02-04 11:27:17
Python中Django与Echarts的结合用法图文详解
2022-02-09 23:10:29
![](https://img.aspxhome.com/file/2023/2/99452_0s.png)
python中sort和sorted排序的实例方法
2022-12-22 09:10:12
python通过伪装头部数据抵抗反爬虫的实例
2022-03-11 09:56:04
Oracle数据库安全策略分析(二)
2010-07-31 13:04:00
django admin管理工具自定义时间区间筛选器DateRangeFilter介绍
2023-03-23 04:12:20
![](https://img.aspxhome.com/file/2023/3/133223_0s.jpg)
VS 2010 Ultimate架构代码探索
2010-05-02 20:38:00
![](https://img.aspxhome.com/file/UploadPic/20105/2/201052204722882s.jpg)