{"id":1109658,"date":"2025-01-08T17:12:12","date_gmt":"2025-01-08T09:12:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109658.html"},"modified":"2025-01-08T17:12:14","modified_gmt":"2025-01-08T09:12:14","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8f%96%e6%95%b0%e7%bb%84%e7%9a%84%e4%b8%80%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109658.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u53d6\u6570\u7ec4\u7684\u4e00\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072828\/6834df35-976e-4b82-bdac-155b03edd4da.webp\" alt=\"python\u4e2d\u5982\u4f55\u53d6\u6570\u7ec4\u7684\u4e00\u884c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53d6\u6570\u7ec4\u7684\u4e00\u884c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7d22\u5f15\u3001\u5207\u7247\u3001\u4ee5\u53canumpy\u5e93\u7b49\u3002<\/strong> \u5728Python\u4e2d\uff0c\u6570\u7ec4\uff08array\uff09\u901a\u5e38\u662f\u901a\u8fc7NumPy\u5e93\u521b\u5efa\u7684\u3002NumPy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u4e00\u79cd\u5e38\u7528\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528numpy\u5e93\u4e2d\u7684\u7d22\u5f15\u548c\u5207\u7247\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u4e00\u884c<\/strong>\u3002<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2aPython\u5e93\uff0c\u7528\u4e8e\u652f\u6301\u5927\u578b\u3001\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u7684\u8fd0\u7b97\uff0c\u6b64\u5916\u8fd8\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u6765\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001NUMPY\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5305\u3002\u5b83\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e00\u4e2a\u5f3a\u5927\u7684N\u7ef4\u6570\u7ec4\u5bf9\u8c61 ndarray\u3002<\/li>\n<li>\u5e7f\u64ad\u529f\u80fd\u51fd\u6570\u3002<\/li>\n<li>\u6574\u5408 C\/C++ \u548c Fortran \u4ee3\u7801\u7684\u5de5\u5177\u3002<\/li>\n<li>\u7ebf\u6027\u4ee3\u6570\u3001\u5085\u91cc\u53f6\u53d8\u6362\u3001\u968f\u673a\u6570\u751f\u6210\u7b49\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><p>\u8981\u4f7f\u7528NumPy\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5NumPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efaNUMPY\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5728\u4e86\u89e3\u5982\u4f55\u53d6\u6570\u7ec4\u7684\u4e00\u884c\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u4e86\u89e3\u5982\u4f55\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u521b\u5efaNumPy\u6570\u7ec4\uff0c\u4f8b\u5982\u4f7f\u7528\u5217\u8868\u6216\u5143\u7ec4\u6765\u521b\u5efa\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_1d = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>array_1d<\/code> \u662f\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c<code>array_2d<\/code> \u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u83b7\u53d6\u6570\u7ec4\u7684\u4e00\u884c<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u6570\u7ec4\u7684\u4e00\u884c\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u7d22\u5f15\u6765\u5b9e\u73b0\u3002\u5bf9\u4e8e\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u884c\u7d22\u5f15\u6765\u83b7\u53d6\u8be5\u884c\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e8c\u884c\u6570\u636e\uff08\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u7b2c\u4e8c\u884c\u7d22\u5f15\u4e3a1\uff09<\/strong><\/h2>\n<p>row_1 = array_2d[1]<\/p>\n<p>print(row_1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>array_2d[1]<\/code> \u83b7\u53d6\u7684\u662f\u6570\u7ec4\u7684\u7b2c\u4e8c\u884c\u6570\u636e\uff0c\u7ed3\u679c\u4e3a <code>[4, 5, 6]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u5355\u884c\u6570\u636e\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u591a\u884c\u6570\u636e\u3002\u5207\u7247\u64cd\u4f5c\u5141\u8bb8\u4f60\u6307\u5b9a\u5f00\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\u6765\u83b7\u53d6\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e00\u884c\u548c\u7b2c\u4e8c\u884c\u6570\u636e\uff08\u7d22\u5f150\u52302\uff0c\u4e0d\u5305\u62ec\u7d22\u5f152\uff09<\/strong><\/h2>\n<p>rows_0_to_1 = array_2d[0:2]<\/p>\n<p>print(rows_0_to_1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>array_2d[0:2]<\/code> \u83b7\u53d6\u7684\u662f\u6570\u7ec4\u7684\u7b2c\u4e00\u884c\u548c\u7b2c\u4e8c\u884c\u6570\u636e\uff0c\u7ed3\u679c\u4e3a <code>[[1, 2, 3], [4, 5, 6]]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u9ad8\u7ea7\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u5916\uff0cNumPy\u8fd8\u652f\u6301\u9ad8\u7ea7\u7d22\u5f15\u3002\u9ad8\u7ea7\u7d22\u5f15\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u548c\u82b1\u5f0f\u7d22\u5f15\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1. \u5e03\u5c14\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u5e03\u5c14\u7d22\u5f15\u7528\u4e8e\u901a\u8fc7\u5e03\u5c14\u6761\u4ef6\u6765\u7b5b\u9009\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u6570\u7ec4\u4e2d\u5927\u4e8e\u67d0\u4e2a\u503c\u7684\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u7ec4\u4e2d\u7b2c\u4e8c\u5217\u5927\u4e8e4\u7684\u884c<\/strong><\/h2>\n<p>bool_idx = array_2d[:, 1] &gt; 4<\/p>\n<p>rows_greater_than_4 = array_2d[bool_idx]<\/p>\n<p>print(rows_greater_than_4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>array_2d[:, 1] &gt; 4<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\uff0c\u8868\u793a\u7b2c\u4e8c\u5217\u4e2d\u5927\u4e8e4\u7684\u5143\u7d20\u3002<code>array_2d[bool_idx]<\/code> \u83b7\u53d6\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\uff0c\u7ed3\u679c\u4e3a <code>[[7, 8, 9]]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u82b1\u5f0f\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u82b1\u5f0f\u7d22\u5f15\u7528\u4e8e\u901a\u8fc7\u6574\u6570\u6570\u7ec4\u6765\u6307\u5b9a\u9700\u8981\u83b7\u53d6\u7684\u884c\u6216\u5217\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u6307\u5b9a\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e00\u884c\u548c\u7b2c\u4e09\u884c\u6570\u636e\uff08\u7d22\u5f150\u548c2\uff09<\/strong><\/h2>\n<p>rows_0_and_2 = array_2d[[0, 2]]<\/p>\n<p>print(rows_0_and_2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>array_2d[[0, 2]]<\/code> \u83b7\u53d6\u7684\u662f\u6570\u7ec4\u7684\u7b2c\u4e00\u884c\u548c\u7b2c\u4e09\u884c\u6570\u636e\uff0c\u7ed3\u679c\u4e3a <code>[[1, 2, 3], [7, 8, 9]]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u83b7\u53d6\u6570\u7ec4\u7684\u4e00\u884c\u4e3b\u8981\u901a\u8fc7NumPy\u5e93\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u57fa\u672c\u7684\u7d22\u5f15\u3001\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u5355\u884c\u6216\u591a\u884c\u6570\u636e\u3002\u6b64\u5916\uff0cNumPy\u8fd8\u652f\u6301\u9ad8\u7ea7\u7d22\u5f15\uff0c\u5982\u5e03\u5c14\u7d22\u5f15\u548c\u82b1\u5f0f\u7d22\u5f15\uff0c\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u6570\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528NumPy\u83b7\u53d6\u6570\u7ec4\u7684\u4e00\u884c\u6570\u636e\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u6570\u7ec4\u7684\u7279\u5b9a\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u8f7b\u677e\u63d0\u53d6\u6570\u7ec4\u7684\u7279\u5b9a\u884c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7684\u7d22\u5f15\u6765\u83b7\u53d6\u6240\u9700\u7684\u884c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>array[row_index]<\/code>\u53ef\u4ee5\u8bbf\u95ee\u6307\u5b9a\u7d22\u5f15\u7684\u884c\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u8981\u83b7\u53d6\u7b2c\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>array[0]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u63d0\u53d6\u591a\u884c\u6570\u636e\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u63d0\u53d6\u591a\u4e2a\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u529f\u80fd\u3002\u5207\u7247\u8bed\u6cd5<code>array[start_index:end_index]<\/code>\u5141\u8bb8\u60a8\u83b7\u53d6\u4ece\u5f00\u59cb\u7d22\u5f15\u5230\u7ed3\u675f\u7d22\u5f15\u4e4b\u95f4\u7684\u6240\u6709\u884c\u3002\u4f8b\u5982\uff0c<code>array[1:4]<\/code>\u5c06\u8fd4\u56de\u7b2c\u4e8c\u5230\u7b2c\u56db\u884c\uff08\u4e0d\u5305\u62ec\u7b2c\u56db\u884c\uff09\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u4e8c\u7ef4\u6570\u7ec4\u5e76\u83b7\u53d6\u884c\u6570\u636e\uff1f<\/strong><br \/>\u5bf9\u4e8e\u4e8c\u7ef4\u6570\u7ec4\uff0c\u4f7f\u7528\u5217\u8868\u6216NumPy\u6570\u7ec4\u90fd\u662f\u5e38\u89c1\u7684\u9009\u62e9\u3002\u5982\u679c\u4f7f\u7528\u5217\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8bbf\u95ee\u884c\uff0c\u4f8b\u5982<code>list_of_lists[row_index]<\/code>\u3002\u5982\u679c\u4f7f\u7528NumPy\u6570\u7ec4\uff0c\u9664\u4e86\u4f7f\u7528\u7d22\u5f15\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5229\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u63d0\u53d6\u7b26\u5408\u6761\u4ef6\u7684\u884c\uff0c\u8fd9\u5728\u6570\u636e\u7b5b\u9009\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u6761\u4ef6\u8868\u8fbe\u5f0f\u751f\u6210\u5e03\u5c14\u6570\u7ec4\u5e76\u83b7\u53d6\u5bf9\u5e94\u7684\u884c\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53d6\u6570\u7ec4\u7684\u4e00\u884c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7d22\u5f15\u3001\u5207\u7247\u3001\u4ee5\u53canumpy\u5e93\u7b49\u3002 \u5728Python\u4e2d\uff0c\u6570\u7ec4\uff08 [&hellip;]","protected":false},"author":3,"featured_media":1109664,"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\/1109658"}],"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=1109658"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109658\/revisions"}],"predecessor-version":[{"id":1109667,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109658\/revisions\/1109667"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109664"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}