[Python] sort of dictionary skills by key (key) or value (value)

To sum up, divided into the following four cases:

  • After all the keys in accordance with the sort key, only the output ordering: I.
  • All values ​​are sorted according to the value output only sort: Scenario 2
  • All keys and corresponding key values ​​in accordance with the order, sort output simultaneously: three cases
  • Four cases: ordered by values, while the output of all sort keys and the corresponding values ​​of
After all the keys in accordance with the sort key, only the output ordering: I.

Use direct sorted()method, the default is to sort the dictionary key

>>> dict = {'b': 2, 'a': 1, 'd': 4, 'c': 3}
>>> print(sorted(dict))
['a', 'b', 'c', 'd']

Alternatively, you can use dict.keys()the key to take the dictionary, and then use the sorted()method to sort

>>> dict = {'b': 2, 'a': 1, 'd': 4, 'c': 3}
>>> print(sorted(dict.keys()))
['a', 'b', 'c', 'd']

The above method is small to large order, if it is descending, add reverse=Trueto

>>> dict = {'a': 2, 'b': 1, 'c': 4, 'd': 3}
>>> print(sorted(dict.keys(), reverse=True))
['d', 'c', 'b', 'a']
All values ​​are sorted according to the value output only sort: Scenario 2

A first dict.values()value takes the dictionary, and then use the sorted()method to sort

>>> dict = {'a': 2, 'b': 1, 'c': 4, 'd': 3}
>>> print(sorted(dict.values()))
[1, 2, 3, 4]

Similarly, if it is sorted in descending order, add reverse=Trueto

>>> dict = {'a': 2, 'b': 1, 'c': 4, 'd': 3}
>>> print(sorted(dict.values(), reverse=True))
[4, 3, 2, 1]
All keys and corresponding key values ​​in accordance with the order, sort output simultaneously: three cases

Method a: dict.items()Method get a tuple contains a dictionary of keys and values, and then use the anonymous function lambda, which i[0]represents a dictionary key

>>> dict = {'a': 2, 'b': 1, 'c': 4, 'd': 3}
>>> print(sorted(dict.items(), key=lambda i: i[0]))
[('a', 2), ('b', 1), ('c', 4), ('d', 3)]

Option two: operatormodule itemgetterfunction, which itemgetter(0)represents a dictionary key

>>> from operator import itemgetter
>>> dict = {'a': 2, 'b': 1, 'c': 4, 'd': 3}
>>> print(sorted(dict.items(), key=itemgetter(0)))
[('a', 2), ('b', 1), ('c', 4), ('d', 3)]

itemgetter What dimensional data, the parameters used to obtain the object function for a number of the serial number, are illustrated below:

>>> import operator
>>>
>>> a = [1, 2, 3, 4]
>>> b = operator.itemgetter(0)  # 定义函数b,获取对象的第0个域的值
>>> print(b(a))
1
>>> b = operator.itemgetter(0, 1) # 定义函数b,获取对象的第0个和第1个域的值
>>> print(b(a))
(1, 2)
Four cases: ordered by values, while the output of all sort keys and the corresponding values ​​of

Method One: in the case of three similar only need to i[0]be changed i[1]to

>>> dict = {'a': 2, 'b': 1, 'c': 4, 'd': 3}
>>> print(sorted(dict.items(), key=lambda i: i[1]))
[('b', 1), ('a', 2), ('d', 3), ('c', 4)]

Method two: in the case of three similar only need to itemgetter(0)be changed itemgetter(1)to

>>> from operator import itemgetter
>>> dict = {'a': 2, 'b': 1, 'c': 4, 'd': 3}
>>> print(sorted(dict.items(), key=itemgetter(0)))
[('a', 2), ('b', 1), ('c', 4), ('d', 3)]
Published 149 original articles · won praise 518 · Views 460,000 +

Guess you like

Origin blog.csdn.net/qq_36759224/article/details/104378484