首页
技术小册
AIGC
面试刷题
技术文章
MAGENTO
云计算
视频课程
源码下载
PDF书籍
「涨薪秘籍」
登录
注册
字典的创建
插入和删除
元素操作
查询和遍历
default dict
五种方法实现字典排序
11个Python字典知识点
8个字典常用的内置函数
合并字典的7种方法
字典的高阶用法
当前位置:
首页>>
技术小册>>
Python合辑6-字典专题
小册名称:Python合辑6-字典专题
**使用 sorted() 函数** 使用 sorted() 函数对字典进行排序,将其转换为元组列表,再按照指定的键或者值进行排序。 按照键排序的示例代码 ``` d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} sorted_d = dict(sorted(d.items(), key=lambda x: x[0])) print(sorted_d) ``` 结果如下:  按照值排序的示例代码 ``` d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} sorted_d = dict(sorted(d.items(), key=lambda x: x[1])) print(sorted_d) ``` 结果如下:  **使用 itemgetter() 函数** 可以使用 operator 模块的 itemgetter() 函数对字典进行排序,将其转换为元组列表,然后按照指定的键或者值进行排序。 按照键排序的示例代码 ``` from operator import itemgetter d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} sorted_d = dict(sorted(d.items(), key=itemgetter(0))) print(sorted_d) ``` 结果如下:  按照值排序的示例代码 ``` from operator import itemgetter d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} sorted_d = dict(sorted(d.items(), key=itemgetter(1))) print(sorted_d) ``` 结果如下:  **使用 OrderedDict 类** 可以使用 collections 模块的 OrderedDict 类对字典进行排序。OrderedDict 是一个有序的字典,它保留了元素插入的顺序。 按照键排序的示例代码 ``` from collections import OrderedDict d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} sorted_d = OrderedDict(sorted(d.items(), key=lambda x: x[0])) print(sorted_d) ``` 结果如下:  按照值排序的示例代码 ``` from collections import OrderedDict d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} sorted_d = OrderedDict(sorted(d.items(), key=lambda x: x[1])) print(sorted_d) ``` 结果如下:  **使用 zip() 函数** 可以使用 zip() 函数将字典的键和值分别转换为列表,然后按照指定的键或者值进行排序,最后将排序后的键和值重新组成字典。 按照键排序的示例代码 ``` d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} keys = list(d.keys()) values = list(d.values()) sorted_keys = sorted(keys) sorted_values = [d[k] for k in sorted_keys] sorted_d = dict(zip(sorted_keys, sorted_values)) print(sorted_d) ``` 结果如下:  照值排序的示例代码 ``` d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} keys = list(d.keys()) values = list(d.values()) sorted_values = sorted(values) sorted_keys = [keys[values.index(v)] for v in sorted_values] sorted_d = dict(zip(sorted_keys, sorted_values)) print(sorted_d) ``` 结果如下:  **使用 Pandas 库** 可以使用 Pandas 库将字典转换为 DataFrame,然后按照指定的键或者值进行排序,最后将排序后的 DataFrame 转换为字典。 按照键排序的示例代码 ``` import pandas as pd d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} df = pd.DataFrame(list(d.items()), columns=['fruit', 'count']) sorted_df = df.sort_values('fruit') sorted_d = dict(zip(sorted_df['fruit'], sorted_df['count'])) print(sorted_d) ``` 结果如下:  按照值排序的示例代码 ``` import pandas as pd d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3} df = pd.DataFrame(list(d.items()), columns=['fruit', 'count']) sorted_df = df.sort_values('count') sorted_d = dict(zip(sorted_df['fruit'], sorted_df['count'])) print(sorted_d) ``` 结果如下: 
上一篇:
default dict
下一篇:
11个Python字典知识点
该分类下的相关小册推荐:
Python甚础Django与爬虫
Python合辑13-面向对象编程案例(上)
Python合辑2-字符串常用方法
Python合辑10-函数
Python合辑7-集合、列表与元组
Python面试指南
Python编程轻松进阶(二)
Python爬虫入门与实战开发(下)
Python机器学习实战
Python合辑4-130个字符串操作示例
Python与办公-玩转Word
实战Python网络爬虫