# 一个函数接收另一个函数作为参数,这种函数称之为高阶函数
def sum(a, b, f):
return f(a) + f(b)
print(sum(-2, 5, abs))
print(sum(1.2,1.9,round))
# map对list1中每个元素经过func后再返回
list1 = [1, 2, 3, 4, 5]
def func(x):
return x ** 2
result = map(func, list1)
print(list(result))
# 将list1的前两个值传给func,func的返回值在和下一个list1中的元素进入func直到运行完
import functools
list1 = [1, 2, 3 ,4, 5]
def func(a, b):
return a * b
result = functools.reduce(func, list1)
print(result)
# 将list1中每个元素传给func,将返回值为True的返回
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def func(x):
return x % 2 == 0
result = filter(func, list1)
print(result)
print(list(result))