Python Learning--lambda using __python

Source: Internet
Author: User

These two days to learn some of the Python application tutorials, see this writing:


return series.apply (lambda x: (x-min_val)/scale)-1.0)

What is a lambda? Never seen, so search online, basically say Lambda is an anonymous function, to solve the problem of the naming of simple functions, for example, you want to write a function in the program

def a (x):
    x = x + 1 return
    x
print (A (2))

So if you write like that, it's stupid. Oh no, it's too much, you still have to name the function for later reading, and if there's a way to do that without having to define the function and do it with just one line, it's cool. This is the role of the lambda, try the following:

A = lambda x:x + 1

The code looks a lot simpler. Perhaps this example does not explain the convenience of the lambda, but now look back at the top example, if you write a function

def a (x):
    x = (x-min_val)/scale)-1.0
    

And this function is only used here once in the whole program, that is not many. In contrast to the topmost type, the convenience of the lambda points to a glance. Here's a concrete explanation: The x behind the lambda is the function argument, the formal parameter we often say, followed by the function entity, which can only be passed in a single argument when using a lambda function alone, that is, only one number is applied, and the map () Function can also apply a lambda function to a list:

foo = [2, 9, 8, MB]
Reslut = map (lambda x:x+1, foo)
print (list (reslut))

The results of help (map) are as follows


Make an iterator that uses the parameter calculation functions from each iterator. Stops when the shortest iterator is depleted. (Google translation)

The map () function produces an iterator that iterates through each element of the list, applies a lambda function to each element, and the list () function converts the return value of the iterator to the list output.


New Python beginners, the first blog, Welcome to the great God advice .... ..... ..... ...... ....... ....... ...

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: 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.