<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # map/reduce Python內建了`map()`和`reduce()`函數。 如果你讀過Google的那篇大名鼎鼎的論文“[MapReduce: Simplified Data Processing on Large Clusters](http://research.google.com/archive/mapreduce.html)”,你就能大概明白map/reduce的概念。 我們先看map。`map()`函數接收兩個參數,一個是函數,一個是序列,`map`將傳入的函數依次作用到序列的每個元素,并把結果作為新的list返回。 舉例說明,比如我們有一個函數f(x)=x&lt;sup&gt;2&lt;/sup&gt;,要把這個函數作用在一個list `[1, 2, 3, 4, 5, 6, 7, 8, 9]`上,就可以用`map()`實現如下: ![map](https://box.kancloud.cn/2016-01-15_56988a24e36ff.png) 現在,我們用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)=x&lt;sup&gt;2&lt;/sup&gt;,還可以計算任意復雜的函數,比如,把這個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實現: ``` >>> def add(x, y): ... return x + y ... >>> reduce(add, [1, 3, 5, 7, 9]) 25 ``` 當然求和運算可以直接用Python內建函數`sum()`,沒必要動用reduce。 但是如果要把序列`[1, 3, 5, 7, 9]`變換成整數13579,reduce就可以派上用場: ``` >>> 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函數的用法在后面介紹。 ### 練習 利用`map()`函數,把用戶輸入的不規范的英文名字,變為首字母大寫,其他小寫的規范名字。輸入:`['adam', 'LISA', 'barT']`,輸出:`['Adam', 'Lisa', 'Bart']`。 Python提供的`sum()`函數可以接受一個list并求和,請編寫一個`prod()`函數,可以接受一個list并利用`reduce()`求積。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看