filter(func,iter) 只能處理一個參數(iter),僅僅將滿足func方法的數值過濾出來
如:
a = [1,2,3,4,5]
list(filter(lambda x:x>2,a))
輸出結果為: [3,4,5]
map(func,iter1,iter2,..) 可以處理多個iter,實現通過func方法對iter1,iter2,..進行處理
如:
reduce(func,iter,init):僅能處理一個iter,init為初始化值,執行順序為:先將每個iter內部第一個值和init進行func處理,處理的結果再與iter第二個值進行func處理,直到結束。
如:
首先加載reduce模塊:
from functools import reduce
reduce(lambda x, y: x + y, [2, 3, 4, 5, 6], 1)
結果為21 執行順序為---->( (((((1+2)+3)+4)+5)+6) )
reduce(lambda x, y: x + y, [2, 3, 4, 5, 6])
結果為20