如何在 Python 中根据字典的值对字典列表进行排序?
在本文中,我们将展示如何在 Python 中根据字典的值对字典列表进行排序。
排序一直是日常编程中一项有用的技术。Python 的字典经常用于各种应用中,从竞赛到面向开发人员的应用(例如处理 JSON 数据)。在某些情况下,能够根据字典的值过滤字典会很有用。
以下是完成此任务的两种方法:
使用 sorted() 和 itemgetter
使用 sorted() 和 lambda 函数
什么是字典?
字典是 Python 中关联数组数据结构的版本。字典是键值对的集合。每个键值对由一个键和与其关联的值表示。
字典由用花括号括起来并用逗号分隔的键值对列表定义。每个键的值由冒号(:)分隔。
字典是一个有序的数据值集合,可以使用键访问。字典是键值映射。dict() 是用于创建 dict 类实例的构造函数。字典中的元素顺序是未定义的。可以迭代键、值或键值对。
使用 sorted() 和 Itemgetter
sorted() 函数
sorted() 函数返回给定可迭代对象的排序列表。
您可以在升序和降序之间进行选择。数字按数字排序,而字符串按字母顺序排列。
语法
sorted(iterable, key=key, reverse=reverse)
参数
iterable - 它是一个序列。
key - 将执行以确定顺序的函数。默认值为 None。
reverse - 布尔表达式。True 表示升序排序,False 表示降序排序。默认值为 False。
Python 中的 Itemgetter
为了实现类似的功能,可以使用 Itemgetter 函数代替 lambda 函数。与 sorted() 和 lambda 相同,但具有不同的内部实现。它接受字典键并将它们转换为元组。它最大限度地减少了开销,同时速度更快、效率更高。
要使用itemgetter,必须导入“operator”模块。
性能 - 在性能方面,itemgetter 在时间方面优于 lambda 函数。
简洁 - 在访问多个值时,itemgetter 看起来比 lambda 函数更简单。
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤:
使用 import 关键字从 operator 模块导入itemgetter以实现 itemgetter。
创建一个变量来存储输入字典列表。
使用sorted()函数(返回给定可迭代对象的排序列表)和 itemgetter 打印按分数排序的列表。
示例
以下程序使用 sorted() 和 itemgetter 返回按值排序的输入字典列表:
# importing itemgetter from the operator module
# (for implementing itemgetter)
from operator import itemgetter
# Input list of dictionaries
inputList = [{"cricketer": "Dhoni", "score": 75},{"cricketer": "Virat Kohli", "score": 90},
{"cricketer": "Hardik Pandya", "score": 65}]
print("The sorted list by score: ")
# printing the sorted list by score using the sorted() and itemgetter
print(sorted(inputList, key=itemgetter('score')))
print("\r")
# printing the sorted list by both cricketer, score using the sorted() and itemgetter
# Here Dhoni comes first since 'D' comes first then H, and V alphabetically
print("The sorted list by both cricketer and score: ")
print(sorted(inputList, key=itemgetter('cricketer', 'score')))
print("\r")
# printing the sorted list by the score in descending order
# using the sorted() and itemgetter
print("The sorted list by the score in descending order: ")
print(sorted(inputList, key=itemgetter('score'), reverse=True))
输出
执行上述程序后,将生成以下输出:
The sorted list by score:
[{'cricketer': 'Hardik Pandya', 'score': 65}, {'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Virat Kohli', 'score': 90}]
The sorted list by both cricketer and score:
[{'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Hardik Pandya', 'score': 65}, {'cricketer': 'Virat Kohli', 'score': 90}]
The sorted list by the score in descending order:
[{'cricketer': 'Virat Kohli', 'score': 90}, {'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Hardik Pandya', 'score': 65}]
使用 sorted() 和 lambda 函数
示例
以下程序使用 sorted() 和 lambda() 函数返回按值排序的输入字典列表:
# Input list of dictionaries
inputList = [{"cricketer": "Dhoni", "score": 75},{"cricketer": "Virat Kohli", "score": 90},
{"cricketer": "Hardik Pandya", "score": 65}]
print("The sorted list by score: ")
# printing the sorted list by score using the sorted() and lambda functions
print(sorted(inputList, key=lambda k: k['score']))
print("\r")
# printing the sorted list by both cricketer, and score using the sorted() and lambda functions
# Here Dhoni comes first since 'D' comes first then H, and V alphabetically
print("The sorted list by both cricketer and score: ")
print(sorted(inputList, key=lambda k: (k['cricketer'], k['score'])))
print("\r")
# printing the sorted list by the score in descending order
# using the sorted() and lambda functions
print("The sorted list by the score in descending order: ")
print(sorted(inputList, key=lambda k: k['score'], reverse=True))
输出
执行上述程序后,将生成以下输出:
The sorted list by score:
[{'cricketer': 'Hardik Pandya', 'score': 65}, {'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Virat Kohli', 'score': 90}]
The sorted list by both cricketer and score:
[{'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Hardik Pandya', 'score': 65}, {'cricketer': 'Virat Kohli', 'score': 90}]
The sorted list by the score in descending order:
[{'cricketer': 'Virat Kohli', 'score': 90}, {'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Hardik Pandya', 'score': 65}]
结论
在这篇文章中,我们学习了如何使用两种不同的方法根据值对字典列表进行排序。在本文中,我们还学习了 lambda 函数和 item getter。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP