One - One Code All

Blog Content

python中的lambda

Python   2010-05-08 11:17:05

python中的lambda


举例如下:

func=lambda x:x+1
print(func(1))
#2
print(func(2))
#3

#以上lambda等同于以下函数
def func(x):
   return(x+1)

lambda作为一个表达式,定义了一个匿名函数,上例的代码x为入口参数,x+1为函数体。在这里lambda简化了函数定义的书写形式。是代码更为简洁,但是使用函数的定义方式更为直观,易理解。

Python中,也有几个定义好的全局函数方便使用的,filter, map, reduce。

 from functools import reduce
 foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
 
 print (list(filter(lambda x: x % 3 == 0, foo)))
 #[18, 9, 24, 12, 27]
 
 print (list(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内置函数reduce()
下一篇:R语言apply()函数用法

The minute you think of giving up, think of the reason why you held on so long.