{"id":964587,"date":"2024-12-27T04:29:40","date_gmt":"2024-12-26T20:29:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/964587.html"},"modified":"2024-12-27T04:29:43","modified_gmt":"2024-12-26T20:29:43","slug":"python%e5%a6%82%e4%bd%95%e9%9a%94%e8%a1%8c%e6%8f%90%e5%8f%96%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/964587.html","title":{"rendered":"python\u5982\u4f55\u9694\u884c\u63d0\u53d6\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181319\/d520de94-c375-4219-8660-f8b64416c4de.webp\" alt=\"python\u5982\u4f55\u9694\u884c\u63d0\u53d6\u77e9\u9635\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u5217\u8868\u89e3\u6790\u3001NumPy\u5e93\u7b49\u65b9\u6cd5\u5b9e\u73b0\u77e9\u9635\u7684\u9694\u884c\u63d0\u53d6\u3002\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u7b80\u6d01\u5730\u63d0\u53d6\u9700\u8981\u7684\u884c\u3001NumPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\u3001\u5217\u8868\u89e3\u6790\u5219\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u5904\u7406\u65b9\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0cNumPy\u5e93\u662f\u5904\u7406\u77e9\u9635\u548c\u6570\u7ec4\u7684\u6807\u51c6\u5de5\u5177\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u8fdb\u884c\u5404\u79cd\u6570\u636e\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5207\u7247\u662f\u4e00\u79cd\u5e38\u7528\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u5217\u8868\u6216\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u5bf9\u4e8e\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff08\u77e9\u9635\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u76f4\u63a5\u63d0\u53d6\u6240\u9700\u7684\u884c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u6b65\u957f\u4e3a2\u7684\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u5b9e\u73b0\u9694\u884c\u63d0\u53d6\u3002<\/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>    [10, 11, 12]<\/p>\n<p>]<\/p>\n<h2><strong>\u9694\u884c\u63d0\u53d6<\/strong><\/h2>\n<p>result = matrix[::2]<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>matrix[::2]<\/code>\u8868\u793a\u4ece\u7b2c0\u884c\u5f00\u59cb\uff0c\u6bcf\u9694\u4e00\u884c\u63d0\u53d6\u4e00\u6b21\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f<code>[[1, 2, 3], [7, 8, 9]]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u8fdb\u884c\u77e9\u9635\u64cd\u4f5c\uff0c\u5305\u62ec\u9694\u884c\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff08\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u6570\u7ec4\u7684\u5207\u7247\u529f\u80fd\u8fdb\u884c\u9694\u884c\u63d0\u53d6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efaNumPy\u6570\u7ec4<\/strong><\/h2>\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>    [10, 11, 12]<\/p>\n<p>])<\/p>\n<h2><strong>\u9694\u884c\u63d0\u53d6<\/strong><\/h2>\n<p>result = matrix[::2, :]<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>matrix[::2, :]<\/code>\u8868\u793a\u5bf9\u884c\u8fdb\u884c\u6b65\u957f\u4e3a2\u7684\u5207\u7247\uff0c\u540c\u65f6\u4fdd\u7559\u6240\u6709\u5217\uff0c\u7ed3\u679c\u662f<code>[[1, 2, 3], [7, 8, 9]]<\/code>\u3002NumPy\u7684\u5207\u7247\u64cd\u4f5c\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u5177\u6709\u66f4\u9ad8\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662fPython\u4e2d\u4e00\u79cd\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u6784\u5efa\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u77e9\u9635\u7684\u9694\u884c\u63d0\u53d6\u3002\u901a\u8fc7\u5728\u5217\u8868\u89e3\u6790\u4e2d\u5f15\u5165\u6761\u4ef6\u6216\u6b65\u957f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u77e9\u9635\u884c\u7684\u9009\u62e9\u3002<\/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>    [10, 11, 12]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u8fdb\u884c\u9694\u884c\u63d0\u53d6<\/strong><\/h2>\n<p>result = [matrix[i] for i in range(0, len(matrix), 2)]<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>[matrix[i] for i in range(0, len(matrix), 2)]<\/code>\u901a\u8fc7\u6b65\u957f\u4e3a2\u7684<code>range<\/code>\u51fd\u6570\u5b9e\u73b0\u9694\u884c\u63d0\u53d6\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u540c\u6837\u662f<code>[[1, 2, 3], [7, 8, 9]]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u8003\u8651<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u9886\u57df\uff0c\u77e9\u9635\u548c\u6570\u7ec4\u662f\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u9694\u884c\u63d0\u53d6\u5728\u6570\u636e\u9884\u5904\u7406\u3001\u7279\u5f81\u9009\u62e9\u7b49\u73af\u8282\u4e2d\u53ef\u80fd\u4f1a\u7528\u5230\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u9884\u5904\u7406<\/strong>\uff1a\u5728\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u62bd\u53d6\u7279\u5b9a\u95f4\u9694\u7684\u6570\u636e\u70b9\u4ee5\u7b80\u5316\u6a21\u578b\u6216\u51cf\u5c11\u8ba1\u7b97\u91cf\u3002\u9694\u884c\u63d0\u53d6\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u79cd\u6570\u636e\u4e0b\u91c7\u6837\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7279\u5f81\u9009\u62e9<\/strong>\uff1a\u5728\u9ad8\u7ef4\u6570\u636e\u96c6\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u9009\u62e9\u7279\u5b9a\u7684\u884c\u6216\u5217\u8fdb\u884c\u5206\u6790\uff0c\u9694\u884c\u63d0\u53d6\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u9009\u62e9\u611f\u5174\u8da3\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u5efa\u8bae\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b83\u662f\u4e13\u95e8\u4e3a\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u4f18\u5316\u7684\uff0c\u80fd\u591f\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u8ba1\u7b97\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5176\u4ed6\u76f8\u5173\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u9694\u884c\u63d0\u53d6\uff0c\u5728\u5904\u7406\u77e9\u9635\u65f6\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u8fdb\u884c\u5176\u4ed6\u76f8\u5173\u64cd\u4f5c\uff0c\u4f8b\u5982\u9694\u5217\u63d0\u53d6\u3001\u5b50\u77e9\u9635\u63d0\u53d6\u3001\u77e9\u9635\u8f6c\u7f6e\u7b49\u3002\u4e0b\u9762\u7b80\u8981\u4ecb\u7ecd\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u9694\u5217\u63d0\u53d6<\/strong>\uff1a\u7c7b\u4f3c\u4e8e\u9694\u884c\u63d0\u53d6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6216NumPy\u5e93\u5b9e\u73b0\u9694\u5217\u63d0\u53d6\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u9694\u5217\u63d0\u53d6<\/p>\n<p>result_columns = matrix[:, ::2]<\/p>\n<p>print(result_columns)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b50\u77e9\u9635\u63d0\u53d6<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u83b7\u53d6\u77e9\u9635\u7684\u4efb\u610f\u5b50\u77e9\u9635\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u5b50\u77e9\u9635<\/p>\n<p>sub_matrix = matrix[1:3, 0:2]<\/p>\n<p>print(sub_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong>\uff1aNumPy\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u8f6c\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u77e9\u9635\u8f6c\u7f6e\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u8f6c\u7f6e<\/p>\n<p>transposed_matrix = matrix.T<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u66f4\u597d\u5730\u8fdb\u884c\u77e9\u9635\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u9694\u884c\u63d0\u53d6\u3002\u7075\u6d3b\u8fd0\u7528\u5207\u7247\u64cd\u4f5c\u3001\u5217\u8868\u89e3\u6790\u548cNumPy\u5e93\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u7b80\u6d01\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u9694\u884c\u63d0\u53d6\u77e9\u9635\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u8f7b\u677e\u5b9e\u73b0\u9694\u884c\u63d0\u53d6\u77e9\u9635\u7684\u529f\u80fd\u3002\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\uff0c\u53ef\u4ee5\u9009\u53d6\u6240\u9700\u7684\u884c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u53d6\u4e00\u4e2a\u77e9\u9635\u7684\u6bcf\u9694\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matrix[::2]<\/code>\u6765\u8fbe\u5230\u76ee\u7684\u3002\u8fd9\u79cd\u65b9\u5f0f\u65e2\u7b80\u6d01\u53c8\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u9002\u5408\u65b0\u624b\u4f7f\u7528\u7684\u793a\u4f8b\u4ee3\u7801\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528NumPy\u8fdb\u884c\u9694\u884c\u63d0\u53d6\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u77e9\u9635\nmatrix = np.array([[1, 2, 3],\n                   [4, 5, 6],\n                   [7, 8, 9],\n                   [10, 11, 12]])\n\n# \u9694\u884c\u63d0\u53d6\nresult = matrix[::2]\nprint(result)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u77e9\u9635\u7684\u7b2c\u4e00\u548c\u7b2c\u4e09\u884c\uff0c\u65b9\u4fbf\u65b0\u624b\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5728\u63d0\u53d6\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u7a7a\u77e9\u9635\u6216\u7279\u6b8a\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5904\u7406\u77e9\u9635\u65f6\uff0c\u786e\u4fdd\u8fdb\u884c\u7a7a\u77e9\u9635\u7684\u68c0\u67e5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u5728\u63d0\u53d6\u524d\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">if matrix.size &gt; 0:\n    result = matrix[::2]\nelse:\n    result = &quot;\u77e9\u9635\u4e3a\u7a7a&quot;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u4ee3\u7801\u7684\u9c81\u68d2\u6027\uff0c\u907f\u514d\u4e86\u5728\u5904\u7406\u7a7a\u77e9\u9635\u65f6\u51fa\u73b0\u9519\u8bef\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5b89\u5168\u5730\u8fdb\u884c\u9694\u884c\u63d0\u53d6\uff0c\u5e76\u5bf9\u7279\u6b8a\u60c5\u51b5\u505a\u51fa\u9002\u5f53\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u5217\u8868\u89e3\u6790\u3001NumPy\u5e93\u7b49\u65b9\u6cd5\u5b9e\u73b0\u77e9\u9635\u7684\u9694\u884c\u63d0\u53d6\u3002\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u7b80\u6d01\u5730\u63d0\u53d6 [&hellip;]","protected":false},"author":3,"featured_media":964598,"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\/964587"}],"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=964587"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964587\/revisions"}],"predecessor-version":[{"id":964599,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964587\/revisions\/964599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/964598"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=964587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=964587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=964587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}