参考1
参考2
lambda函数也叫匿名函数,即,函数没有具体的名称。先来看一个最简单例子:

def f(x):return x**2
print f(4)

Python中使用lambda的话,写成这样

g = lambda x : x**2
print g(4)

lambda表达式在很多编程语言都有对应的实现。比如C#:

var g = x => x**2
Console.WriteLine(g(4))

那么,lambda表达式有什么用处呢?很多人提出了质疑,lambda和普通的函数相比,就是省去了函数名称而已,同时这样的匿名函数,又不能共享在别的地方调用。其实说的没错,lambda在Python这种动态的语言中确实没有起到什么惊天动地的作用,因为有很多别的方法能够代替lambda。同时,使用lambda的写法有时显得并没有那么pythonic。甚至有人提出之后的Python版本要取消lambda。

回过头来想想,Python中的lambda真的没有用武之地吗?其实不是的,至少我能想到的点,主要有:

  1. 使用Python写一些执行脚本时,使用lambda可以省去定义函数的过程,让代码更加精简。

  2. 对于一些抽象的,不会别的地方再复用的函数,有时候给函数起个名字也是个难题,使用lambda不需要考虑命名的问题。

  3. 使用lambda在某些时候让代码更容易理解。

lambda基础

lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值。lambda语句构建的其实是一个函数对象,见证一下:

g = lambda x : x**2
print g<function <lambda> at 0x00AFAAF0>>>> def make_incrementor (n): return lambda x: x + n
>>>
>>> f = make_incrementor(2)
>>> g = make_incrementor(6)
>>>
>>> print f(42), g(42)
44 48
>>>
>>> print make_incrementor(22)(33)
55

C#3.0开始,也有了lambda表达式,省去了使用delegate的麻烦写法。C#中的lambda表达式关键字是=>,看下面的一个例子:

var array = new int[] {2, 3, 5, 7, 9};
var result = array.Where(n => n > 3); // [5, 6, 9]

C#使用了扩展方法,才使得数组对象拥有了像Where,Sum之类方便的方法。Python中,也有几个定义好的全局函数方便使用的,他们就是filter, map, reduce。
复制代码

>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>>
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce(lambda x, y: x + y, foo)
139

非lambda不可?

上面例子中的map的作用,和C#的Where扩展方法一样,非常简单方便。但是,Python是否非要使用lambda才能做到这样的简洁程度呢?在对象遍历处理方面,其实Python的for..in..if语法已经很强大,并且在易读上胜过了lambda。比如上面map的例子,可以写成:

print [x * 2 + 10 for x in foo]

非常的简洁,易懂。filter的例子可以写成:

print [x for x in foo if x % 3 == 0]

同样也是比lambda的方式更容易理解。

所以,什么时候使用lambda,什么时候不用,需要具体情况具体分析,只要表达的意图清晰就好。一般情况下,如果for..in..if能做的,我都不会选择lambda。

lambda broken?

在数学教学中,经常会使用到lambda,比如有一位老兄就遇到这样一个问题。他想创建一个函数数组fs=[f0,…,f9] where fi(n)=i+n. 于是乎,就定义了这么一个lambda函数:

fs = [(lambda n: i + n) for i in range(10)]

但是,奇怪的是,

>>> fs[3](4)
13
>>> fs[4](4)
13
>>> fs[5](4)
13

结果并没有达到这位老兄的预期,预期的结果应该是:
复制代码

>>> fs[3](4)
7
>>> fs[4](4)
8
>>> fs[5](4)
9

问题其实出在变量i上。上面的代码换个简单的不使用lambda的缩减版本:

i = 1
def fs(n):return n + i
print fs(1) # 2i = 2
print fs(1) # 3

可见,上面没有达到预期的原因是lambda中的i使用的是匿名函数外的全局变量。修改一下:
复制代码

fs = [(lambda n, i=i : i + n) for i in range(10)]
>>> fs[3](4)
7
>>> fs[4](4)
8
>>> fs[5](4)
9

