Python Sort Method

Source: Internet
Author: User

Official documents:

sort(*, key=none, reverse=false)

This method sorts the list on place, with the using only < comparisons between items. Exceptions is not suppressed-if any comparison operations fail, the entire sort operation would fail (and the list would Likely is left in a partially modified state).

sort()Accepts arguments that can is passed by keyword (keyword-only arguments):

Key specifies a function of one argument that's used to extract a comparison key from each list element (for Example,   key=str.lower). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of None means that list items is sorted directly without calculating a separate key Value.

The utility is available-to- functools.cmp_to_key() convert a 2.x style cmp function to a key func tion.

Reverse Is a Boolean value. If set True to and then the list elements is sorted as if each comparison were reversed.

This method modifies the sequence on place for economy of space when sorting a large sequence. To remind users, it operates by side effect, it does not return the sorted sequence (use to sorted() explicitly r Equest A new sorted list instance).

The method is guaranteed to be sort() stable. A sort is stable if it guarantees the relative order of elements that compare equal-this are helpful for so Rting in multiple passes (for example, sort by department, then by salary grade).

CPython Implementation Detail: While a list was being sorted, the effect of attempting to mutate, or even inspect, and the list is undefined. The C implementation of Python makes the list appear empty for the duration, and raises ValueError if it can detect tha T the list has been mutated during a sort.

Sort () can only be ordered for pure numbers or letters, otherwise the error is:

>>>alist[1, 3, 4, 7, 8, 9]>>> Alist.append ('a')>>>Alist.sort () Traceback (most recent): File"<pyshell#83>", Line 1,inch<module>Alist.sort () TypeError:'<'  notSupported between instances of'Str'  and 'int'>>>

Sort () can be sorted without parameters or up to two keyword parameters, default key=none, reverse=false, default ascending:

>>> alist = [1,4,2,7,9,3]>>> alist.sort ()>>> alist[1, 2, 3, 4, 7, 9]

Specify reverse=true in descending order:

>>> alist = [1,4,2,7,9,3]>>> alist.sort (reverse=True)>>> alist[ 9, 7, 4, 3, 2, 1]

If you want to keep the original list intact, copy a new list out of order:

>>> alist = [1,4,2,7,9,3]# cannot write blist = alist, so write blist point to alist same memory space >>> blist.sort ()>>> alist[1, 4, 2, 7, 9, 3]>>> blist[1, 2, 3, 4, 7, 9 ]

or use the sorted () method:

>>> alist = [1,4,2,7,9,3]>>> blist = sorted (alist)>>> alist[1, 4 , 2, 7, 9, 3]>>> blist[1, 2, 3, 4, 7, 9]

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