Everyone has used Xinhua Dictionary, so how to implement dictionary sorting using python language? Let's follow this tutorial to learn Python to implement dictionary sorting by value. Friends who need it can refer to it
The specific content is as follows:
Usesorted Sort the dictionary according to its value size
>>> record = {'a':89, 'b':86, 'c':99, 'd':100} >>> sorted(record.items(), key=lambda x:x[1]) [('b', 86), ('a', 89), ('c', 99), ('d', 100)]
sorted The first parameter must be iterable and can be a tuple, list
>>> items = [(1, 'B'), (1, 'A'), (2, 'A'), (0, 'B'), (0, 'a')] >>> sorted(items) [(0, 'B'), (0, 'a'), (1, 'A'), (1, 'B'), (2, 'A')]
Why (0, 'B') is in front of (0, 'a')?
Because the uppercase letters in the ASCII code are arranged before the lowercase letters, use the str.lower() method to change the order
>>> sorted(items, key=lambda x:(x[0], x[1].lower()))
[(0, 'a'), (0, ' B'), (1, 'A'), (1, 'B'), (2, 'A')]
The above is the detailed content of How to implement dictionary sorting in python language?. For more information, please follow other related articles on the PHP Chinese website!