python map() reduce() filter()函数

b = map(lambda x : x ** 2, a) #计算平方
print(list(b))
c = map(lambda x, y : x + y, [1, 3, 4], [2, 4, 6]) #可以处理多个链表
print(list(c))
def square(x):
    return x ** 2
b = map(square, a)  #也可以自己定义函数
print(list(b))
from functools import reduce #reduce置于functools中
#reduce相当于是一个递归
#计算n的阶乘
print(reduce(lambda x, y : x * y, range(1, 11))) #计算10的阶乘

#filter用来过滤
a = filter(lambda x : x > 5 and x < 8, range(10))
print(list(a))

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_36372879/article/details/80786969