{"id":967747,"date":"2024-12-27T05:00:08","date_gmt":"2024-12-26T21:00:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/967747.html"},"modified":"2024-12-27T05:00:10","modified_gmt":"2024-12-26T21:00:10","slug":"python%e5%a6%82%e4%bd%95%e5%bc%95%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\/967747.html","title":{"rendered":"python\u5982\u4f55\u5f15\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\/24182855\/6aa87061-f348-4a1e-87e2-142015547a05.webp\" alt=\"python\u5982\u4f55\u5f15\u7528\u77e9\u9635\u5143\u7d20\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5f15\u7528\u77e9\u9635\u5143\u7d20\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528NumPy\u5e93\u7684\u6570\u7ec4\u3001\u5217\u8868\u5d4c\u5957\u7ed3\u6784\u3001\u901a\u8fc7\u7d22\u5f15\u5f15\u7528\u3002<strong>NumPy\u5e93\u662f\u5904\u7406\u77e9\u9635\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3001\u901a\u8fc7\u884c\u5217\u7d22\u5f15\u5f15\u7528\u5177\u4f53\u5143\u7d20\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u63d0\u53d6\u5b50\u77e9\u9635\u3002<\/strong>\u5176\u4e2d\uff0cNumPy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u77e9\u9635\u8fdb\u884c\u5404\u79cd\u8fd0\u7b97\u548c\u64cd\u4f5c\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001NUMPY\u5e93\u7b80\u4ecb\u4e0e\u57fa\u672c\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u6700\u5e38\u7528\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u7528\u4e8e\u64cd\u4f5c\u8fd9\u4e9b\u6570\u7ec4\u7684\u5404\u79cd\u51fd\u6570\u3002\u8981\u4f7f\u7528NumPy\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u77e9\u9635<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u77e9\u9635\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\u3002NumPy\u6570\u7ec4\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u5728\u540c\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u4e00\u81f4\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5f15\u7528\u77e9\u9635\u5143\u7d20<\/strong>\uff1a\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u5f15\u7528\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002\u4f8b\u5982\uff0c\u8981\u5f15\u7528\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">element = matrix[1, 2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de6\u3002\u6ce8\u610f\uff0cNumPy\u4f7f\u7528\u9017\u53f7\u5206\u9694\u884c\u548c\u5217\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5207\u7247\u64cd\u4f5c<\/strong>\uff1aNumPy\u652f\u6301\u5207\u7247\u64cd\u4f5c\uff0c\u53ef\u4ee5\u63d0\u53d6\u5b50\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u8981\u63d0\u53d6\u524d\u4e24\u884c\u548c\u524d\u4e24\u5217\u7684\u5b50\u77e9\u9635\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">sub_matrix = matrix[0:2, 0:2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5143\u7d20<code>[[1, 2], [4, 5]]<\/code>\u76842&#215;2\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u5d4c\u5957\u7ed3\u6784\u662f\u53e6\u4e00\u79cd\u8868\u793a\u77e9\u9635\u7684\u65b9\u5f0f\u3002\u867d\u7136\u6ca1\u6709NumPy\u9ad8\u6548\uff0c\u4f46\u5bf9\u4e8e\u7b80\u5355\u7684\u77e9\u9635\u64cd\u4f5c\u4ecd\u7136\u9002\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u77e9\u9635<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u6765\u521b\u5efa\u77e9\u9635\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5f15\u7528\u77e9\u9635\u5143\u7d20<\/strong>\uff1a\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u5f15\u7528\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5f15\u7528\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">element = matrix[1][2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de6\u3002\u6ce8\u610f\uff0c\u4f7f\u7528\u4e86\u4e24\u4e2a\u4e2d\u62ec\u53f7\u8868\u793a\u884c\u548c\u5217\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5207\u7247\u64cd\u4f5c<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684\u5207\u7247\u529f\u80fd\u63d0\u53d6\u5b50\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u8981\u63d0\u53d6\u524d\u4e24\u884c\u548c\u524d\u4e24\u5217\u7684\u5b50\u77e9\u9635\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">sub_matrix = [row[0:2] for row in matrix[0:2]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de<code>[[1, 2], [4, 5]]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001NUMPY\u9ad8\u7ea7\u64cd\u4f5c\u4e0e\u6280\u5de7<\/p>\n<\/p>\n<p><p>NumPy\u4e0d\u4ec5\u53ef\u4ee5\u8fdb\u884c\u57fa\u672c\u7684\u77e9\u9635\u5f15\u7528\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u64cd\u4f5c\u548c\u6280\u5de7\uff0c\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u77e9\u9635\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5e03\u5c14\u7d22\u5f15<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u5e03\u5c14\u6761\u4ef6\u7b5b\u9009\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u77e9\u9635\u4e2d\u6240\u6709\u5927\u4e8e5\u7684\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">greater_than_five = matrix[matrix &gt; 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de<code>[6, 7, 8, 9]<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u77e9\u9635\u8fd0\u7b97<\/strong>\uff1aNumPy\u652f\u6301\u5404\u79cd\u77e9\u9635\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u77e9\u9635\u76f8\u52a0\u3001\u76f8\u4e58\u3001\u8f6c\u7f6e\u7b49\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u76f8\u52a0<\/p>\n<p>matrix_sum = matrix + np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])<\/p>\n<h2><strong>\u77e9\u9635\u76f8\u4e58\uff08\u5143\u7d20\u7ea7\u522b\uff09<\/strong><\/h2>\n<p>matrix_product = matrix * np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])<\/p>\n<h2><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>matrix_transpose = matrix.T<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6539\u53d8\u77e9\u9635\u5f62\u72b6<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>reshape()<\/code>\u65b9\u6cd5\u6539\u53d8\u77e9\u9635\u7684\u5f62\u72b6\u3002\u4f8b\u5982\uff0c\u5c063&#215;3\u77e9\u9635\u53d8\u4e3a1&#215;9\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">reshaped_matrix = matrix.reshape(1, 9)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u77e9\u9635\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u77e9\u9635\u5728\u8bb8\u591a\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u77e9\u9635\u5f62\u5f0f\u5b58\u50a8\u548c\u5904\u7406\u3002NumPy\u662f\u6570\u636e\u5206\u6790\u4e2d\u6700\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong>\uff1a\u56fe\u50cf\u53ef\u4ee5\u770b\u4f5c\u662f\u50cf\u7d20\u503c\u7684\u77e9\u9635\uff0c\u4f7f\u7528NumPy\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u56fe\u50cf\u8fdb\u884c\u64cd\u4f5c\u548c\u53d8\u6362\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/strong>\uff1a\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u6a21\u578b\u7684\u8f93\u5165\u548c\u8f93\u51fa\u901a\u5e38\u4ee5\u77e9\u9635\u5f62\u5f0f\u8868\u793a\u3002NumPy\u7684\u9ad8\u6548\u8fd0\u7b97\u80fd\u529b\u4f7f\u5176\u6210\u4e3a\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u5b9e\u73b0\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\u548cPython\u5185\u7f6e\u7684\u5217\u8868\u5d4c\u5957\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5f15\u7528\u548c\u64cd\u4f5c\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002NumPy\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u57fa\u7840\u7684\u5f15\u7528\u548c\u5207\u7247\u529f\u80fd\uff0c\u8fd8\u652f\u6301\u5e03\u5c14\u7d22\u5f15\u3001\u77e9\u9635\u8fd0\u7b97\u548c\u5f62\u72b6\u53d8\u6362\u7b49\u9ad8\u7ea7\u64cd\u4f5c\u3002\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97NumPy\u5728\u6570\u636e\u5206\u6790\u3001\u56fe\u50cf\u5904\u7406\u548c\u673a\u5668\u5b66\u4e60\u7b49\u9886\u57df\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6211\u4eec\u7684\u6570\u636e\u5904\u7406\u6548\u7387\u548c\u80fd\u529b\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\u901a\u8fc7<code>numpy.array()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u4f8b\u5982\uff0c<code>import numpy as np<\/code>\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matrix = np.array([[1, 2], [3, 4]])<\/code>\u6765\u521b\u5efa\u4e00\u4e2a2&#215;2\u7684\u77e9\u9635\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u8bbf\u95ee\u77e9\u9635\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u8bbf\u95ee\u77e9\u9635\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u540d\u4e3a<code>matrix<\/code>\u76842&#215;2\u77e9\u9635\uff0c\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528<code>matrix[0, 1]<\/code>\u3002\u8bf7\u6ce8\u610f\uff0cPython\u7684\u7d22\u5f15\u662f\u4ece0\u5f00\u59cb\u7684\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5bf9\u77e9\u9635\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\uff1f<\/strong><br \/>\u5207\u7247\u64cd\u4f5c\u5141\u8bb8\u7528\u6237\u9009\u62e9\u77e9\u9635\u7684\u4e00\u4e2a\u5b50\u96c6\u3002\u5bf9\u4e8e\u4e00\u4e2a\u540d\u4e3a<code>matrix<\/code>\u7684\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matrix[0:2, 0:1]<\/code>\u6765\u9009\u62e9\u524d\u4e24\u884c\u548c\u7b2c\u4e00\u5217\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u5408\u63d0\u53d6\u6240\u9700\u7684\u7279\u5b9a\u6570\u636e\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5f15\u7528\u77e9\u9635\u5143\u7d20\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528NumPy\u5e93\u7684\u6570\u7ec4\u3001\u5217\u8868\u5d4c\u5957\u7ed3\u6784\u3001\u901a\u8fc7\u7d22\u5f15\u5f15\u7528\u3002NumPy\u5e93\u662f [&hellip;]","protected":false},"author":3,"featured_media":967754,"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\/967747"}],"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=967747"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967747\/revisions"}],"predecessor-version":[{"id":967757,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967747\/revisions\/967757"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/967754"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=967747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=967747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=967747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}