[Python] reduce()
python的另一個內建函數 reduce() ,概念上比 filter() 和 map() 複雜一點點。
Introduction
有別於 map() 和 filter() 的重心都在轉換list, reduce() 是將 list 匯整成一個物件。
reduce(function, sequence, initial_value) 會依序先取出兩個 list 中的元素,套入 function 作用後,再將 function 的回傳值再與 List 中的下一個元素一同作為參數傳入f unction,以此類推,直到 list 裡的所有元素都被取完。也因此,傳入 reduce() 中的 function 必須是具有兩個參數的函式。
|
|
當然, lambda 也可以用進來:
|
|
簡單的圖解來說明一下 reduce() 對list a 做了什麼事:
reduce() 還可以傳入第三個參數作為起始值:
|
|
如果沒有特別給定起始值,預設是list的第一個元素為起始值。
Implementation
|
|
More about reduce()
除了數字組成的 list,來看看 reduce() 怎麼 reduce 字串:
|
|
用 join() 也可以達到一樣的結果
|
|
同場加映,用另一個方法:operator
|
|
參考連結:
http://fcamel-fc.blogspot.tw/2011/08/python-1.html
http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php