详解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)
猜你喜欢
Python注释、分支结构、循环结构、伪“选择结构”用法实例分析
2021-01-15 14:45:25
![](https://img.aspxhome.com/file/2023/7/69587_0s.png)
python调用并链接MATLAB脚本详解
2021-12-11 07:16:42
Python实现的数据结构与算法之基本搜索详解
2021-07-25 03:47:44
![](https://img.aspxhome.com/file/2023/4/67934_0s.png)
MySQL数据库中CHAR与VARCHAR之争
2011-05-05 16:33:00
python实现对列表中的元素进行倒序打印
2023-03-24 01:48:43
![](https://img.aspxhome.com/file/2023/7/99327_0s.jpg)
W3C web标准概念入门
2008-05-25 15:06:00
matplotlib 曲线图 和 折线图 plt.plot()实例
2022-06-19 13:55:06
![](https://img.aspxhome.com/file/2023/4/99564_0s.jpg)
JAVA及PYTHON质数计算代码对比解析
2023-08-29 23:41:31
python函数超时自动退出的实操方法
2021-09-11 07:07:45
Numpy中的数组搜索中np.where方法详细介绍
2022-08-31 22:09:40
python tkinter实现屏保程序
2023-02-20 14:31:20
python函数指定默认值的实例讲解
2021-07-20 14:05:01
Python多线程编程之多线程加锁操作示例
2023-11-01 00:37:19
pandas实现数据合并的示例代码
2021-03-14 01:10:59
sql server update 表的问题
2009-10-04 20:32:00
Python爬虫之网络请求
2023-10-29 21:28:08
![](https://img.aspxhome.com/file/2023/2/76072_0s.png)
在python image 中安装中文字体的实现方法
2021-08-06 06:19:35
浅谈PYTHON 关于文件的操作
2022-06-07 19:54:58
Python 安装第三方库 pip install 安装慢安装不上的解决办法
2023-02-23 13:43:45
![](https://img.aspxhome.com/file/2023/7/65017_0s.png)
Python使用apscheduler模块设置定时任务的实现
2021-05-01 05:01:54