Python Anonymous/Lambda Function
In this article, you'll learn about the anonymous function, also known as lambda functions. You'll learn what is it, its syntax and how to use it (with examples).
Table of Contents
Python匿名/Lambda函数
在这篇文章里,你将学习到匿名函数,你还了解到lambda函数。你将认识到它的语法和怎样使用它(带例子)。
表格内容
- 什么是lambda函数?
- 怎么使用lambda函数
- Lambda函数的语法
- Lambda函数的例子
- Lambda函数的使用
What are lambda functions in Python?
In Python, anonymous function is a function that is defined without a name.
While normal functions are defined using the def
keyword, in Python anonymous functions are defined using the lambda
keyword.
Hence, anonymous functions are also called lambda functions.
什么的Lambda函数?
在Python里,匿名函数是定义一个不带名字的函数。
一个正常的函数定义使用def关键字,在Python匿名函数里被定义使用Lambda关键字。
因此,匿名函数也叫Lambda函数。
How to use lambda Functions in Python?
A lambda function in python has the following syntax.
Syntax of Lambda Function in python
lambda arguments: expression
Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.
怎么使用Lambda函数?
一个Lambda函数有以下语法。
Lambda函数的语法
Lambda函数可以有任何个数的参数但是仅仅一个表达。计算并返回表达式。Lambda函数可以用于任何需要函数对象的地方。
Example of Lambda Function in python
Here is an example of lambda function that doubles the input value.
# Program to show the use of lambda functions
double = lambda x: x * 2
# Output: 10
print(double(5))
In the above program, lambda x: x * 2
is the lambda function. Here x is the argument and x * 2
is the expression that gets evaluated and returned.
Lambda函数的例子
这里有个Lambda函数的例子,输入的值变2倍。
In [1]: double = lambda x: x * 2
In [2]: print(double(5))
10 //输出
在上面程序里,Lambda x: x * 2是Lambda函数,这的x是参数,x * 2是计算表达式再返回。
This function has no name. It returns a function object which is assigned to the identifier double
. We can now call it as a normal function. The statement
double = lambda x: x * 2
is nearly the same as
def double(x):
return x * 2
这函数没有名字,它返回一个函数对象是赋值给double的表示法。我们可以调用它像一个正常函数.这是语法
double = lambda x: x * 2
和他一个的方式
def double(x):
return x * 2
Use of Lambda Function in python
We use lambda functions when we require a nameless function for a short period of time.
In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments). Lambda functions are used along with built-in functions like filter()
, map()
etc.
Example use with filter()
The filter()
function in Python takes in a function and a list as arguments.
The function is called with all the items in the list and a new list is returned which contains items for which the function evaluats to True
.
Lambda函数的使用
当我们需要一个无名字的函数并短暂使用一段时间时2吗可以使用Lambda函数。
在Python里,我们一般使用它作为一个高阶的参数(接受函数作为参数的其它函数)。Lambda函数和内置函数一起使用像filter(),map()等等。
带filter()的例子
这个filter()函数接受一个函数和一个列表作为参数。
在列表中所有的项都被该函数调用,包含函数计算为true的项的一个新的列表将会被返回。
Here is an example use of filter()
function to filter out only even numbers from a list.
# Program to filter out only the even items from a list
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
# Output: [4, 6, 8, 12]
print(new_list)
这里有一个使用filter()函数的例子,去过滤出仅仅是双数的列表。
In [6]: my_list = [1,5,4,6,8,11,3,12]
In [7]: new_list = list(filter(lambda x: (x%2 == 0),my_list))
In [8]: print(new_list)
[4, 6, 8, 12]
Example use with map()
The map()
function in Python takes in a function and a list.
The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item.
使用map()的例子
在Python中这map()函数接收一个函数和列表。
在列表中所有的项都被该函数调用,一个新的列表被返回,包含所有项返回一一返回的项。
Here is an example use of map()
function to double all the items in a list.
# Program to double each item in a list using map()
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2 , my_list))
# Output: [2, 10, 8, 12, 16, 22, 6, 24]
print(new_list)
这里有一个使用map()函数的例子,一个列表中每项变为双倍。
In [9]: my_list = [1,5,4,6,8,11,3,12]
In [10]: new_list = list(map(lambda x:x *2,my_list))
In [11]: print(new_list)
[2, 10, 8, 12, 16, 22, 6, 24]
Check out these examples to learn more: