dictionary sorting
When you use a dictionary in a program to count data information, the dictionary is unordered, so the contents of the dictionary are also unordered when you print it. Therefore, in order to make the results of the statistics more convenient to view, you need to sort the dictionary in Python, which is divided into sorting by "key" and sorting by "value".
Sort by "Value"
Sorting by "value" means sorting by the value of the dictionary, using the built-in sort() function.
sorted(iterable[, cmp[, key[, reverse]]])
- iterable: is an iterable type type.
- cmp: function for comparing, what to compare is determined by the key, has a default value, iterates over an item in the set; cmp: function for comparing, what to compare is determined by the key, has a default value, iterates over an item in the set.
- key: use one of the attributes and functions of the list element as the keyword, with a default value, to iterate over an item in the collection;
- reverse: the sorting rule. reverse = True or reverse = False, with a default value of ascending (False).
Return value: is a sorted iterable type, like iterable. In general, cmp and key can use lambda expressions.
If the dictionary is sorted, the common form is as follows:
sorted((), key=lambda e:e[1], reverse=True)
where e denotes an element in (), e[0] denotes sorting by key, and e[1] denotes sorting by value. reverse=False can be omitted, and the default is ascending.
Note: The items() function of a dictionary returns a list, where each element of the list is a tuple of keys and values. Therefore, the value returned by sorted((), key=lambda e:e[1], reverse=True) is also a list of tuples.
Example:
x=[4,6,2,1,7,9,4] y=x[:] () print x print y
The output is as follows:
[4, 6, 2, 1, 7, 9, 4]
[1, 2, 4, 4, 6, 7, 9]
Note: Calling x[:] gives you a slice that contains all the elements of x, which is a very efficient way to copy the entire list. Simply copying x to y via y=x is not useful, because that makes both x and y point to the same list.
Sort by "Key"
Sorting the dictionary by key can also be done using the sorted function above, just change it to sort((), key=lambda e:e[0], reverse=True). But there's another way to do it besides this one: by sorting the list to achieve the goal of making the dictionary ordered.
Knowledge used: the dictionary keys() function returns a list of keys, the list can be sorted. Sort the list using the list of sort() function, refer to the details of the list sort.
Summary:
Method 1: The simplest method, arranging the elements (key/value pairs) and then picking out the values. The dictionary's items method, returns a list of tuples, where each tuple contains a pair of items - the key and the corresponding value. Sorting at this point can be done with the sort() method.
def sortedDictValues1(adict): items = () () return [value for key, value in items]
Method 2: picks out values using an arrangement of keys (key), which is faster than method 1. The keys() method of the dictionary object returns a list of all the keys in the dictionary in random order. When you need to sort, just use the sort() method on the returned list of keys.
def sortedDictValues1(adict): keys = () () return [adict[key] for key in keys]
Method 3: More efficient implementation of the last step through mapping.
def sortedDictValues1(adict): keys = () () return map (,keys )
Method 4: Sort the dictionary by keystrokes and return it as a list of tuples, while using the lambda function to do so;
sorted(iterable[, cmp[, key[, reverse]]] cmpcap (a poem)keyGeneral uselambda
Example:
>>> d={"ok":1,"no":2} # Sort the dictionary by key and return it as a tuple list >>> sorted((), key=lambda d:d[0]) [('no', 2), ('ok', 1)] # Sort the dictionary by value and return it as a tuple list >>> sorted((), key=lambda d:d[1]) [('ok', 1), ('no', 2)]
This is the whole content of this article.