List sorting instances in Python and python sorting instances

Source: Internet
Author: User

List sorting instances in Python and python sorting instances

In many cases, we need to sort the List. Python provides two methods to sort the given List L:

Method 1. sort by the List member function sort
Method 2. Use the built-in function sorted for sorting (starting from 2.4)

The two methods are similar in use. The first method is used as an example to explain:

From Python2.4, the sort method has three optional parameters, which are described in the Python Library Reference.
Copy codeThe Code is as follows:
Cmp: cmp specifies a custom comparison function of two arguments (iterable elements) which shold return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal, or larger than the second argument:
"Cmp = lambda x, y: cmp (x. lower (), y. lower ())"
Key: key specifies a function of one argument that is used to extract a comparison key from each list element: "key = str. lower"
Reverse: reverse is a boolean value. if set to True, then the list elements are sorted as if each comparison were reversed. in general, the key and reverse conversion processes are much faster than specifying
Equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.

The following are specific instances of sort.
Instance 1:
Copy codeThe Code is as follows:
>>> L = [2, 3, 1, 4]
>>> L. sort ()
>>> L
>>> [1, 2, 4]

Instance 2:
Copy codeThe Code is as follows:
>>> L = [2, 3, 1, 4]
>>> L. sort (reverse = True)
>>> L
>>> [4, 3, 2, 1]

Instance 3:
Copy codeThe Code is as follows:
>>> L = [('B', 2), ('A', 1), ('C', 3), ('D', 4)]
>>> L. sort (cmp = lambda x, y: cmp (x [1], y [1])
>>> L
>>> [('A', 1), ('B', 2), ('C', 3), ('D', 4)]

Instance 4:
Copy codeThe Code is as follows:
>>> L = [('B', 2), ('A', 1), ('C', 3), ('D', 4)]
>>> L. sort (key = lambda x: x [1])
>>> L
>>> [('A', 1), ('B', 2), ('C', 3), ('D', 4)]

Instance 5:
Copy codeThe Code is as follows:
>>> 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)]

Instance 6: (DSU method: Decorate-Sort-Undercorate)
Copy codeThe Code is as follows:
>>> 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)]

In the preceding section, we use the List sorting method in Step 6. instance 3.4.5.6 can be used to sort an item in List item.
Sort the comparison keywords.
Efficiency Comparison:
Copy codeThe Code is as follows:
Cmp <DSU <key

Through experimental comparison, method 3 is slower than Method 6, Method 6 is slower than method 4, and method 4 and Method 5 are basically equivalent.
Sort multiple keywords by comparison:
Instance 7:
Copy codeThe Code is as follows:
>>> 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)]

We can see that the sorted L is only sorted by the second keyword. If we want to use the second keyword
After sorting, what about sorting with the first keyword? There are two methods
Instance 8:
Copy codeThe Code is as follows:
>>> 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)]

Instance 9:
Copy codeThe Code is as follows:
>>> 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 tuple is compared from left to right. After comparison, if
Equal, compared to the second

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.