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

How to sort python dictionary

藏色散人
Release: 2019-10-02 17:29:48
Original
6273 people have browsed it

How to sort python dictionary

How to sort python dictionary?

Define a dictionary type

mydict = {2: '小路', 3: 'Ling Ming', 1: 'Aaron Kwok', 4: 'Dragon Jay'}

You can print key and value separately. Take a look at the data

How to sort python dictionary

Sort by KEY, using lambda and reverse=False (positive sequence)

key and Value is output

reverse= True (reverse order)

How to sort python dictionary

Sort by value, the order of Chinese characters is not output by pinyin

How to sort python dictionary

sorted does not change the data order of the dictionary itself.

The output is a list and a tuple

You can assign A = sorted(mydict.items(),key = lambda mydict:mydict[1],reverse= False) to A, A's The order is after the change

How to sort python dictionary

Recommended: "Python Tutorial"

Notes

sorted does not change the data order of the dictionary itself

If you want to change it, you can assign it to another list variable

The above is the detailed content of How to sort python dictionary. 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