[Python] sorted function, pythonsorted Function

Source: Internet
Author: User

[Python] sorted function, pythonsorted Function

Http://www.cnblogs.com/sysu-blackbear/p/3283993.html

We need to sort List and Dict. Python provides two methods.
Sorts the given List L,
Method 1: Use the List member function sort to sort data locally without returning copies.
Method 2: Use the built-in function sorted for sorting (starting from 2.4). The copy is returned, and the original input remains unchanged.

-------------------------------- Sorted ---------------------------------------
>>> Help (sorted)
Help on built-in function sorted in module _ builtin __:

Sorted (...)
Sorted (iterable, cmp = None, key = None, reverse = False) --> new sorted list
--------------------------------- Sort ----------------------------------------
>>> Help (list. sort)
Help on method_descriptor:

Sort (...)
L. sort (cmp = None, key = None, reverse = False) -- stable sort * in place *;
Cmp (x, y)->-1, 0, 1
-----------------------------------------------------------------------------

Iterable: The iterable type;
Cmp: A function used for comparison. The key determines what to compare;
Key: use an attribute or function of the list element as a keyword. The default value is used to iterate an item in the set;
Reverse: Sorting rule. reverse = True in descending order or reverse = False in ascending order, with the default value.
Return Value: it is a sorted and iteratable type, which is the same as iterable. Parameter description: (1) the cmp parameter accepts a function and uses an integer as an example. The format is def f (a, B): return a-B. If the elements to be sorted are of another type, if logic a is less than logic B, the function returns a negative number. Logic a is equal to logic B, and function 0. Logic a is greater than logic B, and function 2 returns a positive number) the key parameter key also accepts a function. The difference is that this function only accepts one element in the format of def f (a): return len (a) the return value of the function accepted by the key, indicates the weight of this element, and sort will sort the values by weight (3) the reverse parameter accepts False or True to indicate whether the values are in reverse order. Example: (1) sort by element length: L = [{,}, {}] def F(X): return len (x) sort (key = f) print L output: [{1: 9}, {1: 5, 3: 4}, {1: 3, 6: 3}, {1: 1, 2: 4, 5: 6}] (2) sort the values of the elements whose keys are 1 in each dictionary element. L = }, {1:9}] def F2(A, B): return a [1]-B [1] L. sort (cmp = f2) print L

. PairList composed of tupleSort

Python code


Sort by key function:Returns a list composed of tuple.

Python code


Sort by cmp Function

Python code


Use the operator function to speed up the process,

Python code


Perform multi-level sorting using the operator Function

Python code


2. sort by dictionary and return a List composed of tuple, which is no longer a dictionary.

Python code

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.