Python对字典分别按键(key)和值(value)进行排序

简介: 方法一:#使用sorted函数进行排序'''sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数;其中iterable表示可以迭代的对象,例如可以是dict.items()、dict.keys()等key是一个函数,用来选取参与比较的元素,reverse则是用来指定排序是倒序还是顺序,reverse=true则是倒序,reverse=false时则是顺序,默认时reverse=false。
方法一:
# 使用 sorted 函数进行排序
'''
sorted(iterable,key,reverse) sorted 一共有 iterable,key,reverse 这三个参数 ;
其中 iterable 表示可以迭代的对象,例如可以是 dict.items() dict.keys()
key 是一个函数,用来选取参与比较的元素, reverse 则是用来指定排序是倒序还是顺序, reverse=true 则是倒序,
reverse=false 时则是顺序,默认时 reverse=false
'''

# 初始化字典
dict_data={ 6 : 9 , 10 : 5 , 3 : 11 , 8 : 2 , 7 : 6 }

1、对字典按键(key)进行排序
# 对字典按键( key )进行排序(默认由小到大)
test_data_0= sorted (dict_data. keys ())
# 输出结果
print (test_data_0) #[3, 6, 7, 8, 10]
test_data_1= sorted (dict_data. items (), key = lambda x:x[ 0 ])
# 输出结果
print (test_data_1) #[(3, 11), (6, 9), (7, 6), (8, 2), (10, 5)]

2、对字典按值(value)进行排序
# 对字典按值( value )进行排序(默认由小到大)
test_data_2= sorted (dict_data. items (), key = lambda x:x[ 1 ])
# 输出结果
print (test_data_2) #[('8', 2), ('10', 5), ('7', 6), ('6', 9), ('3', 11)]
test_data_3= sorted (dict_data. items (), key = lambda x:x[ 1 ], reverse = True )
# 输出结果
print (test_data_3) #[('3', 11), ('6', 9), ('7', 6), ('10', 5), ('8', 2)]


方法二:
import operator
# 初始化字典
dict_data={ 6 : 9 , 10 : 5 , 3 : 11 , 8 : 2 , 7 : 6 }

# 按键( key )进行排序
test_data_4= sorted (dict_data. items (), key =operator. itemgetter ( 0 ))
test_data_5= sorted (dict_data. items (), key =operator. itemgetter ( 0 ), reverse = True )
print (test_data_4) #[(3, 11), (6, 9), (7, 6), (8, 2), (10, 5)]
print (test_data_5) #[(10, 5), (8, 2), (7, 6), (6, 9), (3, 11)]

# 按值( value )进行排序
test_data_6= sorted (dict_data. items (), key =operator. itemgetter ( 1 ))
test_data_7= sorted (dict_data. items (), key =operator. itemgetter ( 1 ), reverse = True )
print (test_data_6) #[(8, 2), (10, 5), (7, 6), (6, 9), (3, 11)]
print (test_data_7) #[(3, 11), (6, 9), (7, 6), (10, 5), (8, 2)]








相关文章
|
5天前
|
存储 JSON 索引
一文让你彻底搞懂 Python 字典是怎么实现的
一文让你彻底搞懂 Python 字典是怎么实现的
29 13
|
6天前
|
存储 数据安全/隐私保护 Python
Python常用数据结构——字典的应用
Python常用数据结构——字典的应用
11 2
|
6天前
|
存储 数据安全/隐私保护 Python
Python常用数据结构—字典
Python常用数据结构—字典
|
7天前
|
数据处理 Python
python遍历文件夹所有文件按什么排序
python遍历文件夹所有文件按什么排序
WK
|
8天前
|
存储 安全 索引
如何在Python中访问字典中的值
在Python中,访问字典(Dictionary)中的值非常简单。字典是一种无序的集合,它存储了键值对(key-value pairs),其中每个键都是唯一的,并映射到一个值上。要访问字典中的值,你需要使用键作为索引。
WK
10 0
WK
|
8天前
|
存储 Python 容器
如何在Python中创建字典
在Python中,创建字典(Dictionary)是非常直观的。字典是一种可变容器模型,且可存储任意类型对象,如字符串、数字、元组等其他容器模型。在字典中,每个元素都是一个键值对(key-value pair),其中键(key)必须是唯一的,而值(value)则可以是任何数据类型。
WK
8 0
|
1天前
|
机器学习/深度学习 人工智能 数据可视化
Python比较适合哪些场景的编程?
Python比较适合哪些场景的编程?
14 7
|
6天前
|
数据挖掘 索引 Python
Python数据挖掘编程基础3
字典在数学上是一个映射,类似列表但使用自定义键而非数字索引,键在整个字典中必须唯一。可以通过直接赋值、`dict`函数或`dict.fromkeys`创建字典,并通过键访问元素。集合是一种不重复且无序的数据结构,可通过花括号或`set`函数创建,支持并集、交集、差集和对称差集等运算。
15 9
|
2天前
|
存储 数据处理 开发者
深入浅出:Python编程基础与实战技巧
【9月更文挑战第32天】本文将引导读者从零开始,掌握Python编程语言的核心概念,并通过实际代码示例深入理解。我们将逐步探索变量、数据结构、控制流、函数、类和异常处理等基本知识,并结合实用案例,如数据处理、文件操作和网络请求,提升编程技能。无论您是初学者还是有一定经验的开发者,这篇文章都能帮助您巩固基础,拓展视野。
|
1天前
|
大数据 Python
Python 高级编程:深入探索高级代码实践
本文深入探讨了Python的四大高级特性:装饰器、生成器、上下文管理器及并发与并行编程。通过装饰器,我们能够在不改动原函数的基础上增添功能;生成器允许按需生成值,优化处理大数据;上下文管理器确保资源被妥善管理和释放;多线程等技术则助力高效完成并发任务。本文通过具体代码实例详细解析这些特性的应用方法,帮助读者提升Python编程水平。
18 5
下一篇
无影云桌面