与正常写法相比,使用匿名函数相当简洁
- map() 遍历所有
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)))
- reduce(func, seq) 积累每次计算的值
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]))
- filter(func, iterable) 过滤满足条件的值
print(list(filter(lambda x: x%2==0,range(10))))
本文首发于python黑洞网,博客园同步更新
来源:https://www.cnblogs.com/pythonzhilian/p/12541117.html