<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>

                **reduce函數** reduce 在Python3.5 中的源碼,只摘錄了下面咱們比較關注的部分 ~~~ def reduce(function, sequence, initial=None): # real signature unknown; restored from __doc__ """ 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. """ pass ~~~ reduce 在 Python 內置的 _functools 模塊中 reduce 是沒有辦法直接使用的,在沒引入 functools 模塊時使用 reduce 方法,會報 reduce 未定義的錯誤 ~~~ >>> def add(x,y):return x+y ... >>> reduce(add,[1,2,3,4]) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'reduce' is not defined ~~~ 那如何使用呢?模塊的使用我們之后的章節會講,現在先知道可以向下面這么使用就可以 ~~~ >>> from functools import reduce >>> def add(x,y):return x+y ... >>> reduce(add,[1,2,3,4]) 10 ~~~ reduce(add,[1,2,3,4]) 等價于 (((1+2)+3)+4) 所以最終結果是 10 。 **Demo** 在 map 那一講,我們已經把 數字字符串 轉換成 整形 列表 現在更進一步,將 數字字符串 轉換成 整形 ~~~ >>> from functools import reduce #引入模塊 >>> >>> 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(lambda x,y:x*10+y,list(map(char2num,'12345'))) #數字字符串 轉換成 整形 12345 >>> type(reduce(lambda x,y:x*10+y,list(map(char2num,'12345')))) #查看類型 <class 'int'> ~~~ 總結: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>

                              哎呀哎呀视频在线观看