## 字典列表排序

 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. layui 设计资源——2.0 版本的 Axure 组件包,产品交互设计利器

    大家好,很久不见,这次为大家分享的是 layui_2.0版本的axure组件包,在去年发布的 layui Axure 1.0 中(见:http://fly.layui.com/jie/9842/ )赢 ...

  2. %02hhX

    大家经常会遇到将 调试信息例如从网络收到的数据包 或者 转换后的数据 打印出来,调试问题. 如果以ascii码打印的话,控制字符和ascii码以外的字符不能很好的查看具体值(看不到,或者乱码,尤其对于 ...

  3. INDEX SKIP SCAN适用场景

    --请记住这个INDEX SKIP SCAN扫描方式 drop table t purge;create table t as select * from dba_objects;update t s ...

  4. 使用Vaadin的maven archetype创建一个空Vaadin项目

    所在公司要求使用这个臭屎粑粑一样的Vaadin,我也没办法.为了更好地开展工作,对得起老板发给我的工资,就算是臭屎粑粑,也要尽力给他玩儿出花样来. Vaadin针对Eclipse和Netbeans等I ...

  5. copy "xxxxx\xx.dll xxxxxxx\ ” 已退出,代码为1 错误解决方法

    右键=>属性=>生成事件里面,查看预先生成事件命令行和后期生成事件命令行,查看复制的Dll是否存在已经路径是否正确

  6. c++ auto_ptr超简易版实现

    namespace wang{ template<class T> class shared_ptr{ public: explicit shared_ptr(T *p) : count( ...

  7. TypeScript----类

    一.类的属性 public: 公有, private: 私有,不能在声明它的类的外部访问,只能在类内部访问 protect: 保护,不能在声明它的类的外部访问,但继承者除外 readonly 只读属性 ...

  8. 元素设置float属性后,其后面的元素的位置问题

    分两种情况: .content{ width:350px; height:150px; color:#fff; } .content1,.content2{ background-color: #00 ...

  9. js实现抛物线运动 兼容IE低版本(转)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  10. 关闭 XXXXX 前你必须关闭所有会话框

    这个问题应该是和Microsoft管理控制台(Microsoft Management Console,MMC)有关系 我是在使用 任务计划程序 时碰到这个问题的,网上也有其他人在使用 事件查看器 的 ...