Example of ordering methods for dictionaries (DICT) and lists (list) in Python

Source: Internet
Author: User
First, sort the list

The recommended sort method is to use the built-in sort () method, which is the fastest and is a stable sort
Copy the Code code as follows:

>>> a = [1,9,3,7,2,0,5]
>>> A.sort ()
>>> Print a
[0, 1, 2, 3, 5, 7, 9]
>>> A.sort (reverse=true)
>>> Print a
[9, 7, 5, 3, 2, 1, 0]
>>> B = [' E ', ' A ', ' be ', ' ad ', ' dab ', ' DBC ']
>>> B.sort ()
>>> Print B
[' A ', ' ad ', ' Be ', ' dab ', ' DBC ', ' e ']

The ordering of the list follows the DSU (decorate-sort-undecorate) pattern, and the sequence is compared in the order of the installation entries, and for the string just in the example, the characters are compared in order from left to right, and the comparison is stopped once the result is reached.

Second, the dictionary (dict) to sort

In fact, the dictionary (dict) is an unordered sequence, not a sort, we can only follow the dictionary key/value to sort, and then let the corresponding Value/key also in the same order
Any sort of dictionary problem is ultimately attributed to the ordering of a list of keys (key) or values (value) of the dictionary (dict)

1. Sort by the key of the dictionary (dict) [1]

Copy the Code code as follows:

def sorteddictvalues (Adict,reverse=false):
Keys = Adict.keys ()
Keys.sort (Reverse=reverse)
return [Adict[key] for key in keys]

If you need to return both the key and the value, replace the last return statement with the following:
Copy CodeThe code is as follows:

return [(Key,adict[key]]) for key in keys]

Another simple way to write is to use the built-in sorted () method to sort:
Copy CodeThe code is as follows:

>>> d = {' C ': 1, ' e ': ' 5 ', ' B ': 7}
>>> Sorted (D.items ())
[(' B ', 7), (' C ', 1), (' E ', ' 5 ')]

However, performance will be slightly reduced, if very demanding performance, or the use of native to the List.sort () method is better

2. Sort by the value of the dictionary (dict) [2]

Copy CodeThe code is as follows:

def sorted_dict (container, keys, reverse):
"" Returns the list of keys, sorted by the corresponding value in container ""
Aux = [(Container[k], K) for K in keys]
Aux.sort ()
If Reverse:aux.reverse ()
return [k for V, K in aux]

You can also use the sorted () method to achieve the same functionality:
Copy CodeThe code is as follows:

Sorted (D.items (), Key=lambda d:d[1], reverse=true)

Third, conclusion

Through the above code analysis, the following principles are broadly summarized:
* The ordering of dictionaries is ultimately due to the ordering of the list of keys or values of the dictionary
* Sort the list, prioritize using the built-in List.sort () method

  • 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.