map
函数和 reduce
函数是 Python 中的内置函数,分别用于映射操作和归纳操作。
map
函数接受一个函数和一个可迭代对象(例如列表),并将该函数映射到可迭代对象的每个元素上。最终返回一个由映射结果组成的新可迭代对象。
示例代码:
def square(x):
return x * x
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(square, numbers))
print(squared_numbers) # [1, 4, 9, 16, 25]