python如何根据给定的KEY分组
发表于:2025-11-21 作者:千家信息网编辑
千家信息网最后更新 2025年11月21日,小编给大家分享一下python如何根据给定的KEY分组,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!根据给定的KEY分组(
千家信息网最后更新 2025年11月21日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安全错误
数据库的锁怎样保障安全
数据库表列名为数字
杭州车云网络技术有限公司
永磁电机控制器软件开发
数据库table英语解析
交警网络安全8大项自查报告
大学生网络安全第一
页面运行和数据库有关系吗
网络技术有限公司指的是
女图片数据库
上海金掌网络技术有限责任公司
中小型互联网企业域名服务器
网络安全实战攻防总结
电竞网络安全赛
公网服务器是怎么远程管理的
网络安全周有哪些主题日活动
性能 软件开发
网络安全手抄报超简单字少
云南服务器电源排行榜
文明与征服怎么进入第一服务器
word中数据库设计在哪
服务器比较好的游戏对战平台
如何修改服务器的管理地址
河北超级服务器云空间虚拟主机
海康综合管理服务器地址
漳州章鱼猫网络技术有限公司
sql数据库可疑解决办法
有关网络安全的英语博文
数据库如何显示某用户表数据
5g核心网络安全
vba打印最后一行数据库