I have used the Xinhua Dictionary. How can I sort the dictionary using the python language? The following describes how to sort the dictionary by value by using Python in this tutorial. For details, refer to the following:
Sort the dictionary by value by sorted
>>> 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)]
The first sorted parameter must be iterated. It can be tuple or 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 uppercase and lowercase letters are arranged in front of each ASCII code, use str. lower () 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 content is related to the knowledge about how to sort the dictionary by value in Python. I hope it will be helpful to you!