We know that Python's built-in dictionary data type is unordered, and the corresponding value is obtained through the key. But sometimes we need to sort and output the items in the dictionary, which may be sorted according to key or value. How many ways are there to sort and output the contents of the dictionary? Here are some wonderful solutions.
Python has two sorts of data in the container, one is the container's own sort function, and the other is the built-in sorted function.
The only difference between the sort function and the sorted function is that sort is in-place sorting, and sorted generates a new sorted container.
1 Sort by key value
# The easiest way is to sort by key value:
def sortedDictValues1(adict):
items = adict.items()
items.sort()return[value for key, value in items]
# Another one is sorted by key value, which seems to be faster than the previous one
def sortedDictValues2(adict):
keys = adict.keys()
keys.sort()return[dict[key]for key in keys]
# Or sort by key value, which is said to be faster. . . And it still applies when the key is tuple
def sortedDictValues3(adict):
keys = adict.keys()
keys.sort()returnmap(adict.get, keys)
# Get it done in one line:
[( k,di[k])for k insorted(di.keys())]
# Sort using the key parameter (func) of the sorted function:
# Sort by key
print sorted(dict1.items(), key=lambda d: d[0])
2 Sort by value
# Here is a sorting based on value, first put the key and value exchange positions of the item into a list, and then according to the first value of each element of the list, that is, the original value.
Sort:
def sort_by_value(d):
items=d.items()
backitems=[[v[1],v[0]]for v in items]
backitems.sort()return[ backitems[i][1]for i inrange(0,len(backitems))]
# Still do it in one line:
[ v for v insorted(di.values())]
# Sorting with lambda expressions is more flexible:
sorted(d.items(), lambda x, y:cmp(x[1], y[1])),Or reverse order:
sorted(d.items(), lambda x, y:cmp(x[1], y[1]), reverse=True)
# Sort using the key parameter (func) of the sorted function:#Sort by value
print sorted(dict1.items(), key=lambda d: d[1])
Knowledge point expansion:
Preparation knowledge:
In python, dictionary is a built-in data type, an unordered storage structure, each element is a key-value pair:
For example: dict = {'username':'password','database':'master'}, where'username' and'database' are keys, and'password' and'master' are values, which can be passed through d[key] Obtain the reference of the corresponding value value, but the key cannot be obtained by value.
For dictionarynary, you need to know the following points:
a. The key of the dictionary is case sensitive;
b. There can be no duplicate keys in a dictionary;
c. Dictionaries are unordered, there is no concept of element order, they are just a simple arrangement of ordered pairs.
So far, this article on how to sort the python dictionary is introduced. For more information about the sorting method of the python dictionary, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support you in the future. ZaLou.Cn!
Recommended Posts