operator.itemgetter() 字典列表排序
## 字典列表排序
students = [
{"name": "Stanley", "age": 22, "score": 92},
{"name": "Peter", "age": 19, "score": 99},
{"name": "Well", "age": 23, "score": 82},
{"name": "Bob", "age": 20, "score": 88},
{"name": "Lily", "age": 22, "score": 95}
]
students_by_age = sorted(students, key=lambda s: s["age"])
print(students_by_age)
"""
[{'name': 'Peter', 'age': 19, 'score': 99},
{'name': 'Bob', 'age': 20, 'score': 88},
{'name': 'Stanley', 'age': 22, 'score': 92},
{'name': 'Lily', 'age': 22, 'score': 95},
{'name': 'Well', 'age': 23, 'score': 82}]
""" # 使用operator.itemgetter 进行排序,代替sorted函数中的key的匿名函数
# 排序速度比匿名函数更快,也支持多个关键字同时排序,同样适用与min(),max()等函数中的key关键字 from operator import itemgetter students_by_score_age = sorted(students, key=itemgetter("score", "age"), reverse=True)
print(students_by_score_age)
"""
[{'name': 'Peter', 'age': 19, 'score': 99},
{'name': 'Lily', 'age': 22, 'score': 95},
{'name': 'Stanley', 'age': 22, 'score': 92},
{'name': 'Bob', 'age': 20, 'score': 88},
{'name': 'Well', 'age': 23, 'score': 82}]
"""
参考资料:
Python Cookbook, 3rd edition, by David Beazley and Brian K. Jones (O’Reilly).
operator.itemgetter() 字典列表排序的更多相关文章
- python 字典列表排序operator.itemgetter()
举例: import operator x = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] sorted_x = sorted(x, ...
- 【python cookbook】【数据结构与算法】13.通过公共键对字典列表排序
问题:想根据一个或多个字典中的值来对列表排序 解决方案:利用operator模块中的itemgetter()函数对这类结构进行排序是非常简单的. # Sort a list of a dicts on ...
- 【python】时间戳、字典列表排序
记录一下昨天学到的知识: 一.文件相关 文件追加:f = open("fname","a") 文件不存在时创建 二.时间戳相关 http://www.jb ...
- Python字典按值排序、包含字典的列表按字典值排序的方法
operator.itemgetter函数 operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),要注意,operator.i ...
- python之itemgetter函数:对字典列表进行多键排序
itemgetter函数:对字典列表进行多键排序 from operator import itemgetter list_people = [ {'name': 'Mike', 'age': 22, ...
- Python中列表(list)、字典(dict)排序的程序
Python3 中的排序,在 Sorting HOW TO 中已经讲得很清楚了.来个实际的例子,对下面的这个 list 依据创建时间排序: pages = [{'title': '十年学会程序设计', ...
- python中列表排序,字典排序,列表中的字典排序
#-*- encoding=utf-8 -*- # python3代码 import operator 一. 按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} 1 ...
- Python 字典 列表 嵌套 复杂排序大全
https://blog.csdn.net/ray_up/article/details/42084863 一: 字典排序 解析: 使用sorted 方法, 排序后的结果为一个元组. 可以字符串排序( ...
- python 字典排序,列表排序详细
在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序.Python中字典的排序分为按“键”排序和按“值”排序. 1.按“值 ...
随机推荐
- linux第一个C语言和sh脚本
linux第一个C语言 $ gedit hello_world.c #include <stdio.h> int main(void) { printf("hello world ...
- 【转】winrar命令行详解
从命令行也可以运行 WinRAR 命令,常规的命令行语法描述如下: WinRAR <命令> -<开关1> -<开关N> <压缩文件> <文件.. ...
- 【Leetcode】【Medium】Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- linux命令整理
Linux系统命令 1. ls 查看某个 目录下 所有文件的大小总和 ls -lR| awk 'BEGIN{size=0;} /^[-l]/{size+=$5;print $0;} END{print ...
- 设计模式:观察者(Observer)模式
设计模式:观察者(Observer)模式 一.前言 观察者模式其实最好的名称应该是“发布订阅”模式,和我们现在大数据之中的发布订阅方式比较类似,但是也有区别的地方,在上一个设计模式,我们学习的是仲 ...
- 如何玩转Android远控(androrat)
关于WebView中接口隐患与手机挂马利用的引深 看我是怎样改造Android远程控制工具AndroRat 1.修改布局界面 2.配置默认远程ip和端口 3.LauncherActivity修改为运行 ...
- mongodb启动和关闭
mongodb的启动 mongod --dbpath=/data/mongodb/data --logpath=/data/mongodb/log/33988.log --port 33988 --f ...
- Ubuntu16.04死机解决方案
内核下载地址:http://kernel.ubuntu.com/~kernel-ppa/mainline/ Ubuntu 16.04 LTS,电源设置里面的休眠/挂起/睡眠功能会使电脑会进入死机状态, ...
- 类图(Rose) - Windows XP经典软件系列
版权声明:本文为xiaobin原创文章.未经博主同意不得转载. https://blog.csdn.net/xiaobin_HLJ80/article/details/24584625 ...
- C/C++心得-理解指针
上一篇笔者用自己那不是怎么好理解的逻辑介绍了内存和C中的基本数据类型,现在笔者再根据自己重新所学来说说C语言中的指针. 理解指针才能真正的算C语言入门.也许是我大学期间太关注前端UE,也许是当初开始学 ...