Python function object

Source: Internet
Author: User

For Python version 2.x

1. Lambda functions

1 Lambda y 2 print func (2, 4)

Lambda generates a function object, the parameter is x, Y, and returns X+Y.

2. Map () function

1 RTN = map ((Lambda x:x*3), [1, 2, 3, 4, 5, 6])2print RTN

Map is a python built-in function, the first parameter is a function, the second argument is a sequence. The first function acts on each element of the sequence and places the result in the sequence Rtn.

3. Reduce () function

RTN = reduce ((lambda x, y:x + y), [1, 3, 5, 7, 9])print RTN

Reduce is also a python built-in function, with two parameters, the first parameter is a function, and the second is a sequence. The first two elements of the sequence are first used as the two parameters of the lambda function, the return value as the lambda parameter, the next element of the sequence (that is, the 3rd element) as the second parameter of the lambda, which in turn acts on the entire sequence.

4. Filter () function

def isodd (n):     if n% 2 = = 0        :return  False    Else        :return = Filter (isodd, [1, 2, 3, 4, 5, 6])print  rtntype (RTN) 

The filter function is also a python built-in function. The first parameter of the filter function is a function that filters the data, and the second parameter is the data to be filtered.

IsOdd judgment is not the cardinality, is the return true, not the return false. The filter is based on the result returned by the IsOdd function to decide whether to keep the number, and true to keep it. This will play a role in screening.

Summarize:

Lambda functions

Map () function

Reduce () function

Filter () function

Python function object

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.