首页 > 代码库 > Map,Filter 和 Reduce

Map,Filter 和 Reduce

Map会将一个函数映射到一个输入列表的所有元素上

map(function_to_apply, list_of_inputs)

items = [1, 2, 3, 4, 5]

squared = list (map(lambda x: x**2, items))

number_list = range(-5, 5)

less_than_zero = filter(lambda x: x < 0, number_list)

from functools import reduce

product = reduce( (lambda x, y: x * y), [1, 2, 3, 4] )

 

Map,Filter 和 Reduce