Two methods and examples of Python list sorting

Source: Internet
Author: User
Tags python list

to sort the list, Python provides two method methods 1. Sort by the list's built-in function List.sort List.sort (func=none, Key=none, reverse=False) Python instance:>>> list = [2,5,8,9,3]  >>>list [2,5,8,9,3]  >>>List.sort ()>>>list [2, 3, 5, 8, 9] Method 2: Sort the Python instance using the sequence type function sorted (list) (starting from 2.4).>>> list = [2,5,8,9,3]  >>>list [2,5,8,9,3]  >>>sorted (list) [2, 3, 5, 8, 9] The difference between the two methods: sorted (list) Returns an object that can be used as an expression. The original list does not change, creating a new sorted list object. List.sort () does not return an object, changing the original list. Other sort instances: Instance 1: Forward sort>>>l = [2,3,1,4]>>>L.sort ()>>>L>>>[1,2,3,4] Example 2: reverse sort>>>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 (LambdaX,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=LambdaX: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)]>>>Importoperator>>>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) forI,xinchEnumerate (L)]#I can confirm the stable sort>>>A.sort ()>>>l = [S[2] forSinchA]>>>L>>>[('a', 1), ('b', 2), ('C', 3), ('D', 4)] above gives a list of 6 of the method of sorting, where instance 3.4.5. 6 can play to sort an item in list item as the comparison keyword. Efficiency comparison: CMP< DSU <key by experimental comparison, Method 3 is slower than method 6, method 6 is slower than Method 4, Method 4 and Method 5 are basically quite a few keywords comparison sort: instance 7:>>>l = [('D', 2), ('a', 4), ('b', 3), ('C', 2)]>>> L.sort (key=LambdaX:x[1])>>>L>>>[('D', 2), ('C', 2), ('b', 3), ('a', 4] We see that this sort of L is sorted by the second keyword only, if we want to use the second keyword in order to sort the first keyword? There are two methods for instance 8:>>> L = [('D', 2), ('a', 4), ('b', 3), ('C', 2)]>>> L.sort (key=LambdaX: (x[1],x[0]))>>>L>>>[('C', 2), ('D', 2), ('b', 3), ('a', 4)] Example 9:>>> 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

Two methods and examples of Python list sorting

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.