python lambda拉姆达表达式「建议收藏」

python lambda拉姆达表达式「建议收藏」(lambdax:x+1)(50)51addone=lambdax:x+1addone(50)51sum=lambdax,y:x+ysum(5,9)14

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

>>> lambda x: x + 1
<function <lambda> at 0x000001FC51317840>

看出来,就是一个函数,也是一个表达式。
Because a lambda function is an expression, it can be named. Therefore you could write the previous code as follows:

>>> addone= lambda x:x+1
>>> addone(50)
51

>>> (lambda x:x+1)(50)
51

带2个参数的lambda expression:

>>> sum = lambda x,y :x+y
>>> sum(5,10)
15
>>> 

MAP 是什么 ,WTF is MAP?
map(function, iterable, …)
Return an iterator that applies function to every item of iterable, yielding the results.意思就是把这个函数呢应用到每个iterable 的items上。
看下面的例子

def calculateSquare(n):
  return n*n

numbers = (1, 2, 3, 4)
result = map(calculateSquare, numbers)
print(result)
<map object at 0x000001FC51314DD8>

converting map object to set:

numbersSquare = set(result)
print(numbersSquare)
{16, 1, 4, 9}  从结果可见,就是把calculateSquare函数,应用到了每个numbers上。

既然是把函数应用的每个iterable上,自然会想起我们的lambda函数 :

numbers = (1, 2, 3, 4)
result = map(lambda x: x*x, numbers)
print(result)
<map object at 0x00000181A0A280F0>

# converting map object to set
numbersSquare = set(result)
print(numbersSquare)
<map 0x7fafc21ccb00>
{16, 1, 4, 9}

**

Use of lambda() with filter()

**

The filter() function in Python takes in a function and a list as arguments. This offers an elegant way to filter out all the elements of a sequence “sequence”, for which the function returns True. Here is a small program that returns the odd numbers from an input list:

>>> li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
>>> final_list = list(filter(lambda x: (x%2 != 0) , li))
>>> print(final_list)
[5, 7, 97, 77, 23, 73, 61]
>>> 

Use of lambda() with reduce()

The reduce() function in Python takes in a function and a list as argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list. This is a part of functools module. Example:
注意,reduce已经被搬到了functools
https://docs.python.org/3/library/functools.html#functools.reduce

>>> from functools import reduce
>>> li = [5, 8, 10, 20, 50, 100]
>>> sum = reduce((lambda x, y: x + y), li)
>>> 
>>> sum
193
>>> 

Here the results of previous two elements are added to the next element and this goes on till the end of the list like (((((5+8)+10)+20)+50)+100).

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191880.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab画图函数 增加横纵坐标名称_matlab函数绘图

    matlab画图函数 增加横纵坐标名称_matlab函数绘图1.多子图figuresubplot(2,1,1);plot(data1);title(‘原始信号’);%标题subplot(2,1,2);plot(data1_rebuild);title(‘去噪声后信号’);2.多条信号figureplot(data1,’-r’);holdonplot(data1_rebuild,’-b’);legend(‘原始信号’,’去噪后信…

    2022年9月19日
    2
  • HTML meta refresh 刷新与跳转(重定向)页面

    HTML meta refresh 刷新与跳转(重定向)页面

    2021年9月24日
    59
  • [项目源码]ERP进销存系统

    [项目源码]ERP进销存系统介绍ERP进销存管理系统软件架构核心框架:SpringBoot2.0.0持久层框架:Mybatis1.3.2日志管理:Log4j2.10.0JS框架:Jquery1.8.0UI框架…

    2022年5月24日
    45
  • python爬虫 完整代码

    python爬虫 完整代码这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML图表FLowchart流程图导出与导入导出导入欢迎使用Markdown编辑器你好!这是你第一次使用Markdown编辑器所展示的欢迎页。如果你想学习如何使用Mar

    2022年6月6日
    49
  • 浅谈 MyBatis 缓存

    浅谈 MyBatis 缓存EhCache是一个快速的、轻量级的、易于使用的、进程内的缓存。它支持read-only和read/write缓存,内存和磁盘缓存。是一个非常轻量级的缓存实现,而且从1.2之后就支持了集群,目前的最新版本是2.8。

    2022年5月21日
    41
  • 不能复制文字的网页文字复制怎么办_html循环粘贴

    不能复制文字的网页文字复制怎么办_html循环粘贴网页无法复制文字怎么办?当我们在电脑上需要复制某个网页上的文字时,发现我们不能选择复制粘贴文字,那这种情况该怎么解决呢,网页无法复制文字怎么办,怎么解决网页无法复制粘贴文字情况,下面就和小编一起来看看吧!1.可以使用谷歌浏览器扩展程序AllowCopy解决问题,打开谷歌浏览器的网上应用店,搜索【AllowCopy】;2.然后找到SimpleAllowCopy,点击【添加至Chrome】将其…

    2022年10月9日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号