Python sorts dictionaries and python sorts dictionaries.

Source: Internet
Author: User

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 ])

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.