Python中的分组函数(groupby、itertools)
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)的更多相关文章
- python --- Python中的callable 函数
python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...
- python中使用zip函数出现<zip object at 0x02A9E418>
在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...
- [转载]python中multiprocessing.pool函数介绍
原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...
- Python 中的isinstance函数
解释: Python 中的isinstance函数,isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是cla ...
- Python中的map()函数和reduce()函数的用法
Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下 Py ...
- python中multiprocessing.pool函数介绍_正在拉磨_新浪博客
python中multiprocessing.pool函数介绍_正在拉磨_新浪博客 python中multiprocessing.pool函数介绍 (2010-06-10 03:46:5 ...
- 举例详解Python中的split()函数的使用方法
这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下 函数:sp ...
- python中的生成器函数是如何工作的?
以下内容基于python3.4 1. python中的普通函数是怎么运行的? 当一个python函数在执行时,它会在相应的python栈帧上运行,栈帧表示程序运行时函数调用栈中的某一帧.想要获得某个函 ...
- python中的map()函数
MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的map()函数来学习一下. 文档中的介绍在这里: map(function, iterable, .. ...
随机推荐
- Ajax初始接触
演示JS对象的属性,方法和事件的使用 (1)window.location.href (2)form.submit() <form action="" method=&quo ...
- Nginx缓存配置
访问我的博客 前言 本文介绍利用 nginx 的 nginx_ngx_cache_purge 模块来实现缓存功能,前几篇文章介绍了 Nginx 的动静分离以及 CDN 技术,在其基础上,再对整个页面进 ...
- 编写无Java脚本的JSP页面
在上一章中总结了Web开发中应用MVC架构模式,将Servlet 用做控制器,JSP作为视图,JavaBean作为模型,实现业务流程控制,页面逻辑和业务逻辑的分离.然而,使用前面的技术实现MVC,并不 ...
- (转)mysql升级5.5.20时遇到的问题:1548-Cannot load from mysql.proc. The table is probably corrupted
LINUX下将mysql从5.1升级至5.5后,发现存储过程不能用了.创建和使用存储过程时就会提示Cannot load from mysql.proc. The table is probably ...
- 使用whiptail开发linux环境交互式对话框
#!/bin/bash oem=$(/bin/cat /opt/jdwa/etc/oem) systype=$(/bin/cat /opt/jdwa/etc/systype) export selec ...
- FFmpeg简易播放器的实现-最简版
本文为作者原创:https://www.cnblogs.com/leisure_chn/p/10040202.html,转载请注明出处 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- [转]Magento刷新索引的几种方法
本文转自:https://blog.csdn.net/IT_Wallace/article/details/78513951 在数据表中经常会使用索引,下面简单介绍一下索引的利弊: 创建索引可以大大提 ...
- ASP.NET MVC显示异常信息
开发ASP.NET多了,它的异常信息显示也习惯了.但在ASP.NET MVC中,却是另外一番情形. 以前只习惯使用IE浏览器,现在开发ASP.NET MVC程序,为了捕获到异常信息,Firefox的f ...
- mongodb远程连接访问
随着云计算,云服务的不断发展演进,数据库的管理及维护方式也在转变,传统基于C/S客户端工具管理的方式,已经无法满足实际需要. TreeSoft数据库管理系统,采用web方式,对mongoDB,MySQ ...
- Android-消息处理学习总结(Handler,Looper)
参考资料: http://www.cnblogs.com/qlky/p/5657924.html http://blog.csdn.net/guolin_blog/article/details/99 ...