Python sort function sort () differs from sorted ()

Source: Internet
Author: User
Tags iterable what parameter

Sort is the function of the container: sort (Cmp=none, Key=none, Reverse=false)

Sorted is a python built-in function: Sorted (iterable, Cmp=none, Key=none, Reverse=false)

Parameter resolution:

CMP: compare function, compare what parameter is determined by key. For example: CMP (E1, E2) is a comparison function with two parameters, the return value: Negative (E1 < E2), 0 (e1 = = E2), positive (E1 > E2).
Key: Use a property or function of the list element as the keyword.
Reverse: Collation, you can choose True or false.
Sorted one more parameter iterable: The container of the iteration type to be sorted

Example 1:

>>> a=[1,2,5,3,9,4,6,8,7,0,12]
>>> A.sort ()
>>> A
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]


>>> a=[1,2,5,3,9,4,6,8,7,0,12]
>>> Sorted (a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
>>> A
[1, 2, 5, 3, 9, 4, 6, 8, 7, 0, 12]

Difference:

For an unordered list of a, call A.sort () and return the A,sort () function after a is sorted to modify the contents of the list to be sorted.

For the same unordered list A, call sorted (a), a is sorted and a new list is returned, and a has no effect on a.

Example 2:

Suppose you use tuples to hold information about each student, including the number, name, and age. Use the list to save information for all students.

>>> list1=[(8, ' Logan ', +), (2, ' Mike '), (5, ' Lucy ', 19)]
>>> List1.sort ()
>>> List1
[(2, ' Mike ', '), (5, ' Lucy ', '), (8, ' Logan ', 20)]


>>> list1=[(8, ' Logan ', +), (2, ' Mike '), (5, ' Lucy ', 19)]
>>> Sorted (List1)
[(2, ' Mike ', '), (5, ' Lucy ', '), (8, ' Logan ', 20)]
>>> List1
[(8, ' Logan ', '), (2, ' Mike ', '), (5, ' Lucy ', 19)]

Summary: As the example shows, when the list is made up of lists (or tuple), by default, sort and sorted are sorted according to List[0] (or tuple[0]) as the sorted key.

These are the default sorting methods, and we can write code to control the sorting behavior of the two functions. There are three main ways: Based on key function, CMP function and based on reverse function

1) Sort based on key function

>>> list1=[(8, ' Logan ', +), (2, ' Mike '), (5, ' Lucy ', 19)]
>>> List1.sort (Key=lambda x:x[2])
>>> List1
[(5, ' Lucy ', +), (8, ' Logan ', '), (2, ' Mike ', 22)]


>>> list1=[(8, ' Logan ', +), (2, ' Mike '), (5, ' Lucy ', 19)]
>>> Sorted (List1, Key=lambda x:x[2])
[(5, ' Lucy ', +), (8, ' Logan ', '), (2, ' Mike ', 22)]
>>> List1
[(8, ' Logan ', '), (2, ' Mike ', '), (5, ' Lucy ', 19)]

2) based on CMP functions

>>> list1=[(8, ' Zogan ', +), (2, ' Mike '), (5, ' Lucy ', 19)]
>>> List1.sort (Cmp=lambda x,y:cmp (x[1],y[1]))
>>> List1
[(5, ' Lucy ', +), (2, ' Mike ', '), (8, ' Zogan ', 20)]

>>> list1=[(8, ' Zogan ', +), (2, ' Mike '), (5, ' Lucy ', 19)]
>>> Sorted (List1, Cmp=lambda x,y:cmp (x[1],y[1]))
[(5, ' Lucy ', +), (2, ' Mike ', '), (8, ' Zogan ', 20)]
>>> List1
[(8, ' Zogan ', '), (2, ' Mike ', '), (5, ' Lucy ', 19)]

3) based on reverse function

>>> a=[1,2,5,3,9,4,6,8,7,0,12]
>>> A.sort (Reverse=false)
>>> A
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]

>>> a=[1,2,5,3,9,4,6,8,7,0,12]
>>> A.sort (reverse=true)
>>> A
[12, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> A.sort (reverse=true)

Summary: Reverse=false is sorted in ascending order; Reverse=true is sorted in descending order

Python sort function sort () differs from sorted ()

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.