如何在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函数和itemgetter。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP