MapReduce的設計靈感來自於函數式編程,這裏不打算提MapReduce,就拿python中的map()函數來學習一下。html
文檔中的介紹在這裏:python
map(function, iterable, ...)編程
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended withNoneitems. If function isNone, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.多線程
一點一點看:app
一、對可迭代函數'iterable'中的每個元素應用‘function’方法,將結果做爲list返回。ide
來個例子:函數式編程
>>> def add100(x): ... return x+100 ... >>> hh = [11,22,33] >>> map(add100,hh) [111, 122, 133]
就像文檔中說的:對hh中的元素作了add100,返回告終果的list。函數
二、若是給出了額外的可迭代參數,則對每一個可迭代參數中的元素並行(多線程)的應用‘function’。學習
>>> def abc(a, b, c): ... return a*10000 + b*100 + c ... >>> list1 = [11,22,33] >>> list2 = [44,55,66] >>> list3 = [77,88,99] >>> map(abc,list1,list2,list3) [114477, 225588, 336699]
看到並行的效果了吧!在每一個list中,取出了下標相同的元素,執行了abc()。.net
三、若是'function'給出的是‘None’,自動假定一個‘identity’函數
>>> list1 = [11,22,33] >>> map(None,list1) [11, 22, 33] >>> list1 = [11,22,33] >>> list2 = [44,55,66] >>> list3 = [77,88,99] >>> map(None,list1,list2,list3) [(11, 44, 77), (22, 55, 88), (33, 66, 99)]
用語言解釋好像有點拗口 ,例子應該很容易理解。
介紹到這裏應該差很少了吧!不過還有東西能夠挖掘:
stackoverflow上有人說能夠這樣理解map():
map(f, iterable) 基本上等於: [f(x) for x in iterable]
趕快試一下:
>>> def add100(x): ... return x + 100 ... >>> list1 = [11,22,33] >>> map(add100,list1) [101, 102, 103] >>> [add100(i) for i in list1] [101, 102, 103]
哦,輸出結果同樣。原來map()就是列表推導式啊!要是這樣想就錯了:這裏只是表面現象!再來個例子看看:
>>> def abc(a, b, c): ... return a*10000 + b*100 + c ... >>> list1 = [11,22,33] >>> list2 = [44,55,66] >>> list3 = [77,88,99] >>> map(abc,list1,list2,list3) [114477, 225588, 336699]
這個例子咱們在上面看過了,如果用列表推導應該怎麼寫呢?我想是這樣的:
[abc(a,b,c) for a in list1 for b in list2 for c in list3]
可是看到結果,發現根本不是這麼回事:
[114477, 114488, 114499, 115577, 115588, 115599, 116677, 116688, 116699, 224477, 224488, 224499, 225577, 225588, 225599, 226677, 226688, 226699, 334477, 334488, 334499, 335577, 335588, 335599, 336677, 336688, 336699]
這即是上面列表推導的結果。怎麼會這麼多?固然了列表推導能夠這麼寫:
result = [] for a in list1: for b in list2: for c in list3: result.append(abc(abc))
原來如此,如果將三個list看作矩陣的話: map()只作了列上面的運算,而列表推導(也就是嵌套for循環)作了笛卡爾乘積。