Python sorts dictionaries and python sorts dictionaries.
This example describes how to sort dictionaries in python. It is a very practical technique. Share it with you for your reference.
The specific implementation method is as follows:
Import itertools thekeys = ['B', 'A', 'C'] thevalues = ['bbb ', 'aaa', 'cccccc'] d = dict (itertools. izip (thekeys, thevalues) # create the dictionary print d def sortedDictValue (adict): keys = adict. keys () keys. sort () return map (adict. get, keys) print sortedDictValue (d) import itertoolsthekeys = ['B', 'A', 'C'] thevalues = ['bbb ', 'aaa ', 'cccc'] def sortedDictValue (adict): # use a custom sorting function to obtain the dictionary keys () and sort the keys, finally, take the dictionary value keys = adict Based on the sorted keys. keys () keys. sort () return map (adict. get, keys) # here is the only difference. Here, the built-in map function is called to call adict for each project in keys. get function, returns a list of print sortedDictValue (d) # print the same result
The program running result is:
{'a': 'aaa', 'c': 'cccc', 'b': 'bbb'}['aaa', 'bbb', 'cccc']['aaa', 'bbb', 'cccc']
I hope this article will help you learn Python programming.
Sort dictionaries in python
>>> D
{'A': 1, 'World': 11, 'z': 9, 'Hello': 10}
>>> K = d. keys ()
>>> K. sort ()
>>> K
['A', 'Hello', 'World', 'z']
>>> T = map (lambda key :( key, d [key]), k)
>>> T
[('A', 1), ('hello', 10), ('World', 11), ('Z', 9)]
How to sort the dictionaries in python and display them in the order they are added
Let's take a look at the dictionary dic = {'A': 1, 'B': 2, 'C': 3}. The elements in the dictionary have no order, therefore, dic [0] has a syntax error. Duplicate key values are not allowed, so dic. after adding ['C'] = 4, the dictionary becomes {'A': 1, 'B': 2, 'C': 4 }. now I want to think about the following: how can I sort keys or key values in different order as needed? Function prototype: sorted (dic, value, reverse) Interpretation: dic is a comparison function, and value is the sorting object (key or key value here), reverse: note whether ascending or descending, True -- descending, False -- Ascending (default) Case: dic = {'A': 1, 'B': 2, 'C ': 3} 1. print sorted (dic. iteritems (), key = lambda asd: asd [0], reverse = True) # result: [('C', 3), ('B', 2 ), ('A', 1)] 2. print sorted (dic. iteritems (), key = lambda asd: asd [0]) # result: [('A', 1), ('B', 2), ('C ', 3)], default (ascending) 3. print sorted (dic. iteritems (), key = lambda asd: asd [1]) # result: [('A', 1), ('B', 2), ('C ', 3)] Here we will introduce two functions: 1. lambda case: fuc = lambda x: x + 2 print fuc (3) # result 5, meaning x + 22 is returned for x. iteritems () Case: dic = {'A': 1, 'B': 2, 'C': 3} print dic. iteritems () # Return a dictionary key-Value Pair iterator in the function sorted (dic. in iteritems (), key = lambda asd: asd [1]), the first parameter is passed to the second parameter "key-key value ", the second parameter retrieves the key ([0]) or key value ([1 ])