字典(dict)是Python中最常用的数据结构之一,但默认情况下字典是无序的(Python 3.7之前)。本文将全面介绍Python中各种字典排序的方法和技巧,帮助你高效地处理字典排序需求。
一、Python字典排序基础
1. Python 3.7+的字典有序性
从Python 3.7开始,字典保持插入顺序,这是语言规范的正式特性。但要注意,这不等同于”排序”,只是保留了元素插入的顺序。
# Python 3.7+
d = {'b': 2, 'a': 1, 'c': 3}
print(d) # 保持插入顺序: {'b': 2, 'a': 1, 'c': 3}
2. 为什么需要排序字典
- 数据展示需要特定顺序
- 需要按照键或值进行数据处理
- 提高查找效率(排序后可以使用二分查找等算法)
二、按键排序字典
1. 使用sorted()函数排序键
my_dict = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
# 按键排序(升序)
sorted_keys = sorted(my_dict.keys())
print(sorted_keys) # ['apple', 'banana', 'orange', 'pear']
# 按键排序(降序)
sorted_keys_desc = sorted(my_dict.keys(), reverse=True)
print(sorted_keys_desc) # ['pear', 'orange', 'banana', 'apple']
2. 获取排序后的键值对列表
# 按键排序并获取键值对
sorted_items = sorted(my_dict.items())
print(sorted_items)
# [('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]
3. 创建有序字典(OrderedDict)
from collections import OrderedDict
sorted_dict = OrderedDict(sorted(my_dict.items()))
print(sorted_dict)
# OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
三、按值排序字典
1. 使用sorted()和lambda函数
# 按值排序(升序)
sorted_by_value = sorted(my_dict.items(), key=lambda item: item[1])
print(sorted_by_value)
# [('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)]
# 按值排序(降序)
sorted_by_value_desc = sorted(my_dict.items(), key=lambda item: item[1], reverse=True)
print(sorted_by_value_desc)
# [('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]
2. 使用operator.itemgetter
from operator import itemgetter
sorted_by_value = sorted(my_dict.items(), key=itemgetter(1))
print(sorted_by_value)
# [('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)]
四、复杂排序场景
1. 多条件排序
people = [
{'name': 'Alice', 'age': 25, 'score': 90},
{'name': 'Bob', 'age': 30, 'score': 85},
{'name': 'Charlie', 'age': 25, 'score': 95}
]
# 先按年龄升序,再按分数降序
sorted_people = sorted(people, key=lambda x: (x['age'], -x['score']))
print(sorted_people)
2. 自定义排序函数
def custom_sort(item):
"""自定义排序规则:先按值排序,值相同则按键排序"""
return (item[1], item[0])
my_dict = {'a': 2, 'b': 1, 'c': 2, 'd': 3}
sorted_dict = sorted(my_dict.items(), key=custom_sort)
print(sorted_dict)
# [('b', 1), ('a', 2), ('c', 2), ('d', 3)]
五、保持排序后的字典
1. 使用collections.OrderedDict
from collections import OrderedDict
# 按键排序
ordered_dict = OrderedDict(sorted(my_dict.items()))
print(ordered_dict)
# 按值排序
ordered_by_value = OrderedDict(sorted(my_dict.items(), key=lambda x: x[1]))
print(ordered_by_value)
2. Python 3.7+的普通字典
# Python 3.7+可以直接使用普通字典保持顺序
sorted_dict = dict(sorted(my_dict.items()))
print(sorted_dict)
六、性能考虑
1. 各种排序方法性能比较
import timeit
from collections import OrderedDict
setup = """
from random import randint
my_dict = {str(randint(0, 10000)): randint(0, 10000) for _ in range(10000)}
"""
# 测试sorted()+dict()
time_dict = timeit.timeit('dict(sorted(my_dict.items()))', setup=setup, number=100)
# 测试OrderedDict
time_ordered = timeit.timeit('OrderedDict(sorted(my_dict.items()))',
setup=setup + "from collections import OrderedDict",
number=100)
print(f"dict(sorted()): {time_dict:.3f}秒")
print(f"OrderedDict: {time_ordered:.3f}秒")
2. 大型字典排序优化
对于非常大的字典,考虑:
- 使用生成器表达式而非创建完整列表
- 使用堆排序(heapq)获取前N个元素
- 考虑使用pandas等专业库处理大数据
import heapq
# 获取值最大的3个键值对
largest = heapq.nlargest(3, my_dict.items(), key=lambda x: x[1])
print(largest)
七、实际应用示例
1. 统计单词频率并排序
from collections import defaultdict
text = "this is a sample text with several words this is a sample"
words = text.split()
word_count = defaultdict(int)
for word in words:
word_count[word] += 1
# 按频率降序排序
sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
print(sorted_word_count)
2. 学生成绩排序
students = {
'Alice': {'math': 85, 'english': 90},
'Bob': {'math': 78, 'english': 88},
'Charlie': {'math': 92, 'english': 85}
}
# 按数学成绩降序排序
sorted_students = sorted(students.items(),
key=lambda x: x[1]['math'],
reverse=True)
print(sorted_students)
八、常见问题解答
Q: Python 3.6中的字典有序吗?
A: Python 3.6中字典保持插入顺序是CPython的实现细节,Python 3.7才成为语言规范
Q: 如何对字典的键进行不区分大小写的排序?
A: 使用key=str.lower
参数:
sorted_keys = sorted(my_dict.keys(), key=str.lower)
Q: 排序后如何保持字典的更新顺序?
A: 每次修改后需要重新排序:
my_dict['new_key'] = 'value'
my_dict = dict(sorted(my_dict.items()))
Q: 有没有第三方库提供更好的排序字典实现?
A: 可以尝试sortedcontainers
模块的SortedDict
,它提供了高效的排序字典实现
通过本教程,你应该已经掌握了Python中字典排序的各种方法和技巧。根据你的具体需求选择合适的方法:简单排序使用sorted()函数,需要保持顺序使用OrderedDict或Python 3.7+的普通字典,大数据量考虑性能优化方案。