Home > Article > Backend Development > 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
Sort by KEY, using lambda and reverse=False (positive sequence)
key and Value is output
reverse= True (reverse order)
Sort by value, the order of Chinese characters is not output by pinyin
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
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!