更新時(shí)間:2019年10月31日16時(shí)02分 來源:傳智播客 瀏覽次數(shù):
functools.reduce(function, iterable[, initializer])
from functools import reduce func = lambda x, y: x + y result = reduce(func, [1, 2, 3, 4, 5]) print(result)
15
如果在調(diào)用reduce函數(shù)時(shí)傳入了initializer參數(shù),那么function函數(shù)會(huì)以迭代器中的第一個(gè)元素與initializer的和作為參數(shù)進(jìn)行調(diào)用。示例代碼如下:【插播福利:2019傳智播客python視頻教程全集下載】from functools import reduce result = reduce (lambda x, y: x + y, [1, 2, 3, 4], 5) print(result) # 結(jié)果為 15
from functools import reduce result = reduce(lambda x, y: x + y, ['aa', 'bb', 'cc'], 'dd') print(result) # 結(jié)果為 'aabbccdd'
北京校區(qū)