Home > Backend Development > Python Tutorial > Sorting Python dictionary

Sorting Python dictionary

Guanhui
Release: 2020-06-19 18:19:40
forward
2651 people have browsed it

Sorting Python dictionary

We know that Python’s built-in dictionary data type is unordered, and the corresponding value is obtained through key. But sometimes we need to sort the items in the dictionary and output them, maybe according to key or value. How many methods are there to sort and output the contents of a dictionary? Here are some exciting solutions.

Python has two ways of sorting data in a container, one is the container's own sort function, and the other is the built-in sorted function.

The only difference between the sort function and the sorted function is that sort sorts in the container (in-place), and sorted generates a new sorted container.

1 Sort by Key value


#最简单的方法,这个是按照key值排序: 
def sortedDictValues1(adict): 
items = adict.items() 
items.sort() 
return [value for key, value in items] 
 
#又一个按照key值排序,貌似比上一个速度要快点 
def sortedDictValues2(adict): 
keys = adict.keys() 
keys.sort() 
return [dict[key] for key in keys] 
 
#还是按key值排序,据说更快。。。而且当key为tuple的时候照样适用 
def sortedDictValues3(adict): 
keys = adict.keys() 
keys.sort() 
return map(adict.get, keys) 
 
#一行语句搞定: 
[(k,di[k]) for k in sorted(di.keys())] 
 
#用sorted函数的key参数(func)排序: 
#按照key进行排序 
print sorted(dict1.items(), key=lambda d: d[0])
Copy after login

2 Sort by value


#来一个根据value排序的,先把item的key和value交换位置放入一个list中,再根据list每个元素的第一个值,即原来的value值,
排序: 
def sort_by_value(d): 
items=d.items() 
backitems=[[v[1],v[0]] for v in items] 
backitems.sort() 
return [ backitems[i][1] for i in range(0,len(backitems))] 
 
#还是一行搞定: 
[ v for v in sorted(di.values())] 
 
#用lambda表达式来排序,更灵活: 
sorted(d.items(), lambda x, y: cmp(x[1], y[1])), 或反序: 
sorted(d.items(), lambda x, y: cmp(x[1], y[1]), reverse=True) 
 
#用sorted函数的key参数(func)排序: # 按照value进行排序 
print sorted(dict1.items(), key=lambda d: d[1])
Copy after login

Knowledge point expansion:

Preparatory knowledge:

In python, dictionary is a built-in data type, an unordered storage structure, and each element is a key-value pair :

For example: dict = {'username': 'password', 'database': 'master'}, where 'username' and 'database' are keys, and 'password' and 'master' are values , the reference to the corresponding value value can be obtained through d[key], but the key cannot be obtained through value.

For dictionnary, you need to know the following points:

a. The dictionary key is case-sensitive;

b. There cannot be any in a dictionary. Duplicate keys;

c. Dictionaries are unordered and have no concept of element order. They are just simple arrangements of order pairs.

Recommended tutorials: "PHP" "Python Tutorial"

The above is the detailed content of Sorting Python dictionary. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
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