Can you give an example and other examples that show when and when not to use Lambda?

My book gives me examples, but they're confusing.

解决方案

Lambda, which originated from Lambda Calculus and (AFAIK) was first implemented in Lisp, is basically an anonymous function - a function which doesn't have a name, and is used in-line, in other words you can assign an identifier to a lambda function in a single expression as such:

>>> addTwo = lambda x: x+2

>>> addTwo(2)

4

This assigns addTwo to the anonymous function, which accepts 1 argument x, and in the function body it adds 2 to x, it returns the last value of the last expression in the function body so there's no return keyword.

The code above is roughly equivalent to:

>>> def addTwo(x):

... return x+2

...

>>> addTwo(2)

4

Except you're not using a function definition, you're assigning an identifier to the lambda.

The best place to use them is when you don't really want to define a function with a name, possibly because that function will only be used one time and not numerous times, in which case you would be better off with a function definition.

Example of a hash tree using lambdas:

>>> mapTree = {

... 'number': lambda x: x**x,

... 'string': lambda x: x[1:]

... }

>>> otype = 'number'

>>> mapTree[otype](2)

4

>>> otype = 'string'

>>> mapTree[otype]('foo')

'oo'

In this example I don't really want to define a name to either of those functions because I'll only use them within the hash, therefore I'll use lambdas.

python中的方法什么意思,“ lambda”是什么?在Python中是什么意思,最简单的使用方法是什么?...相关推荐

  1. python lambda 判断_在Python的Filter中使用lambda函数时,为何达不到预期效果?

    最近我在学习python的时候也遇到了同样的问题,碰巧看到了这个提问,还是挺有缘分的. 先说结论:直接使用lambda表达式时filter(lambdax:x%n>0, it),n会随着代码的运 ...

  2. python中如何编写代码输入多个数据并把它们放在一个列表中去_这59条编写高质量Python代码的方法你知道吗?...

    这个周末断断续续的阅读完了<Effective Python之编写高质量Python代码的59个有效方法>,感觉还不错,具有很大的指导价值. 下面将以最简单的方式记录这59条建议,并在大部 ...

  3. aws lambda使用_使用python了解AWS Lambda中的多处理

    aws lambda使用 Adding some transparency to the black box 为黑匣子添加一些透明度 Let me start with an observation. ...

  4. python中lambda函数if用法-Python中关于Lambda函数的使用总结

    lambda表达式是一种匿名函数,对应python中的自定义函数def,是定义某个函数时比较高级的一种写法.作为python初学者,本文整理了lambda的一些基本用法和特点. lambda和def的 ...

  5. python文件读取方法read(size)的含义是_在Python中可使用read([size])来读取文件中的数据,如果参数size省略,则读取文件中的()。...

    [单选题]李明在他所属的公司工作五年,每天都很认真地处理繁杂的事情,同事们都夸他认真,但是依然没有建树,这是因为: [多选题]品牌标志的作用表现在 [单选题]新产品开发的第一个阶段是_______. ...

  6. python把坐标写入文本_Python实现将数据写入netCDF4中的方法示例

    本文实例讲述了Python实现将数据写入netCDF4中的方法.分享给大家供大家参考,具体如下: nc文件为处理气象数据文件.用户可以去https://www.lfd.uci.edu/~gohlke/ ...

  7. python中def _init_是什么意思_详细解读Python中的__init__()方法

    __init__()方法意义重大的原因有两个.第一个原因是在对象生命周期中初始化是最重要的一步:每个对象必须正确初始化后才能正常工作.第二个原因是__init__()参数值可以有多种形式. 因为有很多 ...

  8. python文件读取方法read(size)的含义是_在Python中可使用read([size])来读取文件中的数据,如果参数size省略,则读取文件中的()。(4.0分)_学小易找答案...

    [单选题]文本文件存储的是(),由若干文本行组成,通常每行以换行符 '\n' 结尾.(4.0分) [单选题]()属性是返回被打开文件的访问模式.(4.0分) [单选题]重力坝是由砼或( )修筑而成的大 ...

  9. 下列选项中不属于python循环语句的是哪一项_下列选项中,不属于字典操作的方法是哪一项?_学小易找答案...

    [简答题]字符串有哪几种表现形式? [简答题]简述Python程序的执行过程. [单选题]Python 语句x='char';y=2,print(x+y)输出的结果是哪一项?() [编程题]输入三角形 ...

  10. python特征选择的过程_【来点干货】机器学习中常用的特征选择方法及非常详细的Python实例...

    花费了很长时间整理编辑,转载请联系作者授权,违者必究. 特征选择(Feature selection)是在构建预测模型的过程中减少输入变量的一个过程.它是机器学习中非常重要的一步并在很大程度上可以提高 ...

最新文章

  1. 用算法改造过的植物肉,有兴趣试试么?
  2. 内网端口 转发 穿透 工具简介
  3. 命令点无效怎么处理_怎么更好处理闲置包包,买包卖包都要记住这5点
  4. Java项目打包成exe的详细教程
  5. php自动加载什么时候用到,php的自动加载的使用
  6. 《BI那点儿事》数据挖掘各类算法——准确性验证
  7. 报错,o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column ‘org0_.create_by‘ in ‘field list‘
  8. 手机,平板,电脑,超大屏幕 宽度响应式
  9. iPad mini Retina越狱小结【2014年02月06日 - 初稿】
  10. C语言实现BMP格式转RGB格式、YUV格式
  11. Asterisk内核 拾遗
  12. 架构设计案例分析-高速公路收费运营管理平台
  13. 归并排序(C语言版)
  14. 什么是 UI 自动化测试?
  15. bootstrap自学总结不间断更新
  16. Windows10 应用商店打不开问题,报错:Code: 0x80072F7D
  17. 全网首档会员付费网综领跑曲艺文化新形态 爱奇艺《坑王驾到》第二季28日上线...
  18. STC8A单片机应用开发
  19. 全球及中国硅胶收纳袋行业市场需求动态分析及发展趋势预测报告2022-2028年
  20. C# 自动发送邮件被系统当做垃圾邮件退回的处理方法

热门文章

  1. php生成excel(单元格内换行的解决办法)
  2. 自媒体都要“实人实证实名”纳入管控!
  3. vue中实现列表无缝滚动
  4. 原创:没有信用卡如何在iTunes注册帐号?
  5. 2021-02-10 SONiC SAI结构4 L3
  6. 《双十一不一样的技术创新》2016出版 读书笔记
  7. 【转载】解决latex里面的:Bibliography not compatible with author-year citation
  8. 模拟面试训练营:顶尖科技公司的offer等着你
  9. Easyrecovery home15密钥及安装修复硬盘数据教程
  10. 一次糟糕的字节跳动面试经历