<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國際加速解決方案。 廣告
                # Python 映射 > 原文: [http://zetcode.com/python/map/](http://zetcode.com/python/map/) Python 映射教程介紹了 Python 內置的`map()`函數。 ## Python `map()`函數 Python `map()`內置函數將給定函數應用于可迭代的每一項,并返回一個迭代器對象。 ```py map(function, iterable, ...) ``` 可以將多個迭代傳遞給`map()`函數。 該函數必須采用與可迭代項一樣多的參數。 ## Python 映射示例 以下示例在整數列表上使用 Python `map()`。 `python_map.py` ```py #!/usr/bin/python3 def square(x): return x * x nums = [1, 2, 3, 4, 5] nums_squared = map(square, nums) for num in nums_squared: print(num) ``` 我們定義一個整數列表,并使用`map()`在列表的每個元素上應用`square()`函數。 ```py def square(x): return x * x ``` `square()`函數平方其參數。 ```py nums = [1, 2, 3, 4, 5] ``` 我們定義一個整數列表。 ```py nums_squared = map(square, nums) ``` `map()`函數將`square()`函數應用于`nums`列表的每個元素。 ```py for num in nums_squared: print(num) ``` 我們遍歷返回的`iterable`并打印元素。 ```py $ ./python_map.py 1 4 9 16 25 ``` 這是輸出。 ## 等效的 Python 映射 以下示例顯示了與 Python 3 `map()`函數等效的自定義。 `mymap_fun.py` ```py #!/usr/bin/python3 def square(x): return x * x def mymap(func, iterable): for i in iterable: yield func(i) nums = [1, 2, 3, 4, 5] nums_squared = mymap(square, nums) for num in nums_squared: print(num) ``` `mymap()`與 Python 3 `map()`做相同的事情。 ## 帶 lambda 的 Python 映射 下一個示例使用`lambda`運算符在`map()`內部創建一個匿名函數。 `python_map_lambda.py` ```py #!/usr/bin/python3 nums = [1, 2, 3, 4, 5] nums_squared = map(lambda x: x*x, nums) for num in nums_squared: print(num) ``` 該代碼示例將列表元素與`map()`平方在一起,并將匿名函數與`lambda`創建。 ## 具有多個可迭代項的 Python 映射 前面已經提到過,我們可以將多個可迭代對象傳遞給`map()`。 `python_map_iterables.py` ```py #!/usr/bin/python3 def multiply(x, y): return x * y nums1 = [1, 2, 3, 4, 5] nums2 = [6, 7, 8, 9, 10] mult = map(multiply, nums1, nums2) for num in mult: print(num) ``` 在代碼示例中,有兩個持有整數的可迭代對象。 來自兩個可迭代對象的值相乘。 ```py def multiply(x, y): return x * y ``` 該函數必須帶有兩個參數,因為有兩個可迭代變量傳遞給`map()`。 ```py $ ./python_map_iterables.py 6 14 24 36 50 ``` 這是輸出。 ## Python 映射多個函數 在下一個示例中,我們顯示如何在 Python `map()`中使用多個函數。 `python_map_multiple_funcs.py` ```py #!/usr/bin/python3 def add(x): return x + x def square(x): return x * x nums = [1, 2, 3, 4, 5] for i in nums: vals = list(map(lambda x: x(i), (add, square))) print(vals) ``` 在示例中,我們將`add()`和`square()`函數應用于整數值列表。 ```py for i in nums: vals = list(map(lambda x: x(i), (add, square))) print(vals) ``` 我們遍歷`for`循環中的元素。 在每個循環中,我們創建兩個值的列表,這些值是通過對值應用`add()`和`square()`函數來計算的。 ```py $ ./python_map_multiple_funcs.py [2, 1] [4, 4] [6, 9] [8, 16] [10, 25] ``` 第一個值通過加法形成,第二個值通過乘法形成。 ## Python 列表推導式 Python `map()`的功能也可以通過 Python 列表推導式來實現。 `python_list_comprehension.py` ```py #!/usr/bin/python3 def square(x): return x * x nums = [1, 2, 3, 4, 5] nums_squared = [square(num) for num in nums] for num in nums_squared: print(num) ``` 該示例使用 Python 列表推導式從整數列表創建平方值列表。 在本教程中,我們使用了 Python `map()`函數。 您可能也會對以下相關教程感興趣: [Python 教程](/lang/python/), [Python 集教程](/python/set/), [Python 列表推導](/articles/pythonlistcomprehensions/), [OpenPyXL 教程](/articles/openpyxl/),[Python Requests 教程](/web/pythonrequests/)和 [Python CSV 教程](/python/csv/)。
                  <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>

                              哎呀哎呀视频在线观看