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的知识。