【Python學習筆記】函數式編程:高階函數map和reduce

【根據廖雪峯python教程整理】


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

 

如果你讀過Google的那篇大名鼎鼎的論文“MapReduce: Simplified Data

Processing on Large Clusters”,你就能大概明白map/reduce的概念。

 

一、map

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

 

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

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

>>> def f(x):

...     return x * x

...

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

[1, 4, 9, 16, 25, 36, 49, 64, 81]

map()傳入的第一個參數是f,即函數對象本身。

 

你可能會想,不需要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所有數字轉爲字符串:

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

['1', '2', '3', '4', '5', '6', '7', '8', '9']

只需要一行代碼。

 

二、reduce

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

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

比方說對一個序列求和,就可以用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就可以派上用場:

>>> def fn(x, y):

...     return x * 10 + y

...

>>> reduce(fn, [1, 3, 5, 7, 9])

13579

 

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

>>> def fn(x, y):

...     return x * 10 + y

...

>>> def char2num(s):

...     return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]

...

>>> reduce(fn, map(char2num, '13579'))

13579

 

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

def str2int(s):

    def fn(x, y):

        return x * 10 + y

    def char2num(s):

        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]

return reduce(fn, map(char2num, s))

 

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

def char2num(s):

    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]

def str2int(s):

    return reduce(lambda x,y: x*10+y, map(char2num, s))

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

 

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

 

練習

1、利用map()函數,把用戶輸入的不規範的英文名字,變爲首字母大寫,其他小寫的規範名字。輸入:['adam', 'LISA', 'barT'],輸出:['Adam', 'Lisa', 'Bart']

# -*- coding: utf-8 -*-

def normalize(name):
     return str.capitalize(name) #將字符串的第一個字母變成大寫,其他字母變小寫

L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)


2、Python提供的sum()函數可以接受一個list並求和,請編寫一個prod()函數,可以接受一個list並利用reduce()求積。

# -*- coding: utf-8 -*-
from functools import reduce

def prod(L):
    return reduce(lambda x,y:x*y,L)

print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))

3、利用map和reduce編寫一個str2float函數,把字符串'123.456'轉換成浮點數123.456:

# -*- coding: utf-8 -*-
from functools import reduce

def str2float(s):
    s1, s2 = s.split('.') #按.分割成兩個字符串
    def char2float(keys): #根據輸入的keys拆分字符串
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,}[keys]
    def fn(x,y):
        return x * 10 + y        
    return reduce(fn, map(char2float,s1 + s2 )) / 10 ** len(s2) #10的3次方
	
print('str2float(\'123.456\') =', str2float('123.456'))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章