python lambda_Python Lambda

python lambda

Hello python learners, hope all are well. Today we will learn about the python lambda function. Keyword lambda in python is used to create anonymous functions. Anonymous functions are those functions who are unnamed. That means you are defining a function without any name of the function.

python学习者您好,希望一切都好。 今天,我们将学习python lambda函数。 python中的关键字lambda用于创建匿名函数 。 匿名函数是那些未命名的函数。 这意味着您正在定义一个没有任何功能名称的功能。

Python Lambda (Python lambda)

The following is a basic syntax of writing anonymous function using lambda.

以下是使用lambda编写匿名函数的基本语法。

lambda arguments: expression

Compare the above with function structure:

将以上内容与功能结构进行比较:

def functionName( arguments ):
	statements...
	return something

Notice that, def is replaced by the keyword lambda, then there is no function name, after that arguments are as usual arguments.

请注意,def被关键字lambda代替,然后没有函数名,因为该参数是通常的参数。

Function contains some statement then may return some value or not. But using lambda, it will contain an expression that will be returned.

函数包含一些语句,然后可能会返回一些值或不会返回某些值。 但是使用lambda ,它将包含一个将返回的表达式。

Python Lambda函数 (Python lambda function)

def squareof(x):
   return x*x

p = squareof(5)
print(p)

We can convert above function to python lambda function as follows:

我们可以将上述函数转换为python lambda函数,如下所示:

f = lambda x: x*x
p = f(5)
print(p)

Python lambda function will always return the output of the expression. The following is another example that takes two argument and return the greater one.

Python lambda函数将始终返回表达式的输出。 以下是另一个带有两个参数并返回更大的参数的示例。

The output is:

输出为:

14

Python lambda function is helpful for map or filtering of any list.

Python lambda函数有助于映射或过滤任何列表。

filter()中的Python lambda函数 (Python lambda function in filter())

If I want to filter from a list whether the values in the list are having length of 3. Notice that filter() is a function that takes two arguments. One is a function and the second one is a list of values. The following is the code for it:

如果我想从列表中筛选列表中的值的长度是否为3。请注意filter()是一个带有两个参数的函数。 一个是函数,第二个是值列表。 以下是它的代码:

weekdays = ['sun', 'mon', 'tues', 'wed', 'thurs', 'fri']
days = filter(lambda day: day if len(day)==3 else '', weekdays)
for d in days:
   print(d)

Output will be:

输出将是:

sun
mon
wed

In line 2, the function argument is an anonymous function, which takes arguments from the list of weekdays. And check whether the length of the value is equal to 3 or not. If 3 then return the value else nothing will be returned. And the output is according to what we wanted.

在第2行中,函数参数是一个匿名函数,该函数从工作日列表中获取参数。 并检查值的长度是否等于3。 如果为3,则返回该值,否则将不返回任何内容。 输出是根据我们想要的。

在map()中使用python lambda函数 (Use of python lambda function in map())

We can also use lambda in map(). map is a function that takes two argument one is function, another is a list. The following code is an example which finds the remainder of all the number by 5.

我们也可以在map()中使用lambda 。 map是一个带有两个参数的函数,一个是函数,另一个是列表。 以下代码是一个示例,该示例将所有数字的余数乘以5。

numbers = [ 74, 85, 14, 23, 56, 31,44 ]

remainders = map(lambda num: num%5, numbers)
for i in remainders:
   print(i)

The output will be:

输出将是:

4
0
4
3
1
1
4

Thus you can create anonymous function using the python lambda keyword. For small functions, lambda is helpful like shown above. Python lambda is similar to python list comprehension – both are a way to reduce the number of lines of code.

因此,您可以使用python lambda关键字创建匿名函数。 对于小函数, lambda很有帮助,如上所示。 Python lambda与python列表理解类似–两者都是减少代码行数的方法。

In many other function that takes function as input argument, you can use anonymous function using lambda keyword.

在将函数作为输入参数的许多其他函数中,可以使用使用lambda关键字的匿名函数。

Hope now you will be able to play with the anonymous function and got understanding of lambda function in python. Thank you for reading this article.

希望现在您将能够使用匿名函数并了解python中的lambda函数。 感谢您阅读本文。

翻译自: https://www.journaldev.com/15517/python-lambda

python lambda

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值