运行环境Windows10Python3.8.2PyFunctional1.3.0实例原料lst=[-2,-1,0,1,2]筛正数(filter)#方法一list(filter(lambdax:x>0,lst))#方法二[xforxinlstifx>0]output[1,2]doubled(map)#方法一list(map(lambdax:x*2,lst))#方法二[x*2forxinlst]output[-4,-2,0,2,4]sum(reduce)#方法一fromfunctoolsimportreducereduce(lambdax,y:x+y,lst)#方法2total=0forxinlst:total+=xtotaloutput0sievepositivenumber+double+sumlst=[-2,-1,0,1,2]fromfunctoolsimportreduce#方法1reduce(lambdax,y:x+y,map(lambdax:x*2,filter(lambdax:x>0,lst)))#方法2total=0forxin[x*2forxin[xforxinlstifx>0]]:total+=xtotal#方法三(使用第三方库PyFunctional)fromfunctionalimportseqseq(lst)\.filter(lambdax:x>0)\.map(lambdax:x*2)\.reduce(lambdax,y:x+y)output6本文来自qbitsnap
