# map()是python内置的高阶函数
# 它接收一个函数f和一个list,并通过把函数f依次作用在list的每个元素上,
# 得到一个新的list并返回
# reduce()函数也是python内置的一个高阶函数
# reduce()函数接收的一个参数和map()类似,一个函数f,一个list,
# 但行为和map()不同,reduce()传入的函数f必须接收两个参数,
# reduce()对list的每个元素反复调用函数f,并返回最终结果值
a = [1, 2, 3, 4]
print(map(lambda x: x * x, a)) # <map object at 0x0000011BC18A9128>
for item in map(lambda x: x * x, a): # 匿名函数求平方
print(item) # 1 4 9 16
# 还可以通过列表解析
print([item for item in map(lambda x: x * x, a)]) # [1, 4, 9, 16] 打印一个列表
# reduce()
from functools import reduce
b = reduce(lambda x, y: x + y, a) # 匿名函数作用:求和
print(b) # 10