简介
什么是lambda表达式呢? 简单的说就是一个没有 “return” 语句的函数,或者叫”匿名函数”,看一个简单的lambda表达式例子
lambda x : x + 5
定义
lambda表达式有3个部分,第一部分就是”lambda”关键字的,标明这是一个lambda表达式,然后紧接着第二部分”变量”, 然后”:”后面接着第三部分”返回值”上面的表达式接收值x,然后返回x+5的值,相当于函数
def(n):
return n + 5
看看有多个参数的lambda表达式(返回三个数的和):
lambda x, y, z : x + y + z
那么lambda表达式和函数有什么区别呢, 直观的函数肯定要写更多的代码,而lambda仅仅是一行表达式, 假如我们仅仅是临时一次使用一个简单函数(通常只有1-3行规模的),这个函数再也没有其他的用途, 那么选择单独定义一个函数显然是不聪明的,因为要更多的代码,额外的命名. 因此lambda就能很愉快的解决这个问题.
还有一个特点就是lambda表示式是可以赋值的,并可以在函数中构造并返回, 看如下例子
g = lambda x : x + 5
这里’g’就相当于是lambda表达式”lambda x : x + 5”, 然后像函数一样使用即可:
x = 5
n = g(x)
print(n)#结果输出10
看看通过参数构造不同的lambda表达式:
def make_increament(n):
return lambda x: x + n
f = make_increament(10)#构造为"lambda x : x + 10", 并返回给f
g = make_increament(100)#构造为"lambda x : x + 100", 并返回给g
print(f(5))#输出15
print(g(5))#输出105
其他看起来比较叼的应用
foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print(list(filter(lambda x: x % 3 == 0, foo)))#过滤出能被3整出的数
#输出[18, 9, 24, 12, 27]
print(list(map(lambda x: x * 10, foo)))#将列表中的每个数乘上10
#输出[20, 180, 90, 220, 170, 240, 80, 120, 270]
from functools import reduce #python34要从tools中导入reduce()函数
print(reduce(lambda x, y: x + y, foo))#求列表的和
#输出139
sentence = "It is raining cats and dogs"
lens = list(map(lambda x: len(x), sentence.split()))#得到每个单词的长度
print(lens)
#输出[2, 2, 7, 4, 3, 4]
filter(), map(), reduce()的作用简介
filter()函数的功能相当于过滤器。调用一个布尔函数bool_func来迭代遍历每个seq中的元素;返回一个使bool_seq返回值为true的元素的序列。
map()函数处理一个列表里的所有元素,可以使用一个for循环来完成这个工作。但是Python内置的map函数可以帮我们的忙,它接受函数和列表作为参数,然后返回函数处理之后的列表:
reduce()函数对sequence中的item顺序迭代调用function,如果有starting_value,还可以作为初始值调用
附上python34文档对于上述三个函数的说明
map(function, iterable, …)
Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see
functools.reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.
filter(function, iterable)
Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.