{"id":1010923,"date":"2024-12-27T11:25:31","date_gmt":"2024-12-27T03:25:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1010923.html"},"modified":"2024-12-27T11:25:34","modified_gmt":"2024-12-27T03:25:34","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e7%9f%a9%e9%98%b5%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1010923.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528\u77e9\u9635\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085247\/0f1fbbb0-2896-47f4-9e1d-8622ec849c82.webp\" alt=\"python\u5982\u4f55\u8c03\u7528\u77e9\u9635\u5143\u7d20\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/>\u5728Python\u4e2d\u8c03\u7528\u77e9\u9635\u5143\u7d20\u7684\u65b9\u6cd5\u4e3b\u8981\u6709<strong>\u4f7f\u7528\u7d22\u5f15\u3001\u4f7f\u7528\u5207\u7247\u3001\u5229\u7528numpy\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528numpy\u5e93<\/strong>\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002Numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u6570\u7ec4\u5bf9\u8c61\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002\u5229\u7528numpy\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u884c\u5217\u7d22\u5f15\u7684\u65b9\u5f0f\u8f7b\u677e\u8bbf\u95ee\u548c\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002\u9664\u6b64\u4e4b\u5916\uff0cnumpy\u8fd8\u652f\u6301\u5e03\u5c14\u7d22\u5f15\u548c\u82b1\u5f0f\u7d22\u5f15\uff0c\u8fd9\u4e9b\u9ad8\u7ea7\u529f\u80fd\u4f7f\u5f97\u77e9\u9635\u64cd\u4f5c\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002\u4f8b\u5982\uff0c\u8981\u8bbf\u95eenumpy\u77e9\u9635\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>array[row, column]<\/code>\u7684\u5f62\u5f0f\u6765\u83b7\u53d6\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u6b64\u5916\uff0cnumpy\u8fd8\u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff0c\u4f7f\u5f97\u5728\u591a\u7ef4\u6570\u636e\u5904\u7406\u65f6\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON\u4e2d\u77e9\u9635\u7684\u57fa\u672c\u6982\u5ff5\u53ca\u5176\u521b\u5efa<\/p>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u8ba8\u8bba\u5982\u4f55\u8c03\u7528\u548c\u64cd\u4f5c\u77e9\u9635\u5143\u7d20\u4e4b\u524d\uff0c\u9996\u5148\u4e86\u89e3\u77e9\u9635\u7684\u57fa\u672c\u6982\u5ff5\u4ee5\u53ca\u5728Python\u4e2d\u7684\u521b\u5efa\u65b9\u5f0f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u77e9\u9635\u662f\u4e00\u79cd\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u901a\u5e38\u7528\u4e8e\u8868\u793a\u548c\u5904\u7406\u6570\u636e\u96c6\u3001\u56fe\u50cf\u3001\u65b9\u7a0b\u7ec4\u7b49\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868(list)\u6765\u8868\u793a\u7b80\u5355\u7684\u4e8c\u7ef4\u77e9\u9635\uff0c\u4f46\u4e3a\u4e86\u66f4\u9ad8\u6548\u548c\u4e13\u4e1a\u7684\u5904\u7406\u77e9\u9635\uff0c\u901a\u5e38\u4f7f\u7528numpy\u5e93\u3002<\/p>\n<\/p>\n<p><p>1.1 \u77e9\u9635\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<\/p>\n<p><p>\u77e9\u9635\u662f\u4e00\u4e2a\u4ee5\u884c\u548c\u5217\u6392\u5217\u7684\u6570\u5b57\u96c6\u5408\u3002\u5b83\u7684\u57fa\u672c\u5355\u4f4d\u662f\u5143\u7d20\uff0c\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u5b57\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\u3002\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u901a\u8fc7\u884c\u53f7\u548c\u5217\u53f7\u6765\u6807\u8bc6\u3002\u4e00\u4e2am x n\u7684\u77e9\u9635\u8868\u793a\u6709m\u884c\u548cn\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code>| 1  2  3 |<\/p>\n<p>| 4  5  6 |<\/p>\n<p>| 7  8  9 |<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u662f\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u5177\u67099\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>1.2 \u4f7f\u7528\u5217\u8868\u521b\u5efa\u7b80\u5355\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u77e9\u9635\u8868\u793a\u65b9\u6cd5\u662f\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3002\u6bcf\u4e2a\u5b50\u5217\u8868\u8868\u793a\u77e9\u9635\u7684\u4e00\u884c\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u76843&#215;3\u77e9\u9635\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>1.3 \u4f7f\u7528numpy\u521b\u5efa\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u867d\u7136\u5217\u8868\u53ef\u4ee5\u8868\u793a\u77e9\u9635\uff0c\u4f46\u5bf9\u4e8e\u77e9\u9635\u7684\u590d\u6742\u64cd\u4f5c\uff0c\u6bd4\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5f3a\u5927\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7numpy\u7684<code>array<\/code>\u51fd\u6570\u6765\u521b\u5efa\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528numpy\u521b\u5efa\u7684\u77e9\u9635\uff08\u5373ndarray\u5bf9\u8c61\uff09\u4e0d\u4ec5\u64cd\u4f5c\u7b80\u5355\uff0c\u800c\u4e14\u6027\u80fd\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u77e9\u9635\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8bbf\u95ee\u77e9\u9635\u5143\u7d20\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7d22\u5f15\u3002\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u83b7\u53d6\u6216\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>2.1 \u5217\u8868\u4e2d\u7684\u7d22\u5f15\u8bbf\u95ee<\/p>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u8868\u793a\u77e9\u9635\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e24\u6b21\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\uff1a\u7b2c\u4e00\u6b21\u7d22\u5f15\u9009\u62e9\u884c\uff0c\u7b2c\u4e8c\u6b21\u7d22\u5f15\u9009\u62e9\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix[1][2]  # \u8bbf\u95ee\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2 Numpy\u4e2d\u7684\u7d22\u5f15\u8bbf\u95ee<\/p>\n<\/p>\n<p><p>numpy\u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u9017\u53f7\u5206\u9694\u7684\u7d22\u5f15\u6765\u8bbf\u95ee\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix[1, 2]  # \u8bbf\u95ee\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u5d4c\u5957\u5217\u8868\u7684\u8bbf\u95ee\u65b9\u5f0f\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p>2.3 \u4fee\u6539\u77e9\u9635\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u5217\u8868\u8fd8\u662fnumpy\u77e9\u9635\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u4fee\u6539\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix[1, 2] = 10  # \u5c06\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20\u4fee\u6539\u4e3a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5207\u7247\u8bbf\u95ee\u77e9\u9635\u7684\u5b50\u96c6<\/p>\n<\/p>\n<p><p>\u5207\u7247\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bbf\u95ee\u77e9\u9635\u7684\u5b50\u96c6\u3002\u901a\u8fc7\u5207\u7247\uff0c\u53ef\u4ee5\u9009\u62e9\u77e9\u9635\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u5355\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>3.1 \u5217\u8868\u4e2d\u7684\u5207\u7247<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5e38\u89c4\u7684\u5207\u7247\u64cd\u4f5c\u8bbf\u95ee\u591a\u884c\u6216\u591a\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_matrix = [row[1:3] for row in matrix[0:2]]  # \u9009\u62e9\u524d\u4e24\u884c\u7684\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5217<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2 Numpy\u4e2d\u7684\u5207\u7247<\/p>\n<\/p>\n<p><p>numpy\u7684\u5207\u7247\u529f\u80fd\u66f4\u52a0\u4e30\u5bcc\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u591a\u7ef4\u6570\u7ec4\u8fdb\u884c\u5207\u7247\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_matrix = matrix[0:2, 1:3]  # \u9009\u62e9\u524d\u4e24\u884c\u7684\u7b2c\u4e8c\u548c\u7b2c\u4e09\u5217<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u800c\u4e14\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<p><p>3.3 \u7075\u6d3b\u4f7f\u7528\u5207\u7247<\/p>\n<\/p>\n<p><p>\u5229\u7528\u5207\u7247\u53ef\u4ee5\u8fdb\u884c\u66f4\u591a\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u9009\u62e9\u7279\u5b9a\u7684\u884c\u3001\u5217\uff0c\u6216\u8005\u53cd\u8f6c\u77e9\u9635\u7b49\u3002\u4f8b\u5982\uff0c\u53cd\u8f6c\u77e9\u9635\u7684\u6240\u6709\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">reversed_matrix = matrix[::-1, :]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5229\u7528\u5e03\u5c14\u7d22\u5f15\u548c\u82b1\u5f0f\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u7d22\u5f15\u548c\u5207\u7247\uff0cnumpy\u8fd8\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u7684\u7d22\u5f15\u529f\u80fd\uff0c\u5982\u5e03\u5c14\u7d22\u5f15\u548c\u82b1\u5f0f\u7d22\u5f15\uff0c\u8fdb\u4e00\u6b65\u589e\u5f3a\u4e86\u77e9\u9635\u64cd\u4f5c\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>4.1 \u5e03\u5c14\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u5e03\u5c14\u7d22\u5f15\u7528\u4e8e\u9009\u62e9\u77e9\u9635\u4e2d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\uff0c\u7136\u540e\u7528\u5b83\u6765\u7d22\u5f15\u77e9\u9635\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bool_index = matrix &gt; 5  # \u521b\u5efa\u4e00\u4e2a\u5e03\u5c14\u77e9\u9635<\/p>\n<p>filtered_elements = matrix[bool_index]  # \u9009\u62e9\u5927\u4e8e5\u7684\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>4.2 \u82b1\u5f0f\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u82b1\u5f0f\u7d22\u5f15\u5141\u8bb8\u901a\u8fc7\u6307\u5b9a\u4e00\u7ec4\u7d22\u5f15\u6765\u8bbf\u95ee\u591a\u4e2a\u975e\u8fde\u7eed\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = [0, 2]<\/p>\n<p>columns = [1, 2]<\/p>\n<p>selected_elements = matrix[rows, columns]  # \u9009\u62e9(0,1)\u548c(2,2)\u4f4d\u7f6e\u7684\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u4ece\u77e9\u9635\u4e2d\u62bd\u53d6\u7279\u5b9a\u4f4d\u7f6e\u7684\u591a\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u77e9\u9635\u7684\u5176\u4ed6\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20\uff0cPython\u4e2d\u7684\u77e9\u9635\u8fd8\u652f\u6301\u8bb8\u591a\u5176\u4ed6\u64cd\u4f5c\uff0c\u5982\u77e9\u9635\u8fd0\u7b97\u3001\u8f6c\u7f6e\u3001\u5408\u5e76\u7b49\u3002<\/p>\n<\/p>\n<p><p>5.1 \u77e9\u9635\u8fd0\u7b97<\/p>\n<\/p>\n<p><p>Numpy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\uff0c\u5305\u62ec\u77e9\u9635\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<p>result = np.add(matrix1, matrix2)  # \u77e9\u9635\u52a0\u6cd5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>5.2 \u77e9\u9635\u8f6c\u7f6e<\/p>\n<\/p>\n<p><p>\u77e9\u9635\u8f6c\u7f6e\u662f\u6307\u5c06\u77e9\u9635\u7684\u884c\u548c\u5217\u4e92\u6362\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u7684<code>transpose()<\/code>\u51fd\u6570\u6216.T\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transposed_matrix = matrix.T  # \u8f6c\u7f6e\u77e9\u9635<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>5.3 \u5408\u5e76\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7numpy\u7684<code>concatenate<\/code>\u3001<code>vstack<\/code>\u3001<code>hstack<\/code>\u7b49\u51fd\u6570\u5c06\u591a\u4e2a\u77e9\u9635\u5408\u5e76\u4e3a\u4e00\u4e2a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6]])<\/p>\n<p>combined_matrix = np.vstack((matrix1, matrix2))  # \u5782\u76f4\u5408\u5e76\u77e9\u9635<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u77e9\u9635\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u77e9\u9635\u64cd\u4f5c\u662f\u6570\u636e\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3001\u56fe\u50cf\u5904\u7406\u7b49\u9886\u57df\u7684\u57fa\u7840\u3002\u5728\u8fd9\u4e9b\u9886\u57df\u4e2d\uff0cPython\u7684\u77e9\u9635\u64cd\u4f5c\u80fd\u529b\u88ab\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>6.1 \u6570\u636e\u5206\u6790\u4e2d\u7684\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u77e9\u9635\u5e38\u7528\u4e8e\u5b58\u50a8\u548c\u5904\u7406\u6570\u636e\u96c6\u3002\u6bcf\u4e00\u884c\u901a\u5e38\u8868\u793a\u4e00\u4e2a\u6570\u636e\u6837\u672c\uff0c\u800c\u6bcf\u4e00\u5217\u8868\u793a\u4e00\u4e2a\u7279\u5f81\u3002\u901a\u8fc7\u77e9\u9635\u8fd0\u7b97\uff0c\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u7684\u6807\u51c6\u5316\u3001\u5f52\u4e00\u5316\u3001\u964d\u7ef4\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>6.2 \u673a\u5668\u5b66\u4e60\u4e2d\u7684\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u5982\u7ebf\u6027\u56de\u5f52\u3001\u652f\u6301\u5411\u91cf\u673a\u7b49\u90fd\u4f9d\u8d56\u4e8e\u77e9\u9635\u8fd0\u7b97\u3002\u901a\u8fc7\u77e9\u9635\u7684\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\u64cd\u4f5c\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8ba1\u7b97\u6a21\u578b\u53c2\u6570\u3001\u9884\u6d4b\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><p>6.3 \u56fe\u50cf\u5904\u7406\u4e2d\u7684\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u56fe\u50cf\u53ef\u4ee5\u88ab\u89c6\u4e3a\u4e00\u4e2a\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u8868\u793a\u4e00\u4e2a\u50cf\u7d20\u7684\u7070\u5ea6\u503c\u6216\u989c\u8272\u503c\u3002\u901a\u8fc7\u77e9\u9635\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8fdb\u884c\u56fe\u50cf\u7684\u6ee4\u6ce2\u3001\u53d8\u6362\u3001\u538b\u7f29\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u4ece\u57fa\u672c\u7684\u7d22\u5f15\u3001\u5207\u7247\u5230\u9ad8\u7ea7\u7684\u5e03\u5c14\u7d22\u5f15\u548c\u82b1\u5f0f\u7d22\u5f15\uff0c\u4ee5\u53ca\u77e9\u9635\u8fd0\u7b97\u3001\u8f6c\u7f6e\u3001\u5408\u5e76\u7b49\u3002\u901a\u8fc7\u5229\u7528numpy\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u590d\u6742\u7684\u77e9\u9635\u64cd\u4f5c\uff0c\u8fd9\u5728\u6570\u636e\u5206\u6790\u3001\u673a\u5668\u5b66\u4e60\u3001\u56fe\u50cf\u5904\u7406\u7b49\u9886\u57df\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u77e9\u9635\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install numpy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u521b\u5efa\u77e9\u9635\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>matrix = np.array([[1, 2, 3], [4, 5, 6]])<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a2&#215;3\u7684\u77e9\u9635\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u77e9\u9635\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u8bbf\u95ee\u77e9\u9635\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u884c\u548c\u5217\u7684\u7d22\u5f15\u6765\u5b9e\u73b0\u3002\u5728NumPy\u4e2d\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>matrix<\/code>\u7684\u77e9\u9635\uff0c\u60f3\u8981\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>element = matrix[0, 1]<\/code>\u3002\u8fd9\u5c06\u8fd4\u56de\u503c2\uff0c\u56e0\u4e3a\u7d22\u5f15(0, 1)\u5bf9\u5e94\u7684\u662f\u77e9\u9635\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u77e9\u9635\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\uff1f<\/strong><br \/>\u5207\u7247\u64cd\u4f5c\u5141\u8bb8\u4f60\u63d0\u53d6\u77e9\u9635\u7684\u5b50\u96c6\u3002\u5728NumPy\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5192\u53f7\u6765\u6307\u5b9a\u5207\u7247\u8303\u56f4\u3002\u4f8b\u5982\uff0c<code>sub_matrix = matrix[0:2, 1:3]<\/code>\u5c06\u63d0\u53d6\u77e9\u9635\u7684\u524d\u4e24\u884c\u548c\u7b2c\u4e8c\u3001\u7b2c\u4e09\u5217\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u77e9\u9635\u3002\u5982\u679c\u4f60\u60f3\u83b7\u53d6\u6574\u884c\u6216\u6574\u5217\uff0c\u53ef\u4ee5\u7701\u7565\u76f8\u5e94\u7684\u7d22\u5f15\uff0c\u6bd4\u5982<code>all_rows = matrix[:, 1]<\/code>\u5c06\u8fd4\u56de\u7b2c\u4e8c\u5217\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u5728Python\u4e2d\u8c03\u7528\u77e9\u9635\u5143\u7d20\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4f7f\u7528\u7d22\u5f15\u3001\u4f7f\u7528\u5207\u7247\u3001\u5229\u7528numpy\u5e93\u3002\u5176\u4e2d\uff0c\u4f7f\u7528numpy\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1010935,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010923"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1010923"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010923\/revisions"}],"predecessor-version":[{"id":1010936,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010923\/revisions\/1010936"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1010935"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1010923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1010923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1010923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}