Python List Sort

Source: Internet
Author: User
Tags python list

One, Python provides a way to sort the list

1. Method:

(1) The list's built-in function list.sort is sorted,

(2) Sort by the sequence type function sorted (list).

2. Example:

>>> a_list = [ 2,5,4,3,1]
>>> a_list
[2, 5, 4, 3, 1]
>>> Sorted (a_list)
[1, 2, 3, 4, 5]
>>> a_list
[2, 5, 4, 3, 1]
>>> A_list.sort ()
>>> a_list
[1, 2, 3, 4, 5]
>>>

3. Description:

(1) sorted (list) Returns an object that can be used as an expression. The original list does not change, creating a new ordered list object

(2) List.sort () does not return an object, changing the original list

Ii. explanation of the List.sort () method

1.

starting with Python2.4, the sort method has three optional parameters: Cmp,key, Reverse

This is described in the Python Library reference:

CMP:CMP Specifies a custom comparison function of the arguments (iterable elements) which should return a negative, zero O R positive number depending on whether the first argument are 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's 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 is sorted as if each comparison were reversed. In general, the key and reverse conversion processes is much faster than specifying an
Equivalent CMP function. This is because CMP are called multiple times for each list element while keys and reverse touch each element only once.

2. Example


Example 1:
>>>l = [2,3,1,4]
>>>l.sort ()
>>>l
>>>[1,2,3,4]
Example 2:
>>>l = [2,3,1,4]
>>>l.sort (Reverse=true)
>>>l
>>>[4,3,2,1]
Example 3: Sort the second keyword
>>>l = [(' B ', 6), (' A ', 1), (' C ', 3), (' d ', 4)]
>>>L.sort (Lambda x,y:cmp (x[1],y[1]))
>>>l
>>>[(' A ', 1), (' C ', 3), (' d ', 4), (' B ', 6)]
Example 4: Sort the second keyword
>>>l = [(' B ', 6), (' A ', 1), (' C ', 3), (' d ', 4)]
>>>L.sort (Key=lambda x:x[1])
>>>l
>>>[(' A ', 1), (' C ', 3), (' d ', 4), (' B ', 6)]
Example 5: Sort the second keyword
>>>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)
>>>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 the method of sorting the list in 6, where instance 3.4.5.6 can play an item in the list item
Sort the comparison keywords.
Efficiency comparison:
CMP < DSU < key
Comparing with the experiment, 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:
>>>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)]
As we can see, the sort of L is now sorted by the second keyword only,

What if we want to order the second keyword and then sort it with the first keyword? There are two ways
Example 8:
>>> 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:

>>>import operator
>>> 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 does instance 8 work? The reason is that a tuple is compared from left to right, compared to the first, if
Equal, compare the second one





Python List Sort

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.