python如何根据给定的KEY分组
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,小编给大家分享一下python如何根据给定的KEY分组,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!根据给定的KEY分组(
千家信息网最后更新 2025年02月01日python如何根据给定的KEY分组
小编给大家分享一下python如何根据给定的KEY分组,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
根据给定的KEY分组(itertools.groupby)
>>> from operator import itemgetter>>> import itertools>>> with open("contactlenses.csv", "r") as infile:... data = [line.strip().split(",") for line in infile]...>>> data = data[1:]>>> def print_data(rows):... print " ".join(" ".join("{: <16}".format(s) for s in row) for row in rows)... >>> print_data(data)young myope no reduced noneyoung myope no normal softyoung myope yes reduced noneyoung myope yes normal hardyoung hypermetrope no reduced noneyoung hypermetrope no normal softyoung hypermetrope yes reduced noneyoung hypermetrope yes normal hardpre-presbyopic myope no reduced nonepre-presbyopic myope no normal softpre-presbyopic myope yes reduced nonepre-presbyopic myope yes normal hardpre-presbyopic hypermetrope no reduced nonepre-presbyopic hypermetrope no normal softpre-presbyopic hypermetrope yes reduced nonepre-presbyopic hypermetrope yes normal nonepresbyopic myope no reduced nonepresbyopic myope no normal nonepresbyopic myope yes reduced nonepresbyopic myope yes normal hardpresbyopic hypermetrope no reduced nonepresbyopic hypermetrope no normal softpresbyopic hypermetrope yes reduced nonepresbyopic hypermetrope yes normal none >>> data.sort(key=itemgetter(-1))>>> for value, group in itertools.groupby(data, lambda r: r[-1]):... print "-----------"... print "Group: " + value... print_data(group)...-----------Group: hardyoung myope yes normal hardyoung hypermetrope yes normal hardpre-presbyopic myope yes normal hardpresbyopic myope yes normal hard-----------Group: noneyoung myope no reduced noneyoung myope yes reduced noneyoung hypermetrope no reduced noneyoung hypermetrope yes reduced nonepre-presbyopic myope no reduced nonepre-presbyopic myope yes reduced nonepre-presbyopic hypermetrope no reduced nonepre-presbyopic hypermetrope yes reduced nonepre-presbyopic hypermetrope yes normal nonepresbyopic myope no reduced nonepresbyopic myope no normal nonepresbyopic myope yes reduced nonepresbyopic hypermetrope no reduced nonepresbyopic hypermetrope yes reduced nonepresbyopic hypermetrope yes normal none-----------Group: softyoung myope no normal softyoung hypermetrope no normal softpre-presbyopic myope no normal softpre-presbyopic hypermetrope no normal softpresbyopic hypermetrope no normal soft
以上是"python如何根据给定的KEY分组"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
分组
篇文章
内容
不怎么
大部分
更多
知识
行业
资讯
资讯频道
频道
参考
学习
帮助
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
上海运营软件开发诚信合作
硚口区chrome翻译服务器
网络安全行政管理
联邦网络安全部门可以裁员吗
省公安厅网络安全宣传中心
服务器centos7安装
怀旧服副本服务器选择
应届生简历软件开发模板
网络安全在我心400字
java软件开发常用的工具
服务器启动后一直显示请求错误
中科网络安全产品
泰拉瑞亚服务器怎样关闭
热库 冷库 数据库 csdn
举例说说生活中的数据库操作
sga数据库
阿里云服务器实例 需要关闭吗
默拉克服务器调试
软件开发的商标
档案馆网络安全周实施方案
河北特色软件开发配件
软件开发学web吗
北京智慧电梯软件开发怎么样
检查服务器超级管理员用户
海淀区正规软件开发设置
澳洲移民网络安全
光明大陆连接不上服务器
怎么把数据表移到另一个数据库中
等保2.0三级必备网络安全产品
如何提高服务器的响应速度面试