Home  >  Article  >  Backend Development  >  How to sort python dictionary

How to sort python dictionary

藏色散人
藏色散人Original
2019-09-30 11:30:056225browse

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!

Statement:
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