>>> g=lambda x:x+1
>>> g(1)
2
>>> g(2)
3
可以这样认为,lambda作为一个表达式,定义了一个匿名函数,上例的代码x为入口参数,x+1为函数体,用函数来表示为:
1 def g(x):
2 return x+1
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
上面例子中的map的作用,非常简单清晰。但是,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的方式更容易理解。
在python中比较相似的有operator.itemgetter,operator.itemgetter的详细内容见本文的最底端