Introduction to the usage of lambda function in python (with examples)

不言
Release: 2019-04-01 11:17:58
forward
125997 people have browsed it

This article brings you an introduction to the usage of the lambda function in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Anonymous function lambda: refers to a type of function or subroutine that does not need to define an identifier (function name).
The lambda function can receive any number of parameters (including optional parameters) and return the value of a single expression.

Syntax:

lambda [arg1 [,arg2,.....argn]]:expression
Copy after login

Before the colon are parameters, there can be multiple, separated by commas, and the one on the right side of the colon is an expression (can only be one). In fact, the return value of lambda is the address of a function, which is the function object.

Example:

1. Assign the lambda function to a variable, and call the lambda function indirectly through this variable.

def sum(x,y):
    return x+y
print(sum(1,2))
Copy after login

Use lambda function:

sum = lambda x,y : x+y
print(sum(1,2))
Copy after login

2. Pass the lambda function as a parameter to other functions. Some Python built-in functions accept functions as parameters.

def odd(x):
    return x%2
temp = range(10)
show = filter(odd,temp)
print(list(show))   #[1, 3, 5, 7, 9]
Copy after login

Use lambda function:

print(list(filter(lambda x: x%2,range(10))))    #[1, 3, 5, 7, 9]
Copy after login

At this time, the lambda function is used to specify the conditions for filtering list elements.

Another example:

map(lambda x: x+1, [1, 2,3])    #[2, 3, 4]
Copy after login

At this time, the lambda function is used to specify a common operation for each element in the list.

In addition: sorted(), map()

3. Return the lambda function to the caller as the return value of other functions.

The return value of a function can also be a function. For example return lambda x, y: x yReturn an addition function. At this time, the lambda function is actually a function defined inside a certain function, called a nested function, or internal function. Correspondingly, functions containing nested functions are called external functions. Inner functions can access local variables of outer functions. This feature is the basis of closure programming.

4. Assign the lambda function to other functions, thereby replacing other functions with the lambda function.

For example, in order to mask (Mock) the function sleep in the standard library time, we can call: time.sleep=lambda x:None during program initialization. In this way, calling the sleep function of the time library in subsequent code will not perform the original function. For example, when executing time.sleep(3), the program will not sleep for 3 seconds, but will do nothing

5. Reduce the if...else statement It is a single conditional expression. The syntax of

is: expression1 if A else expression2

If A is True, the result of the conditional expression is expression1, otherwise it is expression2

def s(x):
    if x==1:
        return "yes"
    else:
        return "no"
print(s(0))
print(s(1))
Copy after login

Using lambda function:

s=lambda x:"yes" if x==1 else "no"
print(s(0))
print(s(1))
Copy after login

[Related recommendations: python video tutorial]

The above is the detailed content of Introduction to the usage of lambda function in python (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!