{"id":1146028,"date":"2025-01-08T23:13:28","date_gmt":"2025-01-08T15:13:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146028.html"},"modified":"2025-01-08T23:13:31","modified_gmt":"2025-01-08T15:13:31","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%e5%8f%98%e6%88%90%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1146028.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u4e00\u7ef4\u6570\u7ec4\u53d8\u6210\u4e8c\u7ef4\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182311\/b6292139-a8bc-48e2-8b7e-ed9cc56defbe.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u4e00\u7ef4\u6570\u7ec4\u53d8\u6210\u4e8c\u7ef4\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u4e00\u7ef4\u6570\u7ec4\u53d8\u6210\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u7684<code>reshape<\/code>\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53ca\u624b\u52a8\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c<\/strong>\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u6848\u4f8b\u5206\u6790\u3002\u9996\u5148\uff0c\u6211\u4eec\u7b80\u8981\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u6838\u5fc3\u601d\u60f3\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528NumPy\u5e93\u7684reshape\u51fd\u6570\uff0c2. \u5217\u8868\u63a8\u5bfc\u5f0f\uff0c3. \u624b\u52a8\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u91cd\u70b9\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u7684reshape\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e2a\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u7684\u6838\u5fc3\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61<code>ndarray<\/code>\uff0c\u4ee5\u53ca\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\u6765\u64cd\u4f5c\u8fd9\u4e9b\u6570\u7ec4\u3002<code>reshape<\/code>\u51fd\u6570\u662fNumPy\u5e93\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u6539\u53d8\u6570\u636e\u7684\u60c5\u51b5\u4e0b\uff0c\u6539\u53d8\u6570\u7ec4\u7684\u5f62\u72b6\u3002<strong>\u901a\u8fc7\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b9\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b9\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>one_d_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<\/p>\n<h2><strong>\u4f7f\u7528reshape\u51fd\u6570\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a3x3\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>two_d_array = one_d_array.reshape(3, 3)<\/p>\n<p>print(two_d_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u5404\u79cd\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93\u7684reshape\u51fd\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u7684<code>reshape<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u3001\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8\u6570\u636e\u7684\u60c5\u51b5\u4e0b\uff0c\u6539\u53d8\u6570\u7ec4\u7684\u5f62\u72b6\u3002<code>reshape<\/code>\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numpy.reshape(a, newshape, order=&#39;C&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>a<\/code>\uff1a\u9700\u8981\u6539\u53d8\u5f62\u72b6\u7684\u6570\u7ec4\u3002<\/li>\n<li><code>newshape<\/code>\uff1a\u6574\u6570\u6216\u6574\u6570\u5143\u7ec4\uff0c\u8868\u793a\u65b0\u7684\u5f62\u72b6\u3002<\/li>\n<li><code>order<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u8bfb\u53d6\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u9ed8\u8ba4\u4e3a<code>&#39;C&#39;<\/code>\uff08\u6309\u884c\u987a\u5e8f\uff09\u3002<\/li>\n<\/ul>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b12\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>one_d_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])<\/p>\n<h2><strong>\u4f7f\u7528reshape\u51fd\u6570\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a3x4\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>two_d_array = one_d_array.reshape(3, 4)<\/p>\n<p>print(&quot;\u539f\u59cb\u4e00\u7ef4\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(one_d_array)<\/p>\n<p>print(&quot;\\n\u8f6c\u6362\u540e\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(two_d_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b12\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a3&#215;4\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>\u539f\u59cb\u4e00\u7ef4\u6570\u7ec4\uff1a<\/p>\n<p>[ 1  2  3  4  5  6  7  8  9 10 11 12]<\/p>\n<p>\u8f6c\u6362\u540e\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<p>[[ 1  2  3  4]<\/p>\n<p> [ 5  6  7  8]<\/p>\n<p> [ 9 10 11 12]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u521b\u5efa\u5217\u8868\u7684\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5c06\u4e00\u7ef4\u6570\u7ec4\u5212\u5206\u4e3a\u591a\u4e2a\u5b50\u6570\u7ec4\uff0c\u6bcf\u4e2a\u5b50\u6570\u7ec4\u5305\u542b\u76f8\u540c\u6570\u91cf\u7684\u5143\u7d20\u3002<\/li>\n<li>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e9b\u5b50\u6570\u7ec4\u7ec4\u5408\u6210\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/li>\n<\/ol>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b12\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4<\/p>\n<p>one_d_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a3x4\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>rows = 3<\/p>\n<p>cols = 4<\/p>\n<p>two_d_array = [one_d_array[i * cols:(i + 1) * cols] for i in range(rows)]<\/p>\n<p>print(&quot;\u539f\u59cb\u4e00\u7ef4\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(one_d_array)<\/p>\n<p>print(&quot;\\n\u8f6c\u6362\u540e\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(two_d_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b12\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5176\u8f6c\u6362\u4e3a3&#215;4\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>\u539f\u59cb\u4e00\u7ef4\u6570\u7ec4\uff1a<\/p>\n<p>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]<\/p>\n<p>\u8f6c\u6362\u540e\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<p>[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u624b\u52a8\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528NumPy\u5e93\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982\u524d\u4e24\u79cd\u65b9\u6cd5\u7b80\u6d01\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u66f4\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u624b\u52a8\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b12\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4<\/p>\n<p>one_d_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]<\/p>\n<h2><strong>\u624b\u52a8\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a3x4\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>rows = 3<\/p>\n<p>cols = 4<\/p>\n<p>two_d_array = []<\/p>\n<p>for i in range(rows):<\/p>\n<p>    row = []<\/p>\n<p>    for j in range(cols):<\/p>\n<p>        row.append(one_d_array[i * cols + j])<\/p>\n<p>    two_d_array.append(row)<\/p>\n<p>print(&quot;\u539f\u59cb\u4e00\u7ef4\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(one_d_array)<\/p>\n<p>print(&quot;\\n\u8f6c\u6362\u540e\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(two_d_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b12\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\uff0c\u5e76\u901a\u8fc7\u624b\u52a8\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u5c06\u5176\u8f6c\u6362\u4e3a3&#215;4\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>\u539f\u59cb\u4e00\u7ef4\u6570\u7ec4\uff1a<\/p>\n<p>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]<\/p>\n<p>\u8f6c\u6362\u540e\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<p>[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1. \u6570\u636e\u5904\u7406\u548c\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u7ef4\u7684\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u7684\u77e9\u9635\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6279\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u8868\u793a\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff08\u6216\u77e9\u9635\uff09\u3002\u5982\u679c\u6211\u4eec\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u56fe\u50cf\u6570\u636e\uff0c\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u7ef4\u6570\u7ec4\uff0c\u6b64\u65f6\u6211\u4eec\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>3. \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u91cf\u548c\u5f62\u72b6\u5339\u914d<\/strong>\uff1a\u786e\u4fdd\u8f6c\u6362\u524d\u540e\u6570\u636e\u91cf\u5339\u914d\uff0c\u4f8b\u5982\uff0c\u957f\u5ea6\u4e3a12\u7684\u4e00\u7ef4\u6570\u7ec4\u53ef\u4ee5\u8f6c\u6362\u4e3a3&#215;4\u62164&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u4f46\u4e0d\u80fd\u8f6c\u6362\u4e3a3&#215;5\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/li>\n<li><strong>\u5185\u5b58\u6d88\u8017<\/strong>\uff1a\u5927\u89c4\u6a21\u6570\u636e\u8f6c\u6362\u65f6\uff0c\u6ce8\u610f\u5185\u5b58\u6d88\u8017\u548c\u6027\u80fd\u5f71\u54cd\u3002NumPy\u7684<code>reshape<\/code>\u51fd\u6570\u5728\u6027\u80fd\u548c\u5185\u5b58\u7ba1\u7406\u4e0a\u6709\u663e\u8457\u4f18\u52bf\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1. \u52a8\u6001\u786e\u5b9a\u6570\u7ec4\u5f62\u72b6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u901a\u7528\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u957f\u5ea6\u52a8\u6001\u786e\u5b9a\u6570\u7ec4\u5f62\u72b6\uff0c\u5e76\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def reshape_array(arr, rows, cols):<\/p>\n<p>    if len(arr) != rows * cols:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u6570\u7ec4\u957f\u5ea6\u4e0e\u76ee\u6807\u5f62\u72b6\u4e0d\u5339\u914d&quot;)<\/p>\n<p>    return np.array(arr).reshape(rows, cols)<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>one_d_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]<\/p>\n<p>print(reshape_array(one_d_array, 3, 4))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5904\u7406\u4e0d\u89c4\u5219\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u4e0d\u89c4\u5219\u6570\u636e\uff0c\u5373\u65e0\u6cd5\u6574\u9f50\u5212\u5206\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u7684\u60c5\u51b5\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u586b\u5145\u7f3a\u5931\u503c\u6216\u88c1\u526a\u591a\u4f59\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def reshape_irregular_array(arr, rows, cols):<\/p>\n<p>    if len(arr) &lt; rows * cols:<\/p>\n<p>        # \u586b\u5145\u7f3a\u5931\u503c<\/p>\n<p>        arr.extend([None] * (rows * cols - len(arr)))<\/p>\n<p>    elif len(arr) &gt; rows * cols:<\/p>\n<p>        # \u88c1\u526a\u591a\u4f59\u6570\u636e<\/p>\n<p>        arr = arr[:rows * cols]<\/p>\n<p>    return np.array(arr).reshape(rows, cols)<\/p>\n<h2><strong>\u6d4b\u8bd5<\/strong><\/h2>\n<p>one_d_array = [1, 2, 3, 4, 5, 6, 7]<\/p>\n<p>print(reshape_irregular_array(one_d_array, 3, 3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u662f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u7684\u5e38\u89c1\u9700\u6c42\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5b9e\u73b0\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u7684<code>reshape<\/code>\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53ca\u624b\u52a8\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002\u53ef\u4ee5\u5229\u7528<code>reshape()<\/code>\u51fd\u6570\uff0c\u6307\u5b9a\u6240\u9700\u7684\u7ef4\u5ea6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<code>array_1d<\/code>\uff0c\u60f3\u5c06\u5176\u8f6c\u6362\u4e3a2\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\narray_1d = np.array([1, 2, 3, 4, 5, 6])\narray_2d = array_1d.reshape(2, 3)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b2\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u4e00\u7ef4\u6570\u7ec4\u65f6\uff0c\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>reshape()<\/code>\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u65b0\u5f62\u72b6\u7684\u5143\u7d20\u603b\u6570\u4e0e\u539f\u59cb\u6570\u7ec4\u7684\u5143\u7d20\u603b\u6570\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5305\u542b6\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u4efb\u4f55\u5f62\u72b6\uff0c\u5176\u5143\u7d20\u603b\u6570\u4e3a6\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u5426\u5219\uff0c\u5c06\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u5b9e\u73b0\u4e00\u7ef4\u5230\u4e8c\u7ef4\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0cPython\u7684\u6807\u51c6\u5e93\u4e2d\u4e5f\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e8c\u7ef4\u5217\u8868\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982NumPy\u9ad8\u6548\uff0c\u4f46\u5728\u4e0d\u9700\u8981\u5b89\u88c5\u989d\u5916\u5e93\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7b80\u5355\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">array_1d = [1, 2, 3, 4, 5, 6]\narray_2d = [array_1d[i:i + 3] for i in range(0, len(array_1d), 3)]\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u6bcf\u884c3\u4e2a\u5143\u7d20\u7684\u4e8c\u7ef4\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u4e00\u7ef4\u6570\u7ec4\u53d8\u6210\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u7684reshape\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc [&hellip;]","protected":false},"author":3,"featured_media":1146040,"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\/1146028"}],"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=1146028"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146028\/revisions"}],"predecessor-version":[{"id":1146044,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146028\/revisions\/1146044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1146040"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1146028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1146028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1146028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}