{"id":179157,"date":"2024-05-08T20:07:06","date_gmt":"2024-05-08T12:07:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179157.html"},"modified":"2024-05-08T20:07:13","modified_gmt":"2024-05-08T12:07:13","slug":"numpy%e7%9a%84%e5%a4%9a%e7%bb%b4%e6%95%b0%e7%bb%84%e5%88%87%e7%89%87%e6%93%8d%e4%bd%9c%e7%94%a8%e5%88%b0%e4%ba%86%e4%bb%80%e4%b9%88python%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/179157.html","title":{"rendered":"Numpy\u7684\u591a\u7ef4\u6570\u7ec4\u5207\u7247\u64cd\u4f5c\u7528\u5230\u4e86\u4ec0\u4e48Python\u8bed\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063816\/8c1ca6ec-451b-4084-8cf7-12017e8965ed.webp\" alt=\"Numpy\u7684\u591a\u7ef4\u6570\u7ec4\u5207\u7247\u64cd\u4f5c\u7528\u5230\u4e86\u4ec0\u4e48Python\u8bed\u6cd5\" \/><\/p>\n<p><p>NumPy\u7684\u591a\u7ef4\u6570\u7ec4\u5207\u7247\u64cd\u4f5c\u4e3b\u8981\u4f9d\u8d56\u4e8ePython\u7684<strong>\u7d22\u5f15<\/strong>\u3001<strong>\u5207\u7247<\/strong>\u548c<strong>\u9ad8\u7ea7\u7d22\u5f15<\/strong>\u4e09\u79cd\u8bed\u6cd5\u673a\u5236\u3002<strong>\u7d22\u5f15<\/strong>\u5141\u8bb8\u4f60\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c<strong>\u5207\u7247<\/strong>\u5219\u662f\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4e00\u6bb5\u8fde\u7eed\u533a\u57df\uff0c\u800c<strong>\u9ad8\u7ea7\u7d22\u5f15<\/strong>\u6d89\u53ca\u4f7f\u7528\u6570\u7ec4\u6216\u6761\u4ef6\u8868\u8fbe\u5f0f\u9009\u62e9\u5143\u7d20\u3002\u7279\u522b\u5730\uff0c\u6570\u7ec4\u7684<strong>\u5207\u7247\u64cd\u4f5c<\/strong>\u662f\u901a\u8fc7\u65b9\u62ec\u53f7<code>[]<\/code>\u5185\u6307\u5b9a\u8303\u56f4\u6765\u5b9e\u73b0\uff0c\u4f7f\u7528<code>:<\/code>\u5b57\u7b26\u5206\u9694\u8d77\u59cb\u4f4d\u7f6e\u3001\u7ec8\u6b62\u4f4d\u7f6e\u548c\u6b65\u957f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u6570\u7ec4\u4efb\u610f\u7ef4\u5ea6\u7684\u8bbf\u95ee\u548c\u5b50\u96c6\u5207\u5272\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>:2<\/code>\u53d6\u5f97\u524d\u4e24\u4e2a\u5143\u7d20\uff0c\u6216\u8005\u7528<code>1:5:2<\/code>\u9009\u53d6\u4ece\u7b2c1\u4e2a\uff08\u5305\u62ec\uff09\u5230\u7b2c5\u4e2a\uff08\u4e0d\u5305\u62ec\uff09\u95f4\u9694\u4e3a2\u7684\u5143\u7d20\u5e8f\u5217\u3002\u5728\u591a\u7ef4\u6570\u7ec4\u4e2d\uff0c\u8fd9\u4e9b\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u6309\u7ef4\u5ea6\u987a\u5e8f\u7ec4\u5408\uff0c\u7528\u9017\u53f7<code>,<\/code>\u9694\u5f00\uff0c\u5b9e\u73b0\u5bf9\u591a\u7ef4\u6570\u636e\u7684\u590d\u6742\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u57fa\u7840\u64cd\u4f5c<\/p>\n<\/p>\n<p><p><strong>\u6570\u7ec4\u7d22\u5f15<\/strong><\/p>\n<\/p>\n<p><p>\u5728NumPy\u4e2d\uff0c\u6570\u7ec4\u7d22\u5f15\u7528\u4e8e\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7279\u5b9a\u7684\u5143\u7d20\u3002\u5982\u679c\u8981\u8bbf\u95ee\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u7ec4\u7d22\u5f15\uff0c\u6bcf\u4e2a\u7d22\u5f15\u5bf9\u5e94\u4e00\u4e2a\u7ef4\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>element = arr_2d[1, 2]  # \u8bbf\u95ee\u7b2c2\u884c\u7b2c3\u5217\u7684\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u884c\u4e0e\u5217\u7684\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u5b50\u96c6\uff0c\u53ef\u4ee5\u662f\u884c\u3001\u5217\u6216\u8005\u5176\u66f4\u9ad8\u7ef4\u5ea6\u7684\u5207\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">row_slice = arr_2d[1, :]  # \u83b7\u53d6\u7b2c2\u884c\u7684\u6240\u6709\u5143\u7d20<\/p>\n<p>col_slice = arr_2d[:, 2]  # \u83b7\u53d6\u7b2c3\u5217\u7684\u6240\u6709\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5207\u7247\u4e0e\u6b65\u957f<\/p>\n<\/p>\n<p><p><strong>\u5e26\u6b65\u957f\u7684\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u6b65\u957f\u5728\u6570\u7ec4\u7684\u5207\u7247\u64cd\u4f5c\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u4ece\u6570\u7ec4\u4e2d\u53d6\u51fa\u95f4\u9694\u5747\u5300\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">step_slice = arr_2d[::2, ::2]  # \u83b7\u53d6\u6570\u7ec4\u4e2d\u9694\u884c\u9694\u5217\u7684\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u591a\u7ef4\u6570\u7ec4\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5bf9\u4e0d\u540c\u7684\u7ef4\u5ea6\u5206\u522b\u6267\u884c\u5207\u7247\u64cd\u4f5c\uff0c\u8fd9\u79cd\u673a\u5236\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_slice = arr_2d[:2, 1:]  # \u83b7\u53d6\u524d\u4e24\u884c\u548c\u4ece\u7b2c2\u5217\u5230\u6700\u540e\u4e00\u5217\u7684\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u9ad8\u7ea7\u7d22\u5f15<\/p>\n<\/p>\n<p><p><strong>\u5e03\u5c14\u7d22\u5f15<\/strong><\/p>\n<\/p>\n<p><p>\u5e03\u5c14\u7d22\u5f15\u662f\u4e00\u79cd\u57fa\u4e8e\u6570\u7ec4\u5143\u7d20\u7684\u6761\u4ef6\u9009\u62e9\u65b9\u6cd5\uff0c<strong>\u975e\u5e38\u9002\u5408\u4e8e\u6761\u4ef6\u7b5b\u9009<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bool_indexing = arr_2d[arr_2d % 2 == 0]  # \u83b7\u53d6\u6570\u7ec4\u4e2d\u6240\u6709\u5076\u6570\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6574\u6570\u6570\u7ec4\u7d22\u5f15<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6574\u6570\u6570\u7ec4\u8fdb\u884c\u7d22\u5f15\u53ef\u4ee5\u83b7\u53d6\u4efb\u610f\u4f4d\u7f6e\u7684\u6570\u636e\uff0c\u751a\u81f3\u53ef\u4ee5\u6539\u53d8\u6570\u636e\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">int_indexing = arr_2d[[2, 0, 1], :]  # \u91cd\u65b0\u6392\u5217\u6570\u7ec4\u7684\u884c\u987a\u5e8f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7d22\u5f15\u4e0e\u5207\u7247\u7684\u7ed3\u5408\u4f7f\u7528<\/p>\n<\/p>\n<p><p><strong>\u6df7\u5408\u7d22\u5f15<\/strong><\/p>\n<\/p>\n<p><p>\u7d22\u5f15\u4e0e\u5207\u7247\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\uff0c\u5b9e\u73b0\u66f4\u52a0\u7cbe\u786e\u7684\u5143\u7d20\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mixed = arr_2d[1, 0:2]  # \u83b7\u53d6\u7b2c2\u884c\u7684\u524d\u4e24\u4e2a\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u9ad8\u7ea7\u7d22\u5f15\u4e0e\u5207\u7247\u7684\u7ed3\u5408<\/strong><\/p>\n<\/p>\n<p><p>\u9ad8\u7ea7\u7d22\u5f15\u548c\u5207\u7247\u4e5f\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\uff0c\u8fbe\u5230\u9ad8\u5ea6\u5b9a\u5236\u5316\u7684\u6570\u636e\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">advanced_combination = arr_2d[[2, 0, 1], 1:3]  # \u83b7\u53d6\u6307\u5b9a\u884c\u548c\u5217\u8303\u56f4\u7684\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p><strong>\u5185\u5b58\u89c6\u56fe\u4e0e\u590d\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6267\u884c\u6570\u7ec4\u5207\u7247\u65f6\u901a\u5e38\u5f97\u5230\u7684\u662f\u6570\u7ec4\u7684\u89c6\u56fe\uff0c\u800c\u4e0d\u662f\u526f\u672c\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4fee\u6539\u8fd9\u4e2a\u89c6\u56fe\uff0c\u539f\u59cb\u6570\u7ec4\u4e5f\u4f1a\u53d1\u751f\u6539\u53d8\u3002<\/p>\n<\/p>\n<p><p><strong>\u8fde\u7eed\u5185\u5b58\u4e0e\u6548\u7387<\/strong><\/p>\n<\/p>\n<p><p>\u7531\u4e8eNumPy\u7684\u5185\u90e8\u4f18\u5316\uff0c\u8fde\u7eed\u5185\u5b58\uff08\u5982\u884c\u5207\u7247\uff09\u7684\u64cd\u4f5c\u6548\u7387\u901a\u5e38\u66f4\u9ad8\uff0c\u9700\u8981\u6ce8\u610f\u5207\u7247\u64cd\u4f5c\u53ef\u80fd\u5bf9\u6027\u80fd\u6709\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>NumPy\u4e2d\u7684\u591a\u7ef4\u6570\u7ec4\u5207\u7247\u64cd\u4f5c\u529f\u80fd\u4e30\u5bcc\u800c\u5f3a\u5927\uff0c\u901a\u8fc7\u638c\u63e1\u7d22\u5f15\u3001\u5207\u7247\u548c\u9ad8\u7ea7\u7d22\u5f15\u7684\u7ec4\u5408\uff0c\u53ef\u4ee5\u65e0\u6bd4\u7075\u6d3b\u5730\u5904\u7406\u6570\u636e\uff0c\u5bf9\u6570\u7ec4\u8fdb\u884c\u67e5\u8be2\u548c\u5b50\u96c6\u6784\u5efa\u3002\u900f\u5f7b\u7406\u89e3\u548c\u8fd0\u7528\u8fd9\u4e9bPython\u8bed\u6cd5\u89c4\u5219\uff0c\u53ef\u6781\u5927\u63d0\u5347\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u5b9e\u7528\u6027\u3002\u8bb0\u5f97\uff0c\u5bf9\u64cd\u4f5c\u7ed3\u679c\u7684\u7406\u89e3\uff0c\u5305\u62ec\u662f\u5426\u751f\u6210\u4e86\u526f\u672c\u7b49\uff0c\u540c\u6837\u91cd\u8981\uff0c\u56e0\u4e3a\u8fd9\u53ef\u80fd\u5f71\u54cd\u7a0b\u5e8f\u7684\u5185\u5b58\u7ba1\u7406\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Python\u4e2d\u7684\u591a\u7ef4\u6570\u7ec4\u5207\u7247\u64cd\u4f5c\u9700\u8981\u4f7f\u7528\u54ea\u4e9b\u8bed\u6cd5\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u591a\u7ef4\u6570\u7ec4\u7684\u5b50\u6570\u7ec4\uff1f<\/strong><br \/>\n\u4f7f\u7528\u5192\u53f7\uff08:\uff09\u64cd\u4f5c\u7b26\u6765\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u3002\u4f8b\u5982\uff0carr[start:end:step]\u8868\u793a\u4ece\u7d22\u5f15start\u5230\u7d22\u5f15end-1\u7684\u5143\u7d20\uff0c\u4ee5step\u6b65\u957f\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u4fee\u6539\u591a\u7ef4\u6570\u7ec4\u7684\u90e8\u5206\u5143\u7d20\uff1f<\/strong><br \/>\n\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u591a\u7ef4\u6570\u7ec4\u7684\u4e00\u4e2a\u5b50\u6570\u7ec4\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u65b0\u6570\u7ec4\u7684\u503c\u6765\u5b9e\u73b0\u5bf9\u539f\u6570\u7ec4\u90e8\u5206\u5143\u7d20\u7684\u4fee\u6539\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u5bf9\u591a\u7ef4\u6570\u7ec4\u7684\u884c\u548c\u5217\u8fdb\u884c\u64cd\u4f5c\uff1f<\/strong><br \/>\n\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u9009\u62e9\u591a\u7ef4\u6570\u7ec4\u7684\u7279\u5b9a\u884c\u6216\u5217\u3002\u4f7f\u7528arr[:, col_index]\u53ef\u4ee5\u9009\u62e9\u7279\u5b9a\u7684\u5217\uff0c\u4f7f\u7528arr[row_index, :]\u53ef\u4ee5\u9009\u62e9\u7279\u5b9a\u7684\u884c\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u8bf7\u6ce8\u610f\uff0c\u4ee5\u4e0a\u53ea\u662f\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u5207\u7247\u64cd\u4f5c\uff0c\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u3001\u6574\u6570\u7d22\u5f15\u7b49\u5176\u4ed6Python\u8bed\u6cd5\u6765\u5b8c\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"NumPy\u7684\u591a\u7ef4\u6570\u7ec4\u5207\u7247\u64cd\u4f5c\u4e3b\u8981\u4f9d\u8d56\u4e8ePython\u7684\u7d22\u5f15\u3001\u5207\u7247\u548c\u9ad8\u7ea7\u7d22\u5f15\u4e09\u79cd\u8bed\u6cd5\u673a\u5236\u3002\u7d22\u5f15\u5141\u8bb8\u4f60\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u7279 [&hellip;]","protected":false},"author":3,"featured_media":179165,"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\/179157"}],"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=179157"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179157\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/179165"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=179157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=179157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=179157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}