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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java怎么安装 详细教程来了 附带安装包

    Java怎么安装 详细教程来了 附带安装包首先,有很多小伙伴不会java环境搭建,java不需要安装,java是绿色软件,尤其是openjdk是绿色软件,你解压缩即可食用,步骤如下:1)去injdkcn上下载openjdk或者gaa,都可以,下载之后,解压缩2)去idea官方网站下载免费的,社区版的idea,对,就是右边色的那个版本,别下错,下成左边蓝色版本,那个是收费的,下载下来之后,安装ide3)打开刚刚安装好的ide,然后就可以开始写java了,当ide问你,jdk在哪里的时候,你就把你刚刚解压缩后的位置告诉ide就可以了

    2022年7月9日
  • Python全国二级等级考试(2019)

    Python全国二级等级考试(2019)

    2022年2月8日
  • jetbrains rider 激活码【2021免费激活】

    (jetbrains rider 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~1STL…

    2022年3月27日
  • JDK的下载、安装和环境配置教程(2021年,win10)

    JDK的下载、安装和配置JDK的下载输入下载地址:https://www.oracle.com/downloads/往下拉,看到DeveloperDownloads,点击java点击Java(JDK)forDevelopers选择你想要下载的jdk版本点击JDKDownloads,往下拉,(有可能会提示先登录账号,才能下载。没账号的可以注册一个)我的系统是Windows的64位,个人偏爱压缩版,所以选择了jdk-14.0.1_windows-

    2022年4月7日
  • pip怎么卸载安装包_pip删除安装包

    pip怎么卸载安装包_pip删除安装包1、pip下载安装1.1pip下载进入https://pypi.python.org/pypi/pip,下载.tar.gz压缩包1.2Linux安装pip#tar-xzvfpip-1.5.4.tar.gz解压#cdpip-1.5.4进入解压文件#pythonsetup.pyinstall安装1.3升级pippython-mpipinstall–upgradepip2.pi…

    2022年10月9日
  • 以太坊钱包开发「建议收藏」

    以太坊钱包开发「建议收藏」目前的公链项目,影响力最大的应该就数以太坊和比特币了,其他的多数公链,基本上都是借鉴了以太坊和比特币公链项目而设计开发的。了解区块链的人都知道,比特币和以太坊这两个公链项目的差距还是挺大的,故而他们的钱包开发也是特别不一样的。本章我们将详细讲解以太坊钱包原理和开发流程,涉及到的内容有以下这些:依托钱包节点方式开发钱包,但这种方式的缺点就keystore生成存放到区块的节点上;非确定性以…

    2022年8月30日

发表回复

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

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