An instance of the sort method for the Python Dictionary (dict) and lists (list) _python

Source: Internet
Author: User

First, sort lists (list)

The recommended sort is to use the built-in sort () method, which is the fastest and stable sort

Copy 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 sort of list follows the DSU (decorate-sort-undecorate) pattern, the sequence is compared to the order of the installation entries, and for the string in the example, the character is compared from left to right, and the comparison is stopped once the results are drawn.

Second, the dictionary (dict) to sort

In fact, the dictionary (dict) is a unordered sequence, not to order, we can only according to the dictionary key/value to sort, and then let the corresponding values/keys in the same order
Any sort of dictionary problem is ultimately summed up as a list of keys (key) or value (value) of a dictionary (dict).

1, by the Dictionary (dict) of the key to sort [1]

Copy 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 keys and values at the same time, change the last returned statement to:
Copy Code code as follows:
return [(Key,adict[key]])

Another simple way to write is to use the built-in sorted () method for sorting:
Copy Code code 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 using native to List.sort () method is better

2, by the Dictionary (dict) of the value of the sort [2]

Copy Code code as follows:

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

You can also use the sorted () method to achieve the same function:
Copy Code code as follows:
Sorted (D.items (), Key=lambda d:d[1], reverse=true)

Third, the conclusion

Through the above analysis of the Code, the general summary of the following principles:
* The sort of dictionary, which ultimately boils down to the sort of list of keys or values of a dictionary
* Sort the list, using the built-in List.sort () method as a priority

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.