Home > Backend Development > Python Tutorial > How to sort a dictionary in python

How to sort a dictionary in python

爱喝马黛茶的安东尼
Release: 2019-06-20 10:07:16
Original
15536 people have browsed it

How to sort a dictionary in python

How to sort a dictionary in python? Here are two different methods:

Method 1:

#使用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}
Copy after login

1. Sort the dictionary keys

Related recommendations: "python video tutorial"

#对字典按键(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)]
Copy after login

2. Sort the dictionary by value(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)]
Copy after login

Method 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)]
Copy after login

The above is the detailed content of How to sort a dictionary in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template