python-map/reduce

來源:互聯網
上載者:User

標籤:add   lambda函數   ...   data   ble   進一步   lis   產生   元素   

Python內建了map()reduce()函數。

如果你讀過Google的那篇大名鼎鼎的論文“MapReduce: Simplified Data Processing on Large Clusters”,你就能大概明白map/reduce的概念。

我們先看map。map()函數接收兩個參數,一個是函數,一個是Iterablemap將傳入的函數依次作用到序列的每個元素,並把結果作為新的Iterator返回。

舉例說明,比如我們有一個函數f(x)=x2,要把這個函數作用在一個list [1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map()實現如下:

  f(x) = x * x                  │                  │  ┌───┬───┬───┬───┼───┬───┬───┬───┐  │   │   │   │   │   │   │   │   │  ▼   ▼   ▼   ▼   ▼   ▼   ▼   ▼   ▼[ 1   2   3   4   5   6   7   8   9 ]  │   │   │   │   │   │   │   │   │  │   │   │   │   │   │   │   │   │  ▼   ▼   ▼   ▼   ▼   ▼   ▼   ▼   ▼[ 1   4   9  16  25  36  49  64  81 ]


現在,我們用Python代碼實現:

>>> def f(x):...     return x * x...>>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])>>> list(r)[1, 4, 9, 16, 25, 36, 49, 64, 81]

map()傳入的第一個參數是f,即函數對象本身。由於結果r是一個IteratorIterator是惰性序列,因此通過list()函數讓它把整個序列都計算出來並返回一個list。

你可能會想,不需要map()函數,寫一個迴圈,也可以計算出結果:

L = []for n in [1, 2, 3, 4, 5, 6, 7, 8, 9]:    L.append(f(n))print(L)

的確可以,但是,從上面的迴圈代碼,能一眼看明白“把f(x)作用在list的每一個元素並把結果產生一個新的list”嗎?

所以,map()作為高階函數,事實上它把運算規則抽象了,因此,我們不但可以計算簡單的f(x)=x2,還可以計算任意複雜的函數,比如,把這個list所有數字轉為字串:

>>> list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘]

只需要一行代碼。

再看reduce的用法。reduce把一個函數作用在一個序列[x1, x2, x3, ...]上,這個函數必須接收兩個參數,reduce把結果繼續和序列的下一個元素做累積計算,其效果就是:

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

比方說對一個序列求和,就可以用reduce實現:

>>> from functools import reduce>>> def add(x, y):...     return x + y...>>> reduce(add, [1, 3, 5, 7, 9])25

當然求和運算可以直接用Python內建函數sum(),沒必要動用reduce

但是如果要把序列[1, 3, 5, 7, 9]變換成整數13579reduce就可以派上用場:

>>> from functools import reduce>>> def fn(x, y):...     return x * 10 + y...>>> reduce(fn, [1, 3, 5, 7, 9])13579

這個例子本身沒多大用處,但是,如果考慮到字串str也是一個序列,對上面的例子稍加改動,配合map(),我們就可以寫出把str轉換為int的函數:

>>> from functools import reduce>>> def fn(x, y):...     return x * 10 + y...>>> def char2num(s):...     digits = {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}...     return digits[s]...>>> reduce(fn, map(char2num, ‘13579‘))13579

整理成一個str2int的函數就是:

from functools import reduceDIGITS = {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}def str2int(s):    def fn(x, y):        return x * 10 + y    def char2num(s):        return DIGITS[s]    return reduce(fn, map(char2num, s))

還可以用lambda函數進一步簡化成:

from functools import reduceDIGITS = {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}def char2num(s):    return DIGITS[s]def str2int(s):    return reduce(lambda x, y: x * 10 + y, map(char2num, s))

也就是說,假設Python沒有提供int()函數,你完全可以自己寫一個把字串轉化為整數的函數,而且只需要幾行代碼!

lambda函數的用法在後面介紹。

python-map/reduce

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.