python中的Lambda表达式/函数

Explanation:

说明:

In python, there is a function named Lambda. Lambda function is an anonymous function - that means the function which does not have any name.

在python中,有一个名为Lambda的函数。 Lambda函数是一个匿名函数-表示该函数没有任何名称。

When we declare a function, we use "def" keyword to define a function with a suitable function name. But lambda function does not require that.

当我们声明一个函数时,我们使用“ def”关键字来定义一个具有合适函数名的函数。 但是lambda函数不需要。

Syntax to declare a lambda expression/function:

声明lambda表达式/函数的语法:

    lambda parameterlist : expression

Where,

哪里,

  • lambda is a reserved word that defines a lambda expression.

    lambda是定义lambda表达式的保留字。

  • parameterlist is a comma-separated list of parameters as you would find in the function definition (but notice the lack of parentheses).

    参数列表是用逗号分隔的参数列表,您可以在函数定义中找到(但请注意缺少括号)。

  • expression is a single Python expression. The expression cannot be a complete statement.

    expression是单个Python表达式。 该表达式不能是完整的语句。

Note:

注意:

  • This function can take as many arguments as it needs but the expression must be single.

    该函数可以根据需要使用任意数量的参数,但表达式必须为单个。

  • You are free to use the lambda function wherever you want to use.

    您可以随时随地使用lambda函数。

Example 1: Elaborating about the difference between the Simple function and the Lambda function.

示例1:详细说明简单函数和Lambda函数之间的区别。

# simple approach we use to define the area of rectangle:
# Python code to illustrate are of rectangle   
# showing difference between def() and lambda(). 
def area(l,b): 
return l*b; 
g = lambda l,b: l*b 
print('lambda function:',g(7,4)) 
#calling the function
print('Via Simple function:',area(7,4)) 

Output

输出量

lambda function: 28
Via Simple function: 28

Explanation of the code:

代码说明:

Here, both of the functions return the same area of a rectangle, But while using the def keyword we need to do all the function of the function and also return it. But same in lambda we just need to give the arguments and the expression which returns the answer accordingly. As it does not include any return statement. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions.

在这里,两个函数都返回矩形的相同区域,但是在使用def关键字时,我们需要完成函数的所有功能并返回它。 但是在lambda中,我们只需要提供参数和相应的表达式即可返回答案。 由于它不包含任何return语句。 我们还可以将lambda定义放在需要函数的任何地方,而我们根本不必将其分配给变量。 这就是lambda函数的简单

Example2: How we can use lambda function different forms?

例2:我们如何使用lambda函数使用不同的形式?

print('Ways to use and declare lambda functions:')
# simply defining lambda function 
#example - 1
g=lambda x, y: 3*x + y
print('Ex-1:',g(10,2))
#example - 2
f=lambda x, y: print('Ex-2:',x, y)
f(10,2)
#example - 3
h=lambda x, y: 10 if x == y else 2
print('Ex-3:',h(5,5))
#example - 4
i=lambda x, y: 10 if x == y else 2
print('Ex-4:',i(5,3))

Output

输出量

Ways to use and declare lambda functions:
Ex-1: 32
Ex-2: 10 2
Ex-3: 10
Ex-4: 2

具有filter(),map(),reduce()的Lambda函数 (Lambda functions with filter() , map() , reduce())

lambda() function can be used with the other functions like filter() , map() etc.

lambda()函数可与其他函数(例如filter() , map()等)一起使用 。

filter(): It takes the list of arguments. This function filters out all the elements in the given list which return True for the function.

filter():获取参数列表。 该函数过滤掉给定列表中所有返回该函数True的元素。

map(): map() function in python used to map all the elements of the list with its condition by the function or by the lambda function.

map(): python中的map()函数,用于通过函数或lambda函数映射列表中的所有元素及其条件。

Syntax:

句法:

    map(function_object, iterable1, iterable2,...)

Example 3: Lambda Function using the filter() , map() , reduce()

示例3:使用filter(),map(),reduce()的Lambda函数

#Using the filter() , map() with lambda() function.
# Python code to illustrate 
# filter() with lambda() 
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] 
li = list(filter(lambda x: (x%2 == 0) , li)) 
print('By using filter :',li)
# Python code to illustrate  
# map() with lambda()  
# to get double of a list.
l=[{'name':'includehelp', 'star':10},{'name':'yash', 'star':8},{'name':'sanjeev', 'star':8}]
for output1  in (map(lambda x : x['name'],l)):
print('maping name:',output1)
for output2 in (map(lambda x : x['star']*10,l)):
print('maping star:',output2)

Output

输出量

By using filter : [22, 54, 62]
maping name: includehelp
maping name: yash
maping name: sanjeev
maping star: 100
maping star: 80
maping star: 80

翻译自: https://www.includehelp.com/python/lambda-expression-function.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546252.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IDEA 正式版终于支持中文版和 JDK 直接下载了(太方便了)附介绍视频

IDEA 2020.1 经过了漫长的打磨终于发布正式版了,而这次的版本不止直接支持 Java 14,还带来了两个重量级的功能,官方中文版支持和 JDK 直接下载。 在之前的开发中,当我们需要下载 JDK 时,通常的步骤是这样的&#xff1…

HoughLine变换

对于HoughLine变换,有两种方法,标准霍夫变换(SHT)用的矩阵是CV_32FC2,用极坐标法记录直线,而累积概率霍夫变换(PPHT)用的是CV_32FC核心函数:cvCvtColor,cvHoughLines2&am…

华为交换机、路由器配置静态路由实现不同网段通信