应用
The following example is one way to compute prime numbers in Python (not the most efficient one, though)

>>> nums = range(2, 50)
>>> for i in range(2, 8):
...     nums = filter(lambda x: x == i or x % i, nums)
...
>>> print nums
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

In the following example, a sentence is split up into a list of words, then a list is created that contains the length of each word.

>>> sentence = 'It is raining cats and dogs'
>>> words = sentence.split()
>>> print words
['It', 'is', 'raining', 'cats', 'and', 'dogs']
>>>
>>> lengths = map(lambda word: len(word), words)
>>> print lengths
[2, 2, 7, 4, 3, 4]

I think it doesn’t need any further explanation, the code is practically self-documenting.

Of course, it could all be written in one single statement. Admittedly, this is somewhat less readable (not much, though).

>>> print map(lambda w: len(w), 'It is raining cats and dogs'.split())[2, 2, 7, 4, 3, 4]

Here’s an example from the UNIX scripting world: We want to find all mount points in our file system. To do that, we execute the external “mount” command and parse the output.

>>> import commands
>>>
>>> mount = commands.getoutput('mount -v')
>>> lines = mount.splitlines()
>>> points = map(lambda line: line.split()[2], lines)
>>>
>>> print points
['/', '/var', '/usr', '/usr/local', '/tmp', '/proc']

The getoutput function from the commands module (which is part of the Python standard library) runs the given command and returns its output as a single string. Therefore, we split it up into separate lines first. Finally we use “map” with a lambda function that splits each line (on whitespace, which is the default) and returns just the third element of the result, which is the mountpoint.

Again, we could write all of that in one single statement, which increases compactness but reduces readability:

When writing “real-world” scripts, it is recommended to split up complex statements so that it is easier to see what it does. Also, it is easier to make changes.

However, the task of splitting up the output of a command into a list of lines is very common. You need it all the time when parsing the output of external commands. Therefore, it is common practice to include the split operation on the getoutput line, but do the rest separately. This is a good trade-off between compactness and readability:

>>> lines = commands.getoutput('mount -v').splitlines()
>>>
>>> points = map(lambda line: line.split()[2], lines)
>>> print points
['/', '/var', '/usr', '/usr/local', '/tmp', '/proc']

An even better idea is probably to write a small function for that task, which encapsulates the job of running the command and splitting the output.

On a related note, you can also use so-called list comprehensions to construct lists from other lists. Sometimes this is preferable because of efficiency or readability. The previous example could very well be rewritten using a list comprehension:

>>> lines = commands.getoutput('mount -v').splitlines()
>>>
>>> points = [line.split()[2] for line in lines]
>>> print points
['/', '/var', '/usr', '/usr/local', '/tmp', '/proc']

In many cases, you can use list comprehensions instead of map() or filter(). It depends on the situation which one should be preferred.

Note: The commands module is deprecated in newer versions of Python (though it still works in all 2.x versions). Instead, the subprocess module should be used which is available since Python 2.4. The commands.getoutput() function can be replaced by subprocess.check_output(). Please refer to the documentation for details.

