{"id":1137594,"date":"2025-01-08T21:50:22","date_gmt":"2025-01-08T13:50:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1137594.html"},"modified":"2025-01-08T21:50:24","modified_gmt":"2025-01-08T13:50:24","slug":"python%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","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1137594.html","title":{"rendered":"python\u5982\u4f55\u5c06\u4e00\u7ef4\u6570\u7ec4\u53d8\u6210\u4e8c\u7ef4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101412\/839fc081-b51b-4bc2-954a-74dfbad02664.webp\" alt=\"python\u5982\u4f55\u5c06\u4e00\u7ef4\u6570\u7ec4\u53d8\u6210\u4e8c\u7ef4\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7Python\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53careshape\u65b9\u6cd5\u7b49\u65b9\u5f0f\u3002NumPy\u5e93\u3001reshape\u65b9\u6cd5\u662f\u5176\u4e2d\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u5219\u9002\u7528\u4e8e\u7b80\u5355\u573a\u666f\u3002<\/strong> \u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u4e3e\u4f8b\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u7684\u57fa\u7840\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u4e00\u7ef4\u6570\u7ec4\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5NumPy\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528reshape\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>NumPy\u7684reshape\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4efb\u610f\u5f62\u72b6\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<code>arr<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr_2d = arr.reshape((2, 3))<\/p>\n<p>print(arr_2d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u7ef4\u6570\u7ec4<code>arr<\/code>\u8f6c\u6362\u4e3a\u4e00\u4e2a2&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\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\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u9ad8\u6548\u7684Python\u6784\u9020\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u7b80\u5355\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5217\u8868\u63a8\u5bfc\u5f0f\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868<code>lst<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b\u5b50\u5217\u8868\u7684\u4e8c\u7ef4\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868<\/p>\n<p>lst = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>n = 3  # \u6bcf\u4e2a\u5b50\u5217\u8868\u7684\u957f\u5ea6<\/p>\n<p>lst_2d = [lst[i:i + n] for i in range(0, len(lst), n)]<\/p>\n<p>print(lst_2d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u7ef4\u5217\u8868<code>lst<\/code>\u8f6c\u6362\u4e3a\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u5305\u542b3\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528reshape\u65b9\u6cd5\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u7840\u7684reshape\u7528\u6cd5\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u7075\u6d3b\u5730\u8f6c\u6362\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u81ea\u52a8\u8ba1\u7b97\u7ef4\u5ea6<\/h4>\n<\/p>\n<p><p>NumPy\u7684reshape\u65b9\u6cd5\u5141\u8bb8\u4f60\u6307\u5b9a\u4e00\u4e2a\u7ef4\u5ea6\u4e3a-1\uff0cNumPy\u5c06\u81ea\u52a8\u8ba1\u7b97\u8be5\u7ef4\u5ea6\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<h2><strong>\u4f7f\u7528-1\u81ea\u52a8\u8ba1\u7b97\u7ef4\u5ea6<\/strong><\/h2>\n<p>arr_2d = arr.reshape((-1, 3))<\/p>\n<p>print(arr_2d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u7b2c\u4e8c\u4e2a\u7ef4\u5ea6\u4e3a3\uff0cNumPy\u81ea\u52a8\u8ba1\u7b97\u7b2c\u4e00\u4e2a\u7ef4\u5ea6\u4e3a2\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u4e5f\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Pandas\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528Pandas\u7684DataFrame<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3aPandas\u7684DataFrame\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(arr, columns=[&#39;Value&#39;])<\/p>\n<h2><strong>\u5c06DataFrame\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr_2d = df.values.reshape((2, 3))<\/p>\n<p>print(arr_2d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\uff0c\u7136\u540e\u4f7f\u7528reshape\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>5.1 \u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>NumPy\u7684reshape\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u573a\u666f\u6216\u4e0d\u4f9d\u8d56\u5916\u90e8\u5e93\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>Pandas\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><h4>5.2 \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7ef4\u5ea6\u5339\u914d<\/strong>\uff1a\u786e\u4fdd\u5728\u4f7f\u7528reshape\u65b9\u6cd5\u65f6\uff0c\u6570\u7ec4\u7684\u5143\u7d20\u6570\u91cf\u4e0e\u65b0\u5f62\u72b6\u7684\u5143\u7d20\u6570\u91cf\u5339\u914d\uff0c\u5426\u5219\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0cNumPy\u7684\u6027\u80fd\u901a\u5e38\u4f18\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\u548cPandas\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u9ad8\u6548\u5730\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\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 \/>\u8981\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u4e2d\u7684reshape()\u51fd\u6570\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165NumPy\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4\uff0c\u6700\u540e\u4f7f\u7528reshape()\u65b9\u6cd5\u6307\u5b9a\u65b0\u7684\u5f62\u72b6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u542b6\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a2\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u9664\u4e86NumPy\u7684reshape()\u65b9\u6cd5\uff0cPython\u7684\u6807\u51c6\u5e93\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\uff0c\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u901a\u8fc7\u5c06\u4e00\u7ef4\u5217\u8868\u6309\u6240\u9700\u7684\u884c\u6570\u5206\u7ec4\uff0c\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[[array[i], array[i + 1]] for i in range(0, len(array), 2)]<\/code>\u6765\u5b9e\u73b0\u5206\u7ec4\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u662f\u6709\u7528\u7684\uff1f<\/strong><br \/>\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u5728\u5904\u7406\u77e9\u9635\u8fd0\u7b97\u3001\u56fe\u50cf\u5904\u7406\u3001\u6570\u636e\u5206\u6790\u7b49\u573a\u666f\u4e2d\u7279\u522b\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u6570\u636e\u5e38\u5e38\u4ee5\u4e8c\u7ef4\u6570\u7ec4\u7684\u5f62\u5f0f\u8fdb\u884c\u5904\u7406\uff0c\u4ee5\u4fbf\u5c06\u7279\u5f81\u548c\u6807\u7b7e\u5206\u5f00\uff0c\u4fbf\u4e8e\u6a21\u578b\u8bad\u7ec3\u548c\u6d4b\u8bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7Python\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53careshape\u65b9\u6cd5\u7b49\u65b9\u5f0f\u3002Nu [&hellip;]","protected":false},"author":3,"featured_media":1137599,"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\/1137594"}],"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=1137594"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137594\/revisions"}],"predecessor-version":[{"id":1137601,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137594\/revisions\/1137601"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1137599"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1137594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1137594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1137594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}