The use of Python:sorted,operator.itemgetter __python

Source: Internet
Author: User
Tags define function iterable
[Python] has two kinds of data in the container, one is the container's own sort function, one is the built-in sorted function.
The only difference between the sort function and the sorted function is that sort is sorted within the container, sorted generates a new ordered container.

For a simple array of l=[5,2,3,1,4].
Sort:l.sort ()

Sorted (...)
Sorted (iterable, Cmp=none, Key=none, Reverse=false)--> new sorted list
Iterable: An iterative type of container to be sorted;
CMP: A function For comparison, comparing what is determined by the key, having a default value, and an item in the iteration set;
Key: Use a named property or function of a list element (with only one parameter and return a value for sorting) as a keyword, with a default value, an item in the iteration set;
Reverse: Sorting rules. Reverse = True or reverse = False, with default values.
Return value: Is a sorted, iterative type, similar to iterable.

If it is a multidimensional list l=[(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)].
There are three options for sorting this multidimensional list
Using CMP functions

Sorted (L, Cmp=lambda x,y:cmp (x[1],y[1))
L.sort (Cmp=lambda x,y:cmp (x[1],y[1))

Using key

Sorted (L, Key=lambda x:x[1]);
L.sort (Key=lambda x:x[1]);

Reverse order

The above sorts all can add parameter reverse.
For example sorted (reverse=true), L.sort (reverse=true). or change it to false.

Ordereddict is a package in collections that can record the order in which dictionary elements are inserted, often used with sort functions to generate a sorted dictionary.
For example, an unordered dictionary.
D = {' Banana ': 3, ' Apple ': 4, ' pear ': 1, ' Orange ': 2}
By sorting to generate an ordered dictionary, there are several ways
Collections. Ordereddict (Sorted (D.items (), key = Lambda t:t[0))
Collections. Ordereddict (Sorted (D.items (), key = Lambda t:t[1))
Collections. Ordereddict (Sorted (D.items (), key = Lambda T:len (t[0)))

Operator.itemgetter function
The Itemgetter function provided by the operator module is used to obtain which dimension of the object's data, and the argument is some ordinal number (that is, the ordinal number of the data to be obtained in the object), see examples below.

A = [1,2,3]
B=operator.itemgetter (1)//define function B, get the value of the 1th field of the object > B (a)
B=operator.itemgetter (1,0)//define function B, get the value of the 1th field and No. 0 of the object
B (A)
(2, 1)
Note that the Operator.itemgetter function does not get a value, but instead defines a function that acts on the object to get the value.

Multilevel sorting with operator function
Students = [(' John ', ' A ',], (' Jane ', ' B ', '), (' Dave ', ' B ', 10),]
>>> sorted (students, key=itemgetter (1,2)) # Sort by grade then by age
[(' John ', ' A ',], (' Dave ', ' B ', '), (' Jane ', ' B ', 12)]
# # to sort dictionaries
>>> d = {' Data1 ': 3, ' data2 ': 1, ' data3 ': 2, ' DATA4 ': 4}
>>> Sorted (D.iteritems (), Key=itemgetter (1), reverse=true)
[(' Data4 ', 4), (' Data1 ', 3), (' Data3 ', 2), (' Data2 ', 1)]

Author: Nilstark
Source: Jianshu
Copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.
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: 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.