lambda:Python的匿名函数相关推荐

  1. python声明匿名函数_举例讲解Python的lambda语句声明匿名函数的用法

    所谓匿名函数,即是不需要定义函数,像表达式一样使用,不需要函数名(很多时候名字让我很困扰),一些简单的函数简单化, 举个例子 我需要两个整数相加的函数,通常是这么定义的def add(x, y): r ...

  2. python中匿名函数的关键字_Python匿名函数(lambda函数)

    匿名函数-- 一行函数 lambda -- 关键字 x是普通函数的形参(位置,关键字...)可以不接收参数(x可以不写) :x是普通函数的函数值(只能返回一个数据类型)(:x返回值必须写) 1)此函数 ...

  3. Python中lambda表达式_匿名函数

    lambda表达式和匿名函数 lambda表达式 ​ lambda表达式可以用来声明匿名函数,实际生成一个函数对象. ​ lambda表达式只允许包含一个表达式,该表达式的计算结果就是函数的返回值. ...

  4. python在匿名函数作和_跟光磊学Python开发-匿名函数函数和高阶函数

    跟光磊学Python开发-匿名函数函数和高阶函数 跟光磊学Python开发-匿名函数函数和高阶函数跟光磊学Python开发 匿名函数 匿名函数就是函数定义时没有名字的函数,也称为匿名表达式. 普通函数 ...

  5. python定义匿名函数关键字_Python(11):Python函数基础(定义函数、函数参数、匿名函数)...

     函数先定义函数,后调用 一.定义函数: 1.简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数. ...

  6. (二十)python 3 匿名函数

    匿名函数lambda Python使用lambda关键字创造匿名函数.所谓匿名,意即不再使用def语句这样标准的形式定义一个函数.这种语句的目的是由于性能的原因,在调用时绕过函数的栈分配.其语法是: ...

  7. Python中匿名函数与内置高阶函数详解

    大家好,从今天起早起Python将持续更新由小甜同学从 初学者的角度 学习Python的笔记,其特点就是全文大多由 新手易理解 的 代码与注释及动态演示 .刚入门的读者千万不要错过! 很多人学习pyt ...

  8. python的匿名函数返回值_Python匿名函数返回值输出问题望指点

    该段的匿名函数作为返回值返回,那请问可以输出对应的值吗?小白途中!望各位指点! def build(x, y): return lambda: x * x + y * y 1.匿名函数在一定意义上应该 ...

  9. Python 之匿名函数和偏函数

    匿名函数与偏函数 匿名函数 Python允许使用lambda关键字创造匿名函数,lambda表达式用于定义匿名函数,它返回可调用的函数对象,语法如下: lambda arg1, arg2, - : e ...

  10. Python中匿名函数详解

      匿名函数是指没有名字的函数,应用在需要一个函数,但是又不想费神去命名这个函数的场合.通常情况下,这样的函数只使用一次.在Python中,使用 lambda 表达式创建匿名函数,其语法格式如下: r ...

最新文章

  1. C++自动生成的成员函数
  2. CodeForces - 850C Arpa and a game with Mojtaba(博弈+sg函数)
  3. R软件中 文本分析安装包 Rjava 和 Rwordseg 傻瓜式安装方法四部曲
  4. CSS之REM和EM的区别
  5. html5中音频、视频标签、自定义播放器常用属性及方法、全屏操作、新增属性兼容问题
  6. RocketMq 消费消息的两种方式 pull 和 push
  7. java8获取当前时间并格式化
  8. memcpy与memmove的区别
  9. python编程(类的使用)
  10. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_2_BufferedOutputStream_字节缓冲...
  11. 【Alpha】Scrum Meeting 10
  12. 三十不惑:情商智商决定我能走多快,德商(基本素质)决定我能走多远
  13. 快速批量删除新浪微博内容
  14. 人脸识别-----Olivetti Faces人脸数据集合处理
  15. 每天睡前按摩腹部,坚持一个月有什么好处?
  16. 50个超酷的Photoshop的渐变画笔
  17. css transform导致字体像素模糊的问题解决办法(多种方法,亲测有效)
  18. 2021研面准备 -- 计算机网络知识点整理(一)概述
  19. HTML/CSS IE6、7兼容性问题、bug总汇
  20. cesium 实现雨雪雾效果

热门文章

  1. 7-4 计算油费 (15 分)
  2. 龙虎榜小红牛股票池@2021新年开源福利
  3. c语言程序设计移动字母,c语言程序设计word版.pdf
  4. 虚拟内存是什么?它有什么用?又该如何设置呢?
  5. 25 Minutes
  6. 我的个人网站——“甜果网”上线了!
  7. day04_列表元组字符串的操作字典
  8. 谁动了我的IP地址?
  9. 信息技术租赁与融资行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  10. 安卓小白如何制作一个精简ROOT的卡刷ROM?小白制作ROM包详细图文教程