一:map函数
def addone(x): return x+1def changeList(func,list): newList=[] for i in list: res=func(i) newList.append(res) return newList#不使用lambda函数qq=changeList(addone,[1,2,3])print(qq)#使用lambda函数print(changeList(lambda x:x+1,[1,2,3]))#使用map函数print(list(map(lambda x:x+1,[1,2,3])))
二:filter函数
def test(x): return x.startswith("a")def filter_test(func,list): newList=[] for i in list: if not func(i): newList.append(i) return newList#不使用lambda函数qq=filter_test(test,["aer","dsa","fds"])print(qq)#使用lambda函数print(filter_test(lambda x:x.startswith("a"),["aer","dsa","fds"]))#使用filter函数print(list(filter(lambda x:not x.startswith("a"),["aer","dsa","fds"])))
三:reduce函数
from functools import reducedef multi(x,y): return x*ydef reduce_test(func,x,init=None): if init is None: res=x.pop(0) else: res=init for num in x: res=func(res,num) return res#不适用lambda表达式print(reduce_test(multi,[1,2,3,4,5,6]))#使用lambda表达式print(reduce_test(lambda x,y:x*y,[1,2,3,4,5,6]))#reduce函数print(reduce(lambda x,y:x*y,[1,2,3,4,5,6]))