<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 程序:相加兩個矩陣 > 原文: [https://www.programiz.com/python-programming/examples/add-matrix](https://www.programiz.com/python-programming/examples/add-matrix) #### 在此程序中,您將學習使用嵌套循環和列表推導式來相加兩個矩陣,并顯示它們。 要理解此示例,您應該了解以下 [Python 編程](/python-programming "Python tutorial")主題: * [Python `for`循環](/python-programming/for-loop) * [Python 列表](/python-programming/list) * * * 在 Python 中,我們可以將矩陣實現為嵌套列表(列表內的列表)。 我們可以將每個元素視為矩陣的一行。 例如,`X = [[1, 2], [4, 5], [3, 6]]`將代表 3x2 矩陣。 第一行可以選擇為`X[0]`,第一行中的元素可以選擇為`X[0][0]`。 我們可以在 Python 中以各種方式執行矩陣加法。 這里有幾個。 ## 源代碼:使用嵌套循環的矩陣加法 ```py # Program to add two matrices using nested loop X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]] result = [[0,0,0], [0,0,0], [0,0,0]] # iterate through rows for i in range(len(X)): # iterate through columns for j in range(len(X[0])): result[i][j] = X[i][j] + Y[i][j] for r in result: print(r) ``` **輸出** ```py [17, 15, 4] [10, 12, 9] [11, 13, 18] ``` 在此程序中,我們使用了嵌套的`for`循環來遍歷每一行和每一列。 在每一點上,我們在兩個矩陣中添加相應的元素,并將其存儲在結果中。 ## 源代碼:使用嵌套列表推導式的矩陣加法 ```py # Program to add two matrices using list comprehension X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]] result = [[X[i][j] + Y[i][j] for j in range(len(X[0]))] for i in range(len(X))] for r in result: print(r) ``` 該程序的輸出與上述相同。 我們使用嵌套列表推導式來遍歷矩陣中的每個元素。 列表推導式允許我們編寫簡潔的代碼,我們必須嘗試在 Python 中經常使用它們。 他們非常有幫助。
                  <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>

                              哎呀哎呀视频在线观看