提醒:在python3中,reduce被移到了functools里面
from functools import reduce str1 = 'the quick brown fox' str2 = ' jumps over ' str3 = 'the lazy dog.' print(reduce(lambda a, b: a+b, [str1, str2, str3]))
輸出結果為:
the quick brown fox jumps over the lazy dog.
如果計算1+2+3+...+100=?
同樣可以使用reduce
from functools import reduce print(reduce((lambda a,b: a+b), [i for i in range(1, 101)]))
輸出結果為:
5050