python匿名函数_Python匿名函数

Python匿名函数是一种无名称的函数,常用于小范围的程序中。它们由`lambda`关键字定义,通常用于一次性任务,如在`map()`和`filter()`中改变列表。本文介绍了匿名函数的用途,包括如何通过它们增加列表数值和查找两个列表的公共元素。
摘要由CSDN通过智能技术生成

python匿名函数

Welcome to Python anonymous function tutorial. In the previous tutorial we learned about Python Recursion.

欢迎使用Python匿名函数教程。 在上一教程中,我们了解了Python递归

Python匿名函数 (Python Anonymous Function)

Python Anonymous function is a function which has no name. Therefore, we can use Python Anonymous function for a small scope of program. Normally, we define Python function using def keyword. But we define anonymous function using lambda keyword. The basic structure of an anonymous function is given below.

Python匿名函数是没有名称的函数。 因此,我们可以在较小范围的程序中使用Python匿名函数。 通常,我们使用def关键字定义Python函数。 但是我们使用lambda关键字定义匿名函数。 匿名函数的基本结构如下。

lambda arguments : expression

Look closely, that while taking one or more arguments the anonymous function has only one expression. So, if we want to make a function which will calculate sum of two number. The following code will do so;

仔细观察,匿名函数在接受一个或多个参数时 只有一个表达式 。 因此,如果我们要创建一个将计算两个数字之和的函数。 以下代码将这样做;

def sum ( a, b ):
        return a+b

print (sum(1, 2))
print (sum(3, 5))

But, we can convert this function to anonymous function. The code will be like this

但是,我们可以将此函数转换为匿名函数。 代码将像这样

sum = lambda a,b: (a+b)

print (sum(1,2))
print (sum(3,5))

为什么要使用Python匿名函数 (Why Should We Use Python Anonymous Function)

It may seem that we can replace an anonymous function with a regular function. Then, why should we use an anonymous function? Firstly, you may need to do a single task repeatedly across your scope. Therefore, you need a temporary function to do that. With this intension, lambda/anonymous function can help you. Because, anonymous function is valid in between the scope while a regular function is valid in the program.

似乎我们可以用常规函数代替匿名函数。 那么,为什么要使用匿名函数呢? 首先,您可能需要在整个范围内重复执行一项任务。 因此,您需要一个临时功能来执行此操作。 有了这种内涵,lambda /匿名函数可以为您提供帮助。 因为,匿名函数在范围之间是有效的,而常规函数在程序中是有效的。

表达或陈述 (Expression or Statement)

The main confusion about implementing an anonymous function is, not having the ability of differentiating between an expression and a statement. Basically, a statement returns nothing while an expression produces at least one value. Simple mathematical expression like addition, multiplication, division are expressions and can be used in lambda/anonymous function. So, you have to add an expression so that the result of the expression can be returned through the lambda/anonymous function.

关于实现匿名函数的主要困惑是,没有区分表达式和语句的能力。 基本上,一条语句不返回任何内容,而表达式产生至少一个值。 简单的数学表达式(例如加法,乘法,除法)是表达式,可以在lambda /匿名函数中使用。 因此,您必须添加一个表达式,以便可以通过lambda / anonymous函数返回表达式的结果。

Lambda /匿名函数的一些用法 (Some Use of Lambda/Anonymous Function)

Now, we are going to see some common use of lambda. By using map() and filter() function we can change a list using anonymous function. For example, we want increment all the numbers in a list by n. To do so, the code should be like this

现在,我们将看到lambda的一些常用用法。 通过使用map()filter()函数,我们可以使用匿名函数更改列表。 例如,我们希望将列表中的所有数字增加n。 为此,代码应如下所示

#initial list
l = [1,2,3,4,5,6,7,8]

#print the initial list
print("The initial list is: ")
print(l)

#choose n
n = int(input('Enter the value of n: '))

#increment all the values of the list by using map with help of lambda
l = list(map(lambda x: x+n , l))

#print the changed list
print("The changed list is: ")
print(l)

The output will be

输出将是

Similarly, if we want to store the common elements of two list into new list, we can use filter. To check if the an element is in List2, we used lambda function. The example code is given below

同样,如果要将两个列表的公共元素存储到新列表中,则可以使用过滤器。 为了检查a元素是否在List2中,我们使用了lambda函数。 示例代码如下

#initialize list1
list1 = [1,2,3,4,5,6,7,8]

#print list1
print("List1:", end = ' ')
print(list1)

#intialize list2 for select numbers
list2 = [2,3,5,9,12,14]

#print list2
print("List2:", end = ' ')
print(list2)

'''
compare using lambda function if the value is in list2, if yes then filer the result and assign to list3
'''
list3 = list(filter(lambda x: x in list2 , list1))

#print the changed list
print"List3 (List1 intersect List2 ):", end=' ')
print(list3)

The output of the above program will be

上面程序的输出将是

So, that’s all about Python Anonymous Function. Hope that, Python Anonymous Function is clear to you now. For any further query, feel free to use the comment box. Good Luck!

所以,这一切都与Python匿名函数有关。 希望Python匿名函数现在对您很清楚。 对于任何其他查询,请随时使用注释框。 祝好运!

翻译自: https://www.journaldev.com/14288/python-anonymous-function

python匿名函数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值