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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Python `map()`函數 > 原文: [https://thepythonguru.com/python-builtin-functions/map/](https://thepythonguru.com/python-builtin-functions/map/) * * * 于 2020 年 1 月 7 日更新 * * * 將`map()`內置函數應用于序列中的每個項目后,它會返回一個迭代器。 其語法如下: **語法**: `map(function, sequence[, sequence, ...]) -> map object` **Python 3** ```py >>> >>> map(ord, ['a', 'b', 'c', 'd']) <map object at 0x7f36fac76dd8> >>> >>> list(map(ord, ['a', 'b', 'c', 'd'])) >>> [97, 98, 99, 100] >>> ``` ```py map_obj = map(ord, ['a', 'b', 'c', 'd']) print(map_obj) print(list(map_obj)) ``` 在此,列表中的項目一次被傳遞到`ord()`內置函數。 由于`map()`返回一個迭代器,因此我們使用了`list()`函數立即生成結果。 上面的代碼在功能上等同于以下代碼: **Python 3** ```py >>> >>> ascii = [] >>> >>> for i in ['a', 'b', 'c', 'd']: ... ascii.append(ord(i)) ... >>> >>> ascii [97, 98, 99, 100] >>> ``` ```py ascii = [] for i in ['a', 'b', 'c', 'd']: ascii.append(ord(i)) print(ascii) ``` 但是,使用`map()`會導致代碼縮短,并且通常運行速度更快。 在 Python 2 中,`map()`函數返回一個列表,而不是一個迭代器(就內存消耗而言,這不是很有效),因此我們無需在`list()`調用中包裝`map()`。 **Python 2** ```py >>> >>> map(ord, ['a', 'b', 'c', 'd']) # in Python 2 [97, 98, 99, 100] >>> ``` ## 傳遞用戶定義的函數 * * * 在下面的清單中,我們將用戶定義的函數傳遞給`map()`函數。 **Python 3** ```py >>> >>> def twice(x): ... return x*2 ... >>> >>> list(map(twice, [11,22,33,44,55])) [22, 44, 66, 88, 110] >>> ``` ```py def twice(x): return x*2 print(list(map(twice, [11,22,33,44,55]))) ``` 在此,該函數將列表中的每個項目乘以 2。 ## 傳遞多個參數 * * * 如果我們將`n`序列傳遞給`map()`,則該函數必須采用`n`個參數,并且并行使用序列中的項,直到用盡最短的序列。 但是在 Python 2 中,當最長的序列被用盡時,`map()`函數停止,而當較短的序列被用盡時,`None`的值用作填充。 **Python 3** ```py >>> >>> def calc_sum(x1, x2): ... return x1+x2 ... >>> >>> list(map(calc_sum, [1, 2, 3, 4, 5], [10, 20, 30])) [11, 22, 33] >>> ``` ```py def calc_sum(x1, x2): return x1+x2 map_obj = list(map(calc_sum, [1, 2, 3, 4, 5], [10, 20, 30])) print(map_obj) ``` **Python 2** ```py >>> >>> def foo(x1, x2): ... if x2 is None: ... return x1 ... else: ... return x1+x2 ... >>> >>> list(map(foo, [1, 2, 3, 4, 5], [10, 20, 30])) [11, 22, 33, 4, 5] >>> ``` ## 傳遞 Lambda * * * 如果您的函數不打算被重用,則可以傳遞 lambda(內聯匿名函數)而不是函數。 **Python 3** ```py >>> >>> list(map(lambda x1:x1*5, [1, 2, 3])) [5, 10, 15] >>> ``` ```py map_obj = map(lambda x1:x1*5, [1, 2, 3]) print(list(map_obj)) ``` 在此,該函數將列表中的每個項目乘以 5。 ## 配對項目(僅在 Python 2 中) * * * 最后,您可以通過傳遞`None`代替函數來配對多個序列中的項目: **Python 2** ```py >>> >>> map(None, "hello", "pi") [('h', 'p'), ('e', 'i'), ('l', None), ('l', None), ('o', None)] >>> ``` 請注意,當較短的序列用盡時,將使用`None`填充結果。 這種形式的`map()`在 Python 3 中無效。 ```py >>> >>> list(map(None, "hello", "pi")) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not callable >>> ``` ```py print(list(map(None, "hello", "pi"))) ``` 如果要配對多個序列中的項目,請使用[`zip()`](/python-zip-function/)函數。 * * * * * *
                  <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>

                              哎呀哎呀视频在线观看