{"id":1120234,"date":"2025-01-08T18:54:02","date_gmt":"2025-01-08T10:54:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120234.html"},"modified":"2025-01-08T18:54:05","modified_gmt":"2025-01-08T10:54:05","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e5%a4%9a%e7%bb%b4%e6%95%b0%e7%bb%84%e8%bd%ac%e6%8d%a2%e6%88%90%e5%88%97%e8%a1%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120234.html","title":{"rendered":"python\u5982\u4f55\u628a\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082957\/a4cee1f2-f893-4496-9879-a2590d040ec2.webp\" alt=\"python\u5982\u4f55\u628a\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528numpy\u7684flatten()\u65b9\u6cd5\u3001\u4f7f\u7528numpy\u7684ravel()\u65b9\u6cd5\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n()\u3002\u5176\u4e2d\uff0c\u4f7f\u7528numpy\u7684flatten()\u65b9\u6cd5\u6700\u4e3a\u5e38\u7528\u3002<\/strong> \u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528numpy\u7684flatten()\u65b9\u6cd5\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528numpy\u7684flatten()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u8981\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u7684flatten()\u65b9\u6cd5\u3002flatten()\u65b9\u6cd5\u4f1a\u5c06\u591a\u7ef4\u6570\u7ec4\u5c55\u5e73\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u518d\u8f6c\u6362\u6210\u5217\u8868\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>multi_dim_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u4f7f\u7528flatten()\u65b9\u6cd5\u5c06\u591a\u7ef4\u6570\u7ec4\u5c55\u5e73\u6210\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>flattened_array = multi_dim_array.flatten()<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868<\/strong><\/h2>\n<p>result_list = flattened_array.tolist()<\/p>\n<p>print(result_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>numpy\u7684flatten()\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u7b80\u5355\u6613\u7528\u4e14\u6548\u7387\u9ad8\uff0c\u9002\u7528\u4e8e\u7edd\u5927\u591a\u6570\u60c5\u51b5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528numpy\u7684ravel()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>numpy\u5e93\u4e2d\u7684ravel()\u65b9\u6cd5\u4e0eflatten()\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4e5f\u53ef\u4ee5\u5c06\u591a\u7ef4\u6570\u7ec4\u5c55\u5e73\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u6210\u5217\u8868\u3002ravel()\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u89c6\u56fe\uff08view\uff09\uff0c\u800c\u4e0d\u662f\u526f\u672c\uff08copy\uff09\uff0c\u56e0\u6b64\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>multi_dim_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u4f7f\u7528ravel()\u65b9\u6cd5\u5c06\u591a\u7ef4\u6570\u7ec4\u5c55\u5e73\u6210\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>raveled_array = multi_dim_array.ravel()<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868<\/strong><\/h2>\n<p>result_list = raveled_array.tolist()<\/p>\n<p>print(result_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u975e\u5e38Pythonic\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868\u3002\u901a\u8fc7\u5d4c\u5957\u7684\u5217\u8868\u89e3\u6790\uff0c\u53ef\u4ee5\u9010\u5c42\u5c55\u5f00\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4<\/p>\n<p>multi_dim_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868<\/strong><\/h2>\n<p>result_list = [item for sublist in multi_dim_array for item in sublist]<\/p>\n<p>print(result_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5217\u8868\u89e3\u6790\u7684\u4f18\u70b9\u5728\u4e8e\u65e0\u9700\u989d\u5916\u7684\u5e93\uff0c\u975e\u5e38\u76f4\u89c2\u4e14\u4ee3\u7801\u7b80\u6d01\uff0c\u4f46\u5728\u5904\u7406\u9ad8\u7ef4\u6570\u7ec4\u65f6\u4f1a\u663e\u5f97\u590d\u6742\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528itertools.chain()<\/p>\n<\/p>\n<p><p>itertools.chain()\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u8fed\u4ee3\u5bf9\u8c61\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u8fde\u7eed\u7684\u8fed\u4ee3\u5668\u3002\u901a\u8fc7\u5c06\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u5b50\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9itertools.chain()\uff0c\u53ef\u4ee5\u5c06\u5176\u5c55\u5e73\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u518d\u8f6c\u6362\u6210\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>multi_dim_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u4f7f\u7528itertools.chain()\u5c06\u591a\u7ef4\u6570\u7ec4\u5c55\u5e73\u6210\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>flattened_array = list(itertools.chain(*multi_dim_array))<\/p>\n<p>print(flattened_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528numpy\u7684flatten()\u65b9\u6cd5\u3002\u5176\u4ed6\u65b9\u6cd5\u5982numpy\u7684ravel()\u65b9\u6cd5\u3001\u5217\u8868\u89e3\u6790\u548citertools.chain()\u4e5f\u5404\u6709\u5176\u4f18\u70b9\u3002\u5728\u9009\u62e9\u5177\u4f53\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u548c\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868\u7684\u6838\u5fc3\u601d\u60f3\u90fd\u662f\u5c06\u6570\u7ec4\u5c55\u5e73\u6210\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8f6c\u6362\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\uff1f<\/strong><br \/>\u8981\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy<\/code>\u5e93\u4e2d\u7684<code>flatten()<\/code>\u65b9\u6cd5\uff0c\u6216\u8005\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u4ece\u800c\u5c06\u5176\u6dfb\u52a0\u5230\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u7b80\u5355\u5730\u5b9e\u73b0\u591a\u7ef4\u6570\u7ec4\u7684\u6241\u5e73\u5316\u3002<\/p>\n<p><strong>\u4f7f\u7528numpy\u8fdb\u884c\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6709\u54ea\u4e9b\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528<code>numpy<\/code>\u5e93\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u5177\u6709\u8bb8\u591a\u4f18\u52bf\uff0c\u5305\u62ec\u9ad8\u6548\u7684\u5b58\u50a8\u548c\u8ba1\u7b97\u80fd\u529b\u3002<code>numpy<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u51fd\u6570\u6765\u5904\u7406\u6570\u7ec4\uff0c\u4f8b\u5982<code>reshape()<\/code>\u3001<code>ravel()<\/code>\u7b49\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u5730\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u5176\u4ed6\u5f62\u72b6\u7684\u6570\u7ec4\u3002\u540c\u65f6\uff0c<code>numpy<\/code>\u8fd8\u4f18\u5316\u4e86\u6027\u80fd\uff0c\u4f7f\u5f97\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u66f4\u4e3a\u9ad8\u6548\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684\u5217\u8868\u5982\u4f55\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\uff1f<\/strong><br \/>\u4e00\u65e6\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u6392\u5e8f\u3001\u8fc7\u6ee4\u3001\u6620\u5c04\u7b49\u3002Python\u7684\u5185\u7f6e\u51fd\u6570\u5982<code>map()<\/code>\u3001<code>filter()<\/code>\u53ef\u4ee5\u4e0e\u5217\u8868\u7ed3\u5408\u4f7f\u7528\uff0c\u6216\u8005\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u7528\u6237\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\uff0c\u4ee5\u4fbf\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u6210\u5217\u8868\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528numpy\u7684flatten()\u65b9\u6cd5\u3001\u4f7f\u7528numpy\u7684ravel [&hellip;]","protected":false},"author":3,"featured_media":1120236,"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\/1120234"}],"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=1120234"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120234\/revisions"}],"predecessor-version":[{"id":1120242,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120234\/revisions\/1120242"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1120236"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}