## 字典列表排序

 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. Linux下mysql安装过程

    到mysql官网下载mysql编译好的二进制安装包,在下载页面Select Platform:选项选择linux-generic,然后把页面拉到底部,64位系统下载Linux - Generic (g ...

  2. c# FTP操作类(转)

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  3. Ubuntu14.04下如何安装Python爬虫框架Scrapy

    按照官方文档的说明,安装scrapy 需要以下程序或者库: (1).Python 2.7 (2).lxml. Most linux distributions ships PRepackaged ve ...

  4. 用java读取多种文件格式的文件(pdf,pptx,ppt,doc,docx..)

    本文通过开源pdfbox和poi进行处理多种文件格式的文本读入 1.需要的jar的maven坐标: <dependency> <groupId>org.apache.pdfbo ...

  5. 三、安装远程工具xshell,使用SFTP传输文件——Linux学习笔记

    A)远程工具 学Linux没有远程工具怎么行,百度了下,发现了xshell这个东西,重点是可以免费. 链接是多简单啊 输入地址,账号就搞定了. 打命令什么的都搞定了,真的感谢这个时代,求学有路啊! 到 ...

  6. Intellij idea用快捷键自动生成序列化id

    ntellij idea用快捷键自动生成序列化id 类继承了Serializable接口之后,使用alt+enter快捷键自动创建序列化id 进入setting→inspections→seriali ...

  7. jclass和jobject的迷惑

    [译]jclass和jobject 2012-09-18 15:02:58|  分类: Android |字号 订阅   jclass和jobject的迷惑第一次使用JNI,实例引用(jobject) ...

  8. 配置java环境变量(详细)

    内容:java安装.配置java环境变量.简单编译运行(详细) 为什么配置系统环境变量好?个人理解在结尾 ############################################### ...

  9. VC++读写*.ini配置文件

    ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息.ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Ke ...

  10. 【题解】洛谷P2679 [NOIP2015TG] 子串(DP+滚动数组)

    次元传送门:洛谷P2679 思路 蒟蒻一开始并没有思路而去看了题解 我们发现对于两个字串的位置 我们只需要管他们匹配成功或者匹配失败即可 f[i][j][k] 记录当前 a[i]不论等不等于b[j] ...