Properties of the lambda function

Source: Internet
Author: User

Lambda expressions can be understood as an abstract function implementation method, which has only the most basic three steps: give the argument, implement the expression, and return the result. This method is very clean, reduce the use of memory, the entire program is less pollution of the function, the code format will be more concise. However, the use in Python is limited and can only be evaluated in simple expressions.

Here's a quick example of a code that knows how to chop:

Func = (lambda for in xrange (4)) for in func:    I (4 )141664

The meaning of the above code is simple, for I in xrange (4) Returns a list [0,1,2,3], and the Func object is actually an iterator iterator, after which the result is returned:

( Lambda x:x**0)(lambda x:x**1) (lambda x:x**2)(lambda  x:x**3)

The lambda expression is then evaluated.

Other words:

I (0) = (lambda x:x**0) I (1) = (lambda x:x**1) I (2) = (lambda x:x**2  ) I (3) = (lambda x:x**3)

In the end, it naturally returns to the 1,4,16,64 such a result.

Another example of using the Map,filter function in conjunction with a lambda function:

>>> l = [' foo ', ' Bar ', ' far ']>>> map (lambda x:x, L) [' foo ', ' Bar ', ' far ']>>> filter (Lambda x: ' F ' in X, L) [' foo ', ' far ']>>> map (Lambda X:x.upper (), Filter (lambda x: ' F ' in X, L)) [' Foo ', ' far ']     

Map (A, B) establishes a mapping relationship between a A and a, a is actually an element in B, and the lambda function evaluates the expression with a as a parameter before returning the result.

The same is true for filter, except that filter returns only the elements that the lambda function returns as true.

Properties of the lambda function

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.