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+bprint (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匿名函数

python匿名函数_Python匿名函数相关推荐

  1. python中的匿名函数_python匿名函数

    文章导读: 以前自己一直没搞明白Python中的匿名函数,现在拿这个问题基本上搞明白了,拿自己的理解整成一篇文章,附带大量例子,让其更加好理解. 在编程语言中,函数的应用: 1. 代码块重复,这时候必 ...

  2. python用psf函数_Python 嵌套函数(高级用法)

    Python 嵌套函数(高级用法) 一.嵌套函数(高级用法) 1.嵌套函数 函数的嵌套调用是在"函数调用中再调用其他函数".也就是说:函数嵌套允许在一个函数中调用另外一个函数.如下 ...

  3. python中factor函数_Python入门-函数

    函数 在维基百科上函数式这样描述的: 函数在数学中为两集合间的一种对应关系:输入值集合中的每项元素皆能对应唯一一项输出值集合中的元素. 此处的函数区别于我们数学上的函数,在编程世界中,函数(Funct ...

  4. python圆形代码_python圆形函数

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! python3 函数函数文字定义:函数是组织好的,可重复使用的,用来实现单一,或 ...

  5. 用python写一个函数_Python基础-函数篇

    1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 函数与函数式编程 1.面向对象: 华山派----> ...

  6. python莫比乌斯环_python基础|函数

    1 函数 在python中的函数,内置函数有很多,如:int(), str(), list(), dict(), set() 等内置整形函数,bool()内置布尔值函数,len()内置长度计算函数 , ...

  7. python del用法_python del()函数用法 -电脑资料

    示例程序如下: >>> a = [-1, 3, 'aa', 85] # 定义一个list >>> a [-1, 3, 'aa', 85] >>> ...

  8. python 封装函数_python封装函数

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! python函数教程函数本身其实就相当于一个集装箱,她负责把我们之前写的那些代码 ...

  9. python实现排序函数_Python排序函数的使用方法详解

    Python排序函数完美体现了Python语言的简洁性,对于List对象,我们可以直接调用sort()函数(这里称为"方法"更合适)来进行排序,而对于其他可迭代对象(如set,di ...

最新文章

  1. linux c下,从路径名中分离文件名
  2. 单招计算机考试知识点,单招考试数学必背知识点(11页)-原创力文档
  3. Django(part25)--字段查询
  4. android实现重复动画,android – 多次重复AnimatorSet动画
  5. mysql varchar,bigint,char三种类型性能的比较
  6. 卫星星历和历书的区别
  7. 苹果手机微信语音没声音怎么回事_【云喇叭】微信+支付宝收款语音播报音箱一体机,播报声音大,嘈杂环境也听得见,自带流量卡可连WiFi,无需蓝牙,手机不在店里也播报...
  8. java识别手写文字_Java 实现OCR 识别图像文字(手写中文)----tess4j
  9. 关于java构造函数 的错误 there is no default constructor available in ...
  10. 工作中沟通和执行力的重要性-开发、项目经理、需求之间的矛盾冲突
  11. 《计算机软件著作权》申请注意事项
  12. H5拖拽地址-高德地图VUE版
  13. 论文阅读Targetless Calibration of LiDAR-IMU System Based on Continuous-time Batch Optimization(含代码解读)
  14. SPH方法计算流体表面张力
  15. 微信小程序多次跳转后不能点_微信小程序运营需要先知道它的规则
  16. 山西经济林栽培技术章节考试题
  17. MW150UH驱动程序Linux,MW150UH V2.0_20170607驱动程序
  18. Python | encode中的unicode-escape和raw_unicode_escape
  19. python读取excel并绘图
  20. google-sdk-动态更新手机主题(简单版)

热门文章

  1. this 自引用指针
  2. 微软云平台 Azure简介 (三)Windows Azure 存储概述
  3. 设计模式--适配器1模式实现C++
  4. 网上Silverlight项目收集
  5. pip “Cannot uninstall ‘pip包‘. It is a distutils installed project...“ 解决方法
  6. [转载] 当心掉进Python多重继承里的坑
  7. [转载] python中import问题
  8. Nessus虚拟机的几个问题解决办法
  9. (1)数据库和MySql初步认识
  10. lua循环,减少不必要的循环