## 字典列表排序

 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() 字典列表排序的更多相关文章

  1. python 字典列表排序operator.itemgetter()

    举例: import operator x = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] sorted_x = sorted(x, ...

  2. 【python cookbook】【数据结构与算法】13.通过公共键对字典列表排序

    问题:想根据一个或多个字典中的值来对列表排序 解决方案:利用operator模块中的itemgetter()函数对这类结构进行排序是非常简单的. # Sort a list of a dicts on ...

  3. 【python】时间戳、字典列表排序

    记录一下昨天学到的知识: 一.文件相关 文件追加:f = open("fname","a")    文件不存在时创建 二.时间戳相关 http://www.jb ...

  4. Python字典按值排序、包含字典的列表按字典值排序的方法

    operator.itemgetter函数 operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),要注意,operator.i ...

  5. python之itemgetter函数:对字典列表进行多键排序

    itemgetter函数:对字典列表进行多键排序 from operator import itemgetter list_people = [ {'name': 'Mike', 'age': 22, ...

  6. Python中列表(list)、字典(dict)排序的程序

    Python3 中的排序,在 Sorting HOW TO 中已经讲得很清楚了.来个实际的例子,对下面的这个 list 依据创建时间排序: pages = [{'title': '十年学会程序设计', ...

  7. python中列表排序,字典排序,列表中的字典排序

    #-*- encoding=utf-8 -*- # python3代码 import operator 一. 按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} 1 ...

  8. Python 字典 列表 嵌套 复杂排序大全

    https://blog.csdn.net/ray_up/article/details/42084863 一: 字典排序 解析: 使用sorted 方法, 排序后的结果为一个元组. 可以字符串排序( ...

  9. python 字典排序,列表排序详细

    在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序.Python中字典的排序分为按“键”排序和按“值”排序. 1.按“值 ...

随机推荐

  1. 【转】Spark on Yarn遇到的几个问题

    本文转自 http://www.cnblogs.com/Scott007/p/3889959.html 1 概述 Spark的on Yarn模式,其资源分配是交给Yarn的ResourceManage ...

  2. eclipse安装checkStyle

    今天用eclipse mars 安装checkstyle 代码测试工具,安装完后重启竟然没有,最后发现原来是 自己安装的步骤错了,记录下. 1. 我的版本是:Version: Mars.2 Relea ...

  3. apache2 tomat https 变成http的解决方案

    1.apache2   设置 RequestHeader set X-Forwarded-Proto "https" 2.springboot 设置 server.tomcat.p ...

  4. Android学习——Service(一)

    这篇博文来介绍Android另一个十分重要的组件,Service.Service和Activity很类似,区别在于它运行在后台,不可见且没有界面.Service的优先级高于Activity,当系统负载 ...

  5. Detecting Client Connection in WCF Long Running Service (Heartbeat Implementation) z

    Download source - 45.3 KB Introduction Hello everyone! This is my first blog on WCF and I hope that ...

  6. php大文件上传失败的原因及解决方法

    为什么上传大文件总是失败,上传小文件就没有问题.关于PHP大文件上传失败的原因及解决方法如下: 第1种情况:文件上传时存放文件的临时目录必须是开启的并且是 PHP 进程所有者用户可写的目录.如果未指定 ...

  7. 【深入理解JAVA虚拟机】第二部分.内存自动管理机制.4.JVM工具

    1.概述 工具作用:性能监控与故障处理 工作原理:分析数据 数据包含:运行日志. 异常堆栈. GC日志. 线程快照(threaddump/javacore文件). 堆转储快照(heapdump/hpr ...

  8. 分布式链路跟踪系统架构SkyWalking和zipkin和pinpoint

    Net和Java基于zipkin的全链路追踪 https://www.cnblogs.com/zhangs1986/p/8966051.html 在各大厂分布式链路跟踪系统架构对比 中已经介绍了几大框 ...

  9. c++11 实现RAII特性

    参考文章https://blog.csdn.net/pongba/article/details/7911997 什么是RAII 技术?(参见百度百科相关条目) RAII(Resource Acqui ...

  10. JavaScript:回调模式(Callback Pattern) (转载)

    JavaScript:回调模式(Callback Pattern) 函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode() ...