弄明白python reduce 函數,pythonreduce

來源:互聯網
上載者:User

弄明白python reduce 函數,pythonreduce

作者:Panda Fang

出處:http://www.cnblogs.com/lonkiss/p/understanding-python-reduce-function.html

原創文章,轉載請註明作者和出處,未經允許不可用於商業營利活動

reduce() 函數在 python 2 是內建函數, 從python 3 開始移到了 functools 模組。

官方文檔是這樣介紹的

reduce(...)
reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.

從左至右對一個序列的項累計地應用有兩個參數的函數,以此定序序列到一個單一值。

例如,reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])  計算的就是((((1+2)+3)+4)+5)。

如果提供了 initial 參數,計算時它將被放在序列的所有項前面,如果序列是空的,它也就是計算的預設結果值了

嗯, 這個文檔其實不好理解。看了還是不懂。 序列 其實就是python中 tuple  list  dictionary string  以及其他可迭代物,別的程式設計語言可能有數組。

reduce 有 三個參數

function 有兩個參數的函數, 必需參數
sequence tuple ,list ,dictionary, string等可迭代物,必需參數
initial 初始值, 選擇性參數

reduce的工作過程是 :在迭代sequence(tuple ,list ,dictionary, string等可迭代物)的過程中,首先把 前兩個元素傳給 函數參數,函數加工後,然後把得到的結果和第三個元素作為兩個參數傳給函數參數, 函數加工後得到的結果又和第四個元素作為兩個參數傳給函數參數,依次類推。 如果傳入了 initial 值, 那麼首先傳的就不是 sequence 的第一個和第二個元素,而是 initial值和 第一個元素。經過這樣的累計計算之後定序序列到一個單一傳回值

 

reduce 代碼舉例,使用REPL示範

>>> def add(x, y):...     return x+y...>>> from functools import reduce>>> reduce(add, [1,2,3,4])10>>>
上面這段 reduce 代碼,其實就相當於 1 + 2 + 3 + 4 = 10, 如果把加號改成乘號, 就成了階乘了當然 僅僅是求和的話還有更簡單的方法,如下
>>> sum([1,2,3,4])10>>>

 

很多教程只講了一個加法求和,太簡單了,對新手加深理解還不夠。下面講點更深入的例子


還可以把一個整數列表拼成整數,如下

>>> from functools import reduce>>> reduce(lambda x, y: x * 10 + y, [1 , 2, 3, 4, 5])12345>>>

 

對一個複雜的sequence使用reduce ,看下面代碼,更多的代碼不再使用REPL, 使用編輯器編寫

 1 from functools import reduce 2 scientists =({'name':'Alan Turing', 'age':105}, 3              {'name':'Dennis Ritchie', 'age':76}, 4              {'name':'John von Neumann', 'age':114}, 5              {'name':'Guido van Rossum', 'age':61}) 6 def reducer(accumulator , value): 7     sum = accumulator['age'] + value['age'] 8     return sum 9 total_age = reduce(reducer, scientists)10 print(total_age)
這段代碼會出錯,看的執行過程  

 

 

所以代碼需要修改
 1 from functools import reduce 2 scientists =({'name':'Alan Turing', 'age':105, 'gender':'male'}, 3              {'name':'Dennis Ritchie', 'age':76, 'gender':'male'}, 4              {'name':'Ada Lovelace', 'age':202, 'gender':'female'}, 5              {'name':'Frances E. Allen', 'age':84, 'gender':'female'}) 6 def reducer(accumulator , value): 7     sum = accumulator + value['age'] 8     return sum 9 total_age = reduce(reducer, scientists, 0)10 print(total_age)
7, 9 行 紅色部分就是修改 部分。 通過 help(reduce) 查看 文檔,reduce 有三個參數, 第三個參數是初始值的意思,是可有可無的參數。 修改之後就不出錯了,流程如下

 

這個仍然也可以用 sum 來更簡單的完成

sum([x['age'] for x in scientists ])

做點更進階的事情,按性別分組

from functools import reducescientists =({'name':'Alan Turing', 'age':105, 'gender':'male'},             {'name':'Dennis Ritchie', 'age':76, 'gender':'male'},             {'name':'Ada Lovelace', 'age':202, 'gender':'female'},             {'name':'Frances E. Allen', 'age':84, 'gender':'female'})def group_by_gender(accumulator , value):    accumulator[value['gender']].append(value['name'])    return accumulatorgrouped = reduce(group_by_gender, scientists, {'male':[], 'female':[]})print(grouped)

輸出

{'male': ['Alan Turing', 'Dennis Ritchie'], 'female': ['Ada Lovelace', 'Frances E. Allen']}
可以看到,在 reduce 的初始值參數傳入了一個dictionary,, 但是這樣寫 key 可能出錯,還能再進一步自動化,運行時動態插入key修改代碼如下
grouped = reduce(group_by_gender, scientists, collections.defaultdict(list))

當然 先要 import  collections 模組

這當然也能用 pythonic way 去解決

import  itertoolsscientists =({'name':'Alan Turing', 'age':105, 'gender':'male'},             {'name':'Dennis Ritchie', 'age':76, 'gender':'male'},             {'name':'Ada Lovelace', 'age':202, 'gender':'female'},             {'name':'Frances E. Allen', 'age':84, 'gender':'female'})grouped = {item[0]:list(item[1])           for item in itertools.groupby(scientists, lambda x: x['gender'])}print(grouped)

 

再來一個更晦澀難懂的玩法。工作中要與其他人協作的話,不建議這麼用,與上面的例子做同樣的事,看不懂無所謂。

from functools import reducescientists =({'name':'Alan Turing', 'age':105, 'gender':'male'},             {'name':'Dennis Ritchie', 'age':76, 'gender':'male'},             {'name':'Ada Lovelace', 'age':202, 'gender':'female'},             {'name':'Frances E. Allen', 'age':84, 'gender':'female'})grouped = reduce(lambda acc, val: {**acc, **{val['gender']: acc[val['gender']]+ [val['name']]}}, scientists, {'male':[], 'female':[]})print(grouped)

**acc, **{val['gneder']...   這裡使用了 dictionary merge syntax ,  從 python 3.5 開始引入, 詳情請看 PEP 448 - Additional Unpacking Generalizations  怎麼使用可以參考這個 python - How to merge two dictionaries in a single expression? - Stack Overflow

python 社區推薦寫可讀性好的代碼,有更好的選擇時不建議用reduce,所以 python 2 中內建的reduce 函數 移到了 functools模組中

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.