{"id":1137991,"date":"2025-01-08T21:54:52","date_gmt":"2025-01-08T13:54:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1137991.html"},"modified":"2025-01-08T21:54:57","modified_gmt":"2025-01-08T13:54:57","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%89%e7%bb%b4%e6%95%b0%e7%bb%84%e5%8f%98%e4%b8%80%e7%bb%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1137991.html","title":{"rendered":"python\u5982\u4f55\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u4e00\u7ef4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101717\/add2fe18-7c22-4ffe-8b55-c0c68a953f6a.webp\" alt=\"python\u5982\u4f55\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u4e00\u7ef4\" \/><\/p>\n<p><p> <strong>\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u6210\u4e00\u7ef4\u6570\u7ec4\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1anumpy.flatten()\u3001numpy.ravel()\u3001\u5217\u8868\u89e3\u6790\u548c\u4f7f\u7528\u9012\u5f52<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528 <code>numpy.flatten()<\/code> \u65b9\u6cd5\u6700\u4e3a\u7b80\u5355\u548c\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u76f4\u63a5\u8c03\u7528\u5e95\u5c42\u7684 C \u4ee3\u7801\u8fdb\u884c\u64cd\u4f5c\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001numpy.flatten()<\/h3>\n<\/p>\n<p><p><code>numpy.flatten()<\/code> \u65b9\u6cd5\u662f\u5c06\u591a\u7ef4\u6570\u7ec4\u53d8\u4e3a\u4e00\u7ef4\u6570\u7ec4\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u7684\u62f7\u8d1d\uff0c\u539f\u6570\u7ec4\u7684\u7ef4\u5ea6\u4fe1\u606f\u5c06\u4e22\u5931\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])<\/p>\n<h2><strong>\u4f7f\u7528 flatten \u65b9\u6cd5\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_1d = array_3d.flatten()<\/p>\n<p>print(array_1d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>array_3d<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u4f7f\u7528 <code>flatten()<\/code> \u65b9\u6cd5\u540e\uff0c\u5f97\u5230\u7684 <code>array_1d<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001numpy.ravel()<\/h3>\n<\/p>\n<p><p><code>numpy.ravel()<\/code> \u65b9\u6cd5\u4e0e <code>flatten()<\/code> \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u89c6\u56fe\uff08view\uff09\u800c\u4e0d\u662f\u62f7\u8d1d\uff08copy\uff09\uff0c\u5982\u679c\u53ef\u80fd\u7684\u8bdd\u3002\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u5982\u679c\u539f\u6570\u7ec4\u88ab\u4fee\u6539\uff0c\u89c6\u56fe\u4e5f\u4f1a\u88ab\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])<\/p>\n<h2><strong>\u4f7f\u7528 ravel \u65b9\u6cd5\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_1d = array_3d.ravel()<\/p>\n<p>print(array_1d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e <code>flatten()<\/code> \u65b9\u6cd5\u4e0d\u540c\uff0c<code>ravel()<\/code> \u65b9\u6cd5\u66f4\u9002\u5408\u5728\u9700\u8981\u8282\u7701\u5185\u5b58\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c06\u4efb\u610f\u7ef4\u5ea6\u7684\u6570\u7ec4\u53d8\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u4f46\u76f8\u5bf9\u6765\u8bf4\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4<\/p>\n<p>array_3d = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_1d = [element for sublist1 in array_3d for sublist2 in sublist1 for element in sublist2]<\/p>\n<p>print(array_1d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5728\u5904\u7406\u5927\u6570\u7ec4\u65f6\u6027\u80fd\u8f83\u5dee\uff0c\u4e0d\u63a8\u8350\u7528\u4e8e\u9ad8\u6027\u80fd\u9700\u6c42\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9012\u5f52<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u9002\u5408\u5904\u7406\u4efb\u610f\u7ef4\u5ea6\u7684\u6570\u7ec4\uff0c\u7279\u522b\u662f\u5f53\u7ef4\u5ea6\u6570\u4e0d\u786e\u5b9a\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def flatten_array(array):<\/p>\n<p>    result = []<\/p>\n<p>    for element in array:<\/p>\n<p>        if isinstance(element, list):<\/p>\n<p>            result.extend(flatten_array(element))<\/p>\n<p>        else:<\/p>\n<p>            result.append(element)<\/p>\n<p>    return result<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_3d = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]<\/p>\n<h2><strong>\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_1d = flatten_array(array_3d)<\/p>\n<p>print(array_1d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7075\u6d3b\uff0c\u4f46\u9012\u5f52\u8c03\u7528\u7684\u6df1\u5ea6\u53d7\u9650\u4e8e Python \u7684\u9012\u5f52\u6df1\u5ea6\u9650\u5236\uff0c\u5728\u5904\u7406\u975e\u5e38\u6df1\u7684\u5d4c\u5957\u6570\u7ec4\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u4e3a\u4e00\u7ef4\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong><code>numpy.flatten()<\/code> \u548c <code>numpy.ravel()<\/code> \u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u9009\u62e9<\/strong>\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002<strong>\u5217\u8868\u89e3\u6790\u548c\u9012\u5f52\u65b9\u6cd5\u867d\u7136\u7075\u6d3b\uff0c\u4f46\u5728\u6027\u80fd\u548c\u590d\u6742\u5ea6\u4e0a\u4e0d\u5982\u524d\u4e24\u8005<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u6570\u7ec4\u7ef4\u5ea6\u8f6c\u6362\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e09\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u7684<code>reshape<\/code>\u51fd\u6570\u6216<code>flatten<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e09\u7ef4\u6570\u7ec4\u5230\u4e00\u7ef4\u6570\u7ec4\u7684\u8f6c\u6362\u3002\u4f7f\u7528<code>reshape<\/code>\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u6570\u7ec4\u7684\u5f62\u72b6\uff0c\u800c<code>flatten<\/code>\u5219\u662f\u76f4\u63a5\u8fd4\u56de\u4e00\u7ef4\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\narray_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n\n# \u4f7f\u7528flatten\u65b9\u6cd5\narray_1d_flatten = array_3d.flatten()\n\n# \u6216\u8005\u4f7f\u7528reshape\u65b9\u6cd5\narray_1d_reshape = array_3d.reshape(-1)\n\nprint(array_1d_flatten)  # \u8f93\u51fa: [ 1  2  3  4  5  6  7  8  9 10 11 12]\nprint(array_1d_reshape)  # \u8f93\u51fa: [ 1  2  3  4  5  6  7  8  9 10 11 12]\n<\/code><\/pre>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4f1a\u4e22\u5931\u6570\u636e\u5417\uff1f<\/strong><br \/>\u4e0d\u4f1a\uff0c\u4f7f\u7528<code>flatten<\/code>\u6216<code>reshape<\/code>\u65b9\u6cd5\u65f6\uff0c\u6570\u636e\u4e0d\u4f1a\u4e22\u5931\u3002\u5b83\u4eec\u53ea\u662f\u6539\u53d8\u4e86\u6570\u7ec4\u7684\u5f62\u72b6\uff0c\u800c\u4e0d\u6539\u53d8\u6570\u7ec4\u4e2d\u7684\u5b9e\u9645\u6570\u636e\u3002\u65e0\u8bba\u91c7\u7528\u54ea\u79cd\u65b9\u5f0f\uff0c\u539f\u59cb\u4e09\u7ef4\u6570\u7ec4\u7684\u6570\u636e\u90fd\u4f1a\u5b8c\u6574\u4fdd\u7559\u5728\u4e00\u7ef4\u6570\u7ec4\u4e2d\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06\u4e0d\u540c\u5f62\u72b6\u7684\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u65e0\u8bba\u662f\u591a\u5927\u6216\u591a\u5c0f\u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u90fd\u53ef\u4ee5\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002\u53ea\u8981\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u662f\u56fa\u5b9a\u7684\uff0c\u4efb\u610f\u5f62\u72b6\u7684\u4e09\u7ef4\u6570\u7ec4\u90fd\u53ef\u4ee5\u901a\u8fc7<code>flatten<\/code>\u6216<code>reshape<\/code>\u65b9\u6cd5\u987a\u5229\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002\u786e\u4fdd\u5728\u8f6c\u6362\u65f6\uff0c\u5143\u7d20\u7684\u603b\u6570\u4fdd\u6301\u4e00\u81f4\u5373\u53ef\u3002<\/p>\n<p><strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e3a\u4ec0\u4e48\u9700\u8981\u5c06\u4e09\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5c06\u4e09\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u5728\u8bb8\u591a\u6570\u636e\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4efb\u52a1\u4e2d\u975e\u5e38\u6709\u7528\u3002\u8bb8\u591a\u7b97\u6cd5\u548c\u5e93\uff08\u5982TensorFlow\u548cScikit-learn\uff09\u8981\u6c42\u8f93\u5165\u6570\u636e\u4e3a\u4e00\u7ef4\u683c\u5f0f\u3002\u901a\u8fc7\u5c06\u4e09\u7ef4\u6570\u636e\u5c55\u5e73\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5206\u6790\u3001\u7279\u5f81\u63d0\u53d6\u548c\u6a21\u578b\u8bad\u7ec3\u7b49\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06\u4e09\u7ef4\u6570\u7ec4\u53d8\u6210\u4e00\u7ef4\u6570\u7ec4\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1anumpy.flatten()\u3001numpy.ravel()\u3001\u5217\u8868\u89e3\u6790\u548c\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1138004,"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\/1137991"}],"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=1137991"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137991\/revisions"}],"predecessor-version":[{"id":1138008,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137991\/revisions\/1138008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1138004"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1137991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1137991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1137991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}