一、eNSP模拟器仿真图 二、目标要求 在主核心交换机上配置vlanif10 192.168.10.254/24,vlanif20 192.168.20.254/24,vlanif100 192.168.100.1/24,PC1:192.168.10.1能ping通PC2:192.168.20.1,也能ping通PC3:192.168.30.1,交换机和路由器使用静态路由。 三、华为交换机配置…

答读者问:学历不高,要如何破局?

今天读者群在激烈讨论学历是否重要,有的朋友说非常重要,也有人说并没有那么重要。有读者问:“我是专科毕业,我需要读在职本科或者研究生吗”,也有读者问:“洋哥,三本毕业几年,想辞职…

python中三角函数_Python中的三角函数

python中三角函数Python三角函数/方法 (Python Trigonometric functions/methods) In python programming language, there are some of the built-in functions which are defined in math module – they can be used for Trigonometric calculations, python has following …

华为路由器配置OSPF实现不同网段通信

一、简介 PC1、PC2、LSW1和AR1配置单臂路由,实现PC1能够ping通PC2,具体实现请参考:华为交换机、路由器配置单臂路由实现不同网段通信。下面实现AR1和AR2通过配置OSPF,实现PC1能ping通PC3。 二、华为路由器配置 AR1新增配置: [Huawei]int g0/0/1 [Huawei-GigabitEthernet…

PHP开发框架[国内框架]

1.Thinkphp http://thinkphp.cn/ 2.Brophp http://www.brophp.com/zf/ 由LAMP兄弟连打造 3.WindFramework http://phpwind.github.com/windframework/framework.html 著名论坛程序phpwind推出的php框架 4.SpeedPHP http://www.speedphp.com/ 5.CanPHP http://www.canphp…

史上最全的 SQL 优化方案!建议收藏

在进行MySQL的优化之前,必须要了解的就是MySQL的查询过程,很多查询优化工作实际上就是遵循一些原则,让MySQL的优化器能够按照预想的合理方式运行已。图-MySQL查询过程1优化的哲学 注:优化有风险,涉足需谨慎a优化可能带…

python 示例_是Python中带有示例的关键字

python 示例Python是关键字 (Python is keyword) is is a keyword (case-sensitive) in python, it is used to check whether two objects are the same objects or not. is是python中的关键字(区分大小写),用于检查两个对象是否相同。 Note: If two variables re…

Linux Podman安装MySQL数据库

1.拉取MySQL镜像 这里拉取官方最新版本镜像 podman pull mysql2.查看本地镜像 podman images可以看到,我们已安装最新版本的mysql镜像 3.运行容器 可以使用以下命令来运行 mysql 容器 podman run -d --name mysql-test -p 4000:3306 -e MYSQL_ROOT_PASSWORD12…

不吹牛逼,撸个注解有什么难的

注解是 Java 中非常重要的一部分,但经常被忽视也是真的。之所以这么说是因为我们更倾向成为一名注解的使用者而不是创建者。Override 注解用过吧?Service 注解用过吧?但你知道怎么自定义一个注解吗?恐怕你会摇摇头,摆摆…

python二分法查找程序_Python程序查找地板划分

python二分法查找程序When we divide a number by another number – division operator (/) return quotient it may be an integer or float. But, when we need quotient without floating number – we can floor division (//) operator, it returns quotient (result) wi…

DOS命令(bat批处理脚本)遍历目录、遍历子目录下的文件、遍历数字和遍历文件内容

1.遍历目录 格式: for /d %%名称 in (路径) do 具体操作脚本测试:创建test.bat文件,*代表test.bat文件所在的当前目录 echo offfor /d %%a in (*) do (echo %%a)pause2.遍历目录和子目录下的文件 格式: for /r "目录路径&…

几位阿里朋友重写的Java并发编程,牛逼了

昨天在黄金时代群里和读者聊机械键盘大 F 的时候,好朋友 cxuan 推了一篇文章,吸引了我的眼球,名叫“太赞了,阿里几位工程师重写了 《Java 并发编程》”,我看完后,直呼“牛逼了”,就想着赶紧推荐…

FFmpeg 2.1 试用(新版支持HEVC,VP9)

2019独角兽企业重金招聘Python工程师标准>>> 前两天帮一位老师转码图像的时候,无意间发现新版FFmpeg竟然支持了下一代编码标准HEVC,以及Google提出的下一代编码标准VP9。真心没想到FFmpeg对下一代的编码标准支持的是如此之快。我还以为这两种…

电源变换适用于非独立源码_适用于非None测试的Python程序

电源变换适用于非独立源码As we have discussed in the previous post (Python None keyword), that "None" is a keyword which can be used to assign a null value to a variable or to check whether a variable contains any value or not. 正如我们在上一篇文章…

Linus shell 在一个脚本中调用另外一个脚本变量

1.新建public.sh文件,并添加以下内容: 2.新建ceshi.sh文件,并添加以下内容: 3.在终端赋予ceshi.sh文件执行权限,并运行该文件。

史上最全的延迟任务实现方式汇总!附代码(强烈推荐)

这篇文章的诞生要感谢一位读者,是他让这篇优秀的文章有了和大家见面的机会,重点是优秀文章,哈哈。 事情的经过是这样的... 不用谢我,送人玫瑰,手有余香。相信接下来的内容一定不会让你失望,因为它将是目前…

linux定时任务生产java服务无法执行问题群友案例

linux定时任务crond export变量问题群友案例来自网友兄弟 北京Grady(254553457) 的总结。1)我写了一个重启resin的脚本,由于业务原因,需要定时在某一个时间重启下resin服务器,于是就在crontab里配置了如下内容:50 17 *…

在Python中执行while循环

Python执行while循环 (Python do while loop) Like other programming languages, do while loop is an exit controlled loop – which validates the test condition after executing the loop statements (loop body). 像其他编程语言一样, do while循环是退出控…