How to sort a dictionary in python

We know that Python's built-in dictionary data type is unordered, and the corresponding value is obtained through the key. But sometimes we need to sort and output the items in the dictionary, which may be sorted according to key or value. How many ways are there to sort and output the contents of the dictionary? Here are some wonderful solutions.

Python has two sorts of data in the 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 is in-place sorting, and sorted generates a new sorted container.

1 Sort by key value

# The easiest way is to sort by key value:
def sortedDictValues1(adict): 
items = adict.items() 
items.sort()return[value for key, value in items] 
 
# Another one is sorted by key value, which seems to be faster than the previous one
def sortedDictValues2(adict): 
keys = adict.keys() 
keys.sort()return[dict[key]for key in keys] 
 
# Or sort by key value, which is said to be faster. . . And it still applies when the key is tuple
def sortedDictValues3(adict): 
keys = adict.keys() 
keys.sort()returnmap(adict.get, keys) 
 
# Get it done in one line:
[( k,di[k])for k insorted(di.keys())] 
 
# Sort using the key parameter (func) of the sorted function:
# Sort by key
print sorted(dict1.items(), key=lambda d: d[0])

2 Sort by value

# Here is a sorting based on value, first put the key and value exchange positions of the item into a list, and then according to the first value of each element of the list, that is, the original value.
Sort:
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 inrange(0,len(backitems))] 
 
# Still do it in one line:
[ v for v insorted(di.values())] 
 
# Sorting with lambda expressions is more flexible:
sorted(d.items(), lambda x, y:cmp(x[1], y[1])),Or reverse order:
sorted(d.items(), lambda x, y:cmp(x[1], y[1]), reverse=True) 
 
# Sort using the key parameter (func) of the sorted function:#Sort by value
print sorted(dict1.items(), key=lambda d: d[1])

Knowledge point expansion:

Preparation knowledge:

In python, dictionary is a built-in data type, an unordered storage structure, 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, which can be passed through d[key] Obtain the reference of the corresponding value value, but the key cannot be obtained by value.

For dictionarynary, you need to know the following points:

a. The key of the dictionary is case sensitive;

b. There can be no duplicate keys in a dictionary;

c. Dictionaries are unordered, there is no concept of element order, they are just a simple arrangement of ordered pairs.

So far, this article on how to sort the python dictionary is introduced. For more information about the sorting method of the python dictionary, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support you in the future. ZaLou.Cn!

Recommended Posts

How to sort a dictionary in python
How to write a confession program in python
How to create a Python virtual environment in Ubuntu 14.04
How to wrap in python code
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to write return in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to find the area of a circle in python
How to make a globe with Python
How to use and and or in Python
How to introduce third-party modules in Python
How to represent null values in python
How to write win programs in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to add background music in python
How to represent relative path in python
How to use the round function in python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to use code running assistant in python
Teach you how to write games in python
How to delete files and directories in python
How to install the downloaded module in python
How to perform continuous multiplication calculation in python
How to comment python code
How to create a CentOS virtual machine in VMware
How to learn python quickly
How does python call the key of a dictionary
How to uninstall python plugin
How to understand the introduction of packages in Python
Example of how to automatically download pictures in python
How to save IE as an attachment in python
How to understand python objects
How to use python tuples
Python implements FTP to upload files in a loop
Do you still know how to draw cakes in python? ? ?
python how to view webpage code
How to use hanlp in ubuntu
How to use python thread pool
How to install PHP7.4 in CentOS
How to save the python program
How to install mysql in Ubuntu 14.04
Detailed usage of dictionary in Python
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install Python on CentOS 8
How to install HDP2.6 in Centos7.2
How to solve python dict garbled
How to install mysql in Ubuntu 14.04
How to view the python module