首页 > 代码库 > lambda表达式

lambda表达式

lambda表达式:

>>> lambda x : 2 * x + 1
<function <lambda> at 0x0000000003039CF8>
>>> g = lambda x : 2 * x +1
>>> g(5)
11

 

filter()表达式:

>>> filter(None, [1, 0, False, True])
[1, True]

>>> def odd(x):
return x % 2

>>> filter(odd, range(10))
[1, 3, 5, 7, 9]

 

map()表达式:

>>> map(lambda x : x * 2, range(10))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

lambda表达式