Sort instances of lists in Python _python

Source: Internet
Author: User
Tags in python

Most of the time, we need to sort the list, and Python provides two ways to sort the given list L:

Method 1. Sort with the member function of the list
Method 2. Sort with the built-in function sorted (starting from 2.4)

These two methods are similar to each other and are explained in the first example:

Starting with Python2.4, the sort method has three optional parameters, as described in the Python Library reference

Copy Code code as follows:

CMP:CMP Specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero O R positive number depending on whether the the ' argument is considered smaller-than, equal to, or larger than the second Argument
"Cmp=lambda x,y:cmp (X.lower (), Y.lower ())"
Key:key specifies a function of one argument that is used to extract a comparison key from each list element: "Key=str.low Er
Reverse:reverse is a Boolean value. If set to True, then the "list elements are sorted as if each comparison were. In general, the key and reverse conversion processes are much faster than specifying
Equivalent CMP function. This is because the CMP is called multiple The times for each list element while key and reverse the touch of each element only once.

The following is a concrete example of sort.
Example 1:
Copy Code code as follows:

>>>l = [2,3,1,4]
>>>l.sort ()
>>>l
>>>[1,2,3,4]

Example 2:
Copy Code code as follows:

>>>l = [2,3,1,4]
>>>l.sort (Reverse=true)
>>>l
>>>[4,3,2,1]

Example 3:
Copy Code code as follows:

>>>l = [(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)]
>>>l.sort (Cmp=lambda x,y:cmp (x[1],y[1))
>>>l
>>>[(' A ', 1), (' B ', 2), (' C ', 3), (' d ', 4)]

Example 4:
Copy Code code as follows:

>>>l = [(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)]
>>>l.sort (Key=lambda x:x[1])
>>>l
>>>[(' A ', 1), (' B ', 2), (' C ', 3), (' d ', 4)]

Example 5:
Copy Code code as follows:

>>>l = [(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)]
>>>import operator
>>>l.sort (Key=operator.itemgetter (1))
>>>l
>>>[(' A ', 1), (' B ', 2), (' C ', 3), (' d ', 4)]

Example 6: (DSU method: Decorate-sort-undercorate)
Copy Code code as follows:

>>>l = [(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)]
>>>a = [(x[1],i,x) for i,x in Enumerate (L)] #i can confirm the stable sort
>>>a.sort ()
>>>l = [s[2] for s in A]
>>>l
>>>[(' A ', 1), (' B ', 2), (' C ', 3), (' d ', 4)]

The above gives a method of sorting list in 6, where instance 3.4.5.6 can play a pair of items in the list item
Sorts the comparison keywords.
Efficiency comparison:
Copy Code code as follows:

CMP < DSU < key

By experiment comparison, Method 3 is slower than method 6, method 6 is slower than Method 4, Method 4 and Method 5 are basically equivalent
Multi-keyword comparison sort:
Example 7:
Copy Code code as follows:

>>>l = [(' d ', 2), (' A ', 4), (' B ', 3), (' C ', 2)]
>>> L.sort (Key=lambda x:x[1])
>>> L
>>>[(' d ', 2), (' C ', 2), (' B ', 3), (' A ', 4)]

We see that at this time the sorted L is only sorted by the second keyword, if we want to use the second keyword
After the sequence, then use the first keyword to sort it? There are two ways
Example 8:
Copy Code code as follows:

>>> L = [(' d ', 2], (' A ', 4), (' B ', 3), (' C ', 2)]
>>> L.sort (Key=lambda x: (x[1],x[0))
>>> L
>>>[(' C ', 2), (' d ', 2), (' B ', 3), (' A ', 4)]

Example 9:
Copy Code code as follows:

>>> L = [(' d ', 2], (' A ', 4), (' B ', 3), (' C ', 2)]
>>> L.sort (Key=operator.itemgetter (1,0))
>>> L
>>>[(' C ', 2), (' d ', 2), (' B ', 3), (' A ', 4)]

Why is instance 8 able to work? The reason is that tuple is compared from left to right, comparing the first one, if
Equal, compare the second

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.