Go Python3:sorted () function and the sort () function in the list

Source: Internet
Author: User
Tags iterable

Python3:sorted () function and the sort () function in the list

    • Reprint please indicate author and source: http://blog.csdn.net/u011475210
    • Operating system: WINDOWS 10
    • Software version: PYTHON-3.6.2-AMD64
    • A compilation?? By: wordzzzz

First, sort,sorted function introduction:

?? The sort function is a function in the list, and sorted can sort lists or iterator.

?? Below we use Help to see their usage and features:
Sort

>>> help(list.sort)Help on method_descriptor:sort(...)    L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
    • 1
    • 2
    • 3
    • 4
    • 5

Sorted
python3.x:

>> > Help (sorted)-on Built-in function sorted in module Builtins:sorted (iterable,/, *, Key=none, Reverse=False) Return a new list containing all items from the iterable in Ascendi Ng order. A Custom key function can be supplied to customize the sort order, and the reverse flag can be Set to request the result in descending order.       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

python2.x:

>>> help(sorted)Help on built-in function sorted in module __builtin__:sorted(...)    sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
    • 1
    • 2
    • 3
    • 4
    • 5

?? Well, the sorted function of python3.x and python2.x is a bit different, and the CMP parameter is missing. Below this slag is mainly based on python2.x sorted function to explain, python3.x directly ignore the parameters of CMP, in order to ensure code commonality, not recommended for future programming in the use of CMP parameters.

Ii. comparison of sort and sorted:

?? Sorting a list with the Sort function affects the list itself, and sorted does not.
Example:

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

?? Sorted function of python2.x: sorted (iterable,cmp,key,reverse)
Parameters:
-Iterable can be a list or a iterator;
-CMP is a comparison function with two parameters;
-Key is a function with one parameter;
-Reverse is false or true;

To illustrate:
(1) Sort by CMP function:

>>> List1 = [(' David ',90), (' Mary ',90), (' Sara ',80), (' Lily ',95)]>>> sorted (list1,cmp =lambda x,y:cmp (X[0],y[0])) [( Span class= "hljs-string" > ' David ', 90), ( ' Lily ', 95", ( "Mary", 90), ( Sara ', 80)]>>> sorted (list1,cmp = lambda x,y:cmp (X[1],y[1])) [(  ' Sara ', 80), ( ' David ', 90", ( "Mary", 90), ( Lily ', 95)]           
    • 1
    • 2
    • 3
    • 4
    • 5

(2) Sort by key function:

>>> List1 = [(' David ',90), (' Mary ',90), (' Sara ',80), (' Lily ',95)] >>> sorted (List1,key = lambda List1:list1[0]) [( ' David ', 90), ( 95), ( ' Mary ', 90", ( ' Sara ', 80)] >>> sorted (List1,key = lambda list1:list1[1]) [( ' Sara ', 80), ( ' David ', 90), ( ' Mary ', 90), ( Lily ', 95)]         
    • 1
    • 2
    • 3
    • 4
    • 5

(3) Sort by reverse:

>>> sorted(list1,reverse = True)[(‘sara‘, 80), (‘mary‘, 90), (‘lily‘, 95), (‘david‘, 90)]
    • 1
    • 2

(4) Sort by Operator.itemgetter function:

>>>from operator import itemgetter >>> Sorted (List1, Key=itemgetter (1)) [( ' Sara ', 80), ( ' David ', 90), ( 90), ( ' Lily ', 95)]>>> sorted (List1, Key=itemgetter (0 ) [( ' David ', 90), ( ' Lily ', 95), ( ' Mary ', 90), (  ' Sara ', 80)]         
    • 1
    • 2
    • 3
    • 4
    • 5

?? Describes the Operator.itemgetter function:

>>> import operator>>> a = [1,2,3]>>> b = operator.itemgetter(0)>>> b(a)1
    • 1
    • 2
    • 3
    • 4
    • 5

?? The Operator.itemgetter function does not get a value, but instead defines a function.

(5) Multilevel sequencing:

>>> sorted(list1, key=itemgetter(0,1))[(‘david‘, 90), (‘lily‘, 95), (‘mary‘, 90), (‘sara‘, 80)]

Go Python3:sorted () function and the sort () function in the list

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.