Lambda expression application in Python

Source: Internet
Author: User

For simple functions, there is also an easy way to represent a lambda expression

#普通函数
1def func (a):2 return a+13print' TEST1_FUNC0:', func (+)
4
#lambda表达式 5Lambda a:a+16print'test2_func0: ', func0 (+)

In this way, the results of the 1000+1 will be printed out of this function, but with the following

The existence of a lambda means a concise representation of a simple function.

Speaking of Lambda, here are some more small partners that can give Lambda buff:

1.map function, we use the map function to manipulate all the elements in the list. Map has two parameters (function, list), which iterates through each element of the list and executes the passed function arguments. In the output to the new list.

1 li = [one, one, 2] new_list = map (lambda a:a + li)
Output: [111, 122, 133]

Of course, map can also complete the addition of multiple arrays:

1 li = [one, one, one]2 sl = [1, 2, 3]3 new_list = map (Lambda A, b:a + B, Li, sl) 4 Print new_list
Output:[approx.]

2.reduce function, which accumulates for all elements in the sequence:

1 lst = [11,22,33]2 func2 = reduce (lambda arg1,arg2:arg1+arg2,lst)3  print'func2:', Func2
Output:func2:66

3.filter function, he can filter the data according to the conditions:

1 li = [one, one, 2] new_list = filter (lambda arg:arg > P, li)
New_list
Output: [33]

Lambda expression application in Python

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.