Lambda function, anonymous function __ function

Source: Internet
Author: User
Tags anonymous

In simple terms, a lambda is an expression of an anonymous function.

What is an anonymous function, which is usually used in situations where you need to define a function, but do not bother to name a function.

For example: The square of every element in a list: map (lambda x:x*x, [y for y in range (10)])

This approach is better than:
def SW (x):
Return x*x
Map (sw,[y for Y in range (10)]

is because the latter defines a function that wastes time and space.

An anonymous function is essentially a function, and he says that what is abstracted is a set of operations

A = [1,2,3], you will find that the right side of the equal sign is completely off the left side of the equals sign, and if you can get used to the thing on the right side of the equal sign, then the F = lambda x:x+1 alone is not hard to understand.

Now look at the map function again, and he can map a function to an enumerable type. Follow the previous function map (f, a)
The result becomes (2,3,4), and the F is applied to each element of a in turn.

When it comes to this type of wording, you'll find yourself able to do some sort of operation on each element of a list, as a map function from a loop, and then pass this operation as a parameter to the map with a lambda expression, thinking about things higher, There will be fewer details to filter on.

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.