The sorted function in Python and the Operator.itemgetter function "reprint"

Source: Internet
Author: User
Tags define function

Operator.itemgetter function
The Itemgetter function provided by the operator module is used to get the data of which dimensions of the object, with some ordinal numbers (that is, the number of the data that needs to be fetched in the object), as shown in the example below.

A = [A]
>>> B=operator.itemgetter (1)//define function B, get the value of the 1th field of an object
>>> B (a)
2
>>> B=operator.itemgetter (1,0)//define function B, get the 1th field and No. 0 value of an 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.

Sorted function
Python's built-in sorting function sorted can be sorted on list or iterator, on official website: http://docs.python.org/2/library/functions.html?highlight= Sorted#sorted, the function prototype is:

Sorted (iterable[, cmp[, key[, reverse]])

Parameter explanation:

(1) iterable specifies the list or iterable to sort, not to mention;

(2) CMP is a function that specifies a function to compare when sorting, you can specify a function or a lambda function, such as:

Students is a list of class objects, no member has three fields, you can set the CMP function by comparing it with sorted, for example, to sort by comparing the third data member, the code can write:
Students = [(' John ', ' A ', '), (' Jane ', ' B ', '), (' Dave ', ' B ', 10)]
Sorted (students, KEY=LAMBDA student:student[2])
(3) key is a function that specifies which item to sort the elements to be sorted, the function is illustrated by the above example, the code is as follows:
Sorted (students, KEY=LAMBDA student:student[2])

The function of the lambda function specified by key is to go to the third field of the element student (i.e.: student[2]), so when sorted is sorted, it is sorted by the third field that students all the elements.

With the Operator.itemgetter function above, you can also use this function, for example, to sort by the third field of student, you can write:
Sorted (students, Key=operator.itemgetter (2))
The sorted function can also be ordered in multiple levels, for example, to sort by the second field and the third field, so you can write:
Sorted (students, Key=operator.itemgetter ())

That is, sort the second field first, and then sort by the third field.
(4) The reverse parameter does not have to say more, is a bool variable, indicating ascending or descending order, the default is False (ascending order), when defined as true will be sorted in descending order.

The sorted function in Python and the Operator.itemgetter function "reprint"

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.