与正常写法相比,使用匿名函数相当简洁
- map() 遍历所有
<code>a = [1, 2, 3]
x = []
for each in a:
x.append(each+1)
print(x)
### 使用map(func, iterable)
print(list(map(lambda x: x+1, a)))/<code>
- reduce(func, seq) 积累每次计算的值
<code>def num(x, y):
return x + y
print(reduce(num, [1, 2, 3, 4]))
--------------------------
print(reduce(lambda x, y: x*y, [1, 2, 3, 4]))/<code>
- filter(func, iterable) 过滤满足条件的值
<code>print(list(filter(lambda x: x%2==0,range(10))))/<code>
閱讀更多 程序員亮哥 的文章