{"id":1143586,"date":"2025-01-08T22:51:44","date_gmt":"2025-01-08T14:51:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1143586.html"},"modified":"2025-01-08T22:51:47","modified_gmt":"2025-01-08T14:51:47","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e4%ba%8c%e7%bb%b4%e5%88%97%e8%a1%a8%e5%8f%98%e6%88%90%e4%b8%80%e7%bb%b4%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143586.html","title":{"rendered":"python\u5982\u4f55\u5c06\u4e8c\u7ef4\u5217\u8868\u53d8\u6210\u4e00\u7ef4\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181124\/83d7286e-2f73-445c-ad0e-9bc338745e56.webp\" alt=\"python\u5982\u4f55\u5c06\u4e8c\u7ef4\u5217\u8868\u53d8\u6210\u4e00\u7ef4\u7684\" \/><\/p>\n<p><p> <strong>Python \u5982\u4f55\u5c06\u4e8c\u7ef4\u5217\u8868\u53d8\u6210\u4e00\u7ef4\u7684\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528 itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u3001\u4f7f\u7528 numpy.flatten<\/strong><\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06\u4e8c\u7ef4\u5217\u8868\uff08\u5217\u8868\u7684\u5217\u8868\uff09\u53d8\u4e3a\u4e00\u7ef4\u5217\u8868\u3002<strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u76f4\u89c2\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u3002\u9664\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c<strong>\u4f7f\u7528 itertools.chain \u548c numpy.flatten<\/strong> \u4e5f\u662f\u5e38\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e09\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u53ca\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><h3>\u5217\u8868\u63a8\u5bfc\u5f0f\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u5b83\u901a\u8fc7\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u5d4c\u5957\u591a\u4e2a for \u5faa\u73af\uff0c\u4ece\u800c\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u4e8c\u7ef4\u5217\u8868\u53d8\u4e3a\u4e00\u7ef4\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868 <code>matrix<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8981\u5c06\u8fd9\u4e2a\u4e8c\u7ef4\u5217\u8868\u53d8\u4e3a\u4e00\u7ef4\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">flattened = [item for sublist in matrix for item in sublist]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b80\u6d01<\/strong>\uff1a\u4ee3\u7801\u91cf\u5c11\uff0c\u5bb9\u6613\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/li>\n<li><strong>\u6027\u80fd\u8f83\u597d<\/strong>\uff1a\u76f8\u5bf9\u4e8e\u67d0\u4e9b\u65b9\u6cd5\uff0c\u6267\u884c\u901f\u5ea6\u5feb\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u5d4c\u5957\u5c42\u6b21\u8f83\u6df1\u7684\u5217\u8868\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u4e0d\u9002\u5408\u5904\u7406\u7279\u522b\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<p><h2>\u4e8c\u3001itertools.chain<\/h2>\n<\/p>\n<p><h3>itertools.chain \u7b80\u4ecb<\/h3>\n<\/p>\n<p><p><code>itertools.chain<\/code> \u662f Python \u6807\u51c6\u5e93 <code>itertools<\/code> \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fde\u63a5\u8d77\u6765\uff0c\u751f\u6210\u4e00\u4e2a\u5355\u4e00\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528 itertools.chain \u5c06\u4e8c\u7ef4\u5217\u8868\u53d8\u4e3a\u4e00\u7ef4\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u5bf9\u4e8e\u4e8c\u7ef4\u5217\u8868 <code>matrix<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>itertools.chain<\/code> \u5c06\u5176\u53d8\u4e3a\u4e00\u7ef4\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>flattened = list(itertools.chain(*matrix))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>itertools.chain \u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u9002\u7528\u4e8e\u591a\u79cd\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u5185\u5b58\u6548\u7387<\/strong>\uff1a\u751f\u6210\u8fed\u4ee3\u5668\uff0c\u4e0d\u4f1a\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u5230\u5185\u5b58\u4e2d\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5e93<\/strong>\uff1a\u9700\u8981\u5bfc\u5165\u989d\u5916\u7684\u6a21\u5757\uff0c\u589e\u52a0\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u590d\u6742\u6027<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u9700\u6c42\uff0c\u53ef\u80fd\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/li>\n<\/ol>\n<p><h2>\u4e09\u3001numpy.flatten<\/h2>\n<\/p>\n<p><h3>numpy.flatten \u7b80\u4ecb<\/h3>\n<\/p>\n<p><p><code>numpy<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f88\u591a\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u5176\u4e2d <code>flatten<\/code> \u5c31\u662f\u7528\u4e8e\u5c06\u591a\u7ef4\u6570\u7ec4\u53d8\u4e3a\u4e00\u7ef4\u6570\u7ec4\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528 numpy.flatten \u5c06\u4e8c\u7ef4\u5217\u8868\u53d8\u4e3a\u4e00\u7ef4\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5 <code>numpy<\/code> \u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u4e8c\u7ef4\u5217\u8868\u53d8\u4e3a\u4e00\u7ef4\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>flattened = np.array(matrix).flatten().tolist()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>numpy.flatten \u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u6027\u80fd\u4f18\u8d8a\u3002<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1a<code>numpy<\/code> \u63d0\u4f9b\u4e86\u5f88\u591a\u5176\u4ed6\u6709\u7528\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165\u989d\u5916\u7684\u6a21\u5757\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u719f\u6089 <code>numpy<\/code> \u7684\u7528\u6237\uff0c\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u5b66\u4e60\u6210\u672c\u3002<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>\u9012\u5f52\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5d4c\u5957\u5217\u8868\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u5c06\u5176\u53d8\u4e3a\u4e00\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def flatten_list(nested_list):<\/p>\n<p>    flattened = []<\/p>\n<p>    for item in nested_list:<\/p>\n<p>        if isinstance(item, list):<\/p>\n<p>            flattened.extend(flatten_list(item))<\/p>\n<p>        else:<\/p>\n<p>            flattened.append(item)<\/p>\n<p>    return flattened<\/p>\n<p>matrix = [[1, 2, [3, 4]], [5, 6], [7, 8, 9]]<\/p>\n<p>flattened = flatten_list(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u901a\u7528\u6027<\/strong>\uff1a\u9002\u7528\u4e8e\u4efb\u610f\u6df1\u5ea6\u7684\u5d4c\u5957\u5217\u8868\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u590d\u6742\u6027<\/strong>\uff1a\u5b9e\u73b0\u8f83\u4e3a\u590d\u6742\uff0c\u4e14\u53ef\u80fd\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n<\/ol>\n<p><h3>\u4f7f\u7528 functools.reduce<\/h3>\n<\/p>\n<p><p><code>functools.reduce<\/code> \u662f Python \u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>flattened = reduce(lambda x, y: x + y, matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b80\u6d01<\/strong>\uff1a\u4ee3\u7801\u91cf\u5c11\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982\u5176\u4ed6\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u719f\u6089 <code>reduce<\/code> \u7684\u7528\u6237\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002<\/li>\n<\/ol>\n<p><h2>\u4e94\u3001\u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><h3>\u6027\u80fd\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>import itertools<\/p>\n<p>import numpy as np<\/p>\n<p>from functools import reduce<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] * 1000<\/p>\n<p>def list_comprehension(matrix):<\/p>\n<p>    return [item for sublist in matrix for item in sublist]<\/p>\n<p>def itertools_chain(matrix):<\/p>\n<p>    return list(itertools.chain(*matrix))<\/p>\n<p>def numpy_flatten(matrix):<\/p>\n<p>    return np.array(matrix).flatten().tolist()<\/p>\n<p>def reduce_method(matrix):<\/p>\n<p>    return reduce(lambda x, y: x + y, matrix)<\/p>\n<p>def test_functions():<\/p>\n<p>    list_comprehension(matrix)<\/p>\n<p>    itertools_chain(matrix)<\/p>\n<p>    numpy_flatten(matrix)<\/p>\n<p>    reduce_method(matrix)<\/p>\n<p>print(timeit.timeit(&quot;list_comprehension(matrix)&quot;, globals=globals(), number=1000))<\/p>\n<p>print(timeit.timeit(&quot;itertools_chain(matrix)&quot;, globals=globals(), number=1000))<\/p>\n<p>print(timeit.timeit(&quot;numpy_flatten(matrix)&quot;, globals=globals(), number=1000))<\/p>\n<p>print(timeit.timeit(&quot;reduce_method(matrix)&quot;, globals=globals(), number=1000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u679c\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u6d4b\u8bd5\u7ed3\u679c\uff0c\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong> \u5728\u5c0f\u89c4\u6a21\u6570\u636e\u4e0b\u6027\u80fd\u8f83\u597d\uff0c\u4f46\u968f\u7740\u6570\u636e\u89c4\u6a21\u7684\u589e\u52a0\uff0c\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n<li><strong>itertools.chain<\/strong> \u5728\u5927\u89c4\u6a21\u6570\u636e\u4e0b\u6027\u80fd\u4f18\u8d8a\uff0c\u540c\u65f6\u5185\u5b58\u6548\u7387\u9ad8\u3002<\/li>\n<li><strong>numpy.flatten<\/strong> \u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\uff0c\u6027\u80fd\u6700\u4f73\uff0c\u4f46\u9700\u8981\u989d\u5916\u7684\u5e93\u652f\u6301\u3002<\/li>\n<li><strong>functools.reduce<\/strong> \u6027\u80fd\u76f8\u5bf9\u8f83\u5dee\uff0c\u4e14\u4ee3\u7801\u53ef\u8bfb\u6027\u4e0d\u9ad8\u3002<\/li>\n<\/ol>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5c06\u4e8c\u7ef4\u5217\u8868\u53d8\u4e3a\u4e00\u7ef4\u5217\u8868\u5728 Python \u4e2d\u6709\u591a\u79cd\u5b9e\u73b0\u65b9\u6cd5\uff0c<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u3001itertools.chain \u548c numpy.flatten \u662f\u6700\u5e38\u7528\u7684\u4e09\u79cd\u65b9\u6cd5<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u89c4\u6a21\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong> \u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u548c\u7b80\u5355\u5d4c\u5957\u7ed3\u6784\uff0c\u4ee3\u7801\u7b80\u6d01\u4f46\u53ef\u8bfb\u6027\u7a0d\u5dee\u3002<\/li>\n<li><strong>itertools.chain<\/strong> \u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u5185\u5b58\u6548\u7387\u9ad8\uff0c\u4f46\u9700\u8981\u989d\u5916\u7684\u5e93\u652f\u6301\u3002<\/li>\n<li><strong>numpy.flatten<\/strong> \u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\uff0c\u6027\u80fd\u6700\u4f73\uff0c\u4f46\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165 <code>numpy<\/code> \u5e93\u3002<\/li>\n<\/ul>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u5d4c\u5957\u7ed3\u6784\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\uff0c\u4f46\u9700\u6ce8\u610f\u6027\u80fd\u548c\u5b9e\u73b0\u590d\u6742\u5ea6\u3002<code>functools.reduce<\/code> \u9002\u7528\u4e8e\u4e00\u4e9b\u7279\u5b9a\u573a\u666f\uff0c\u4f46\u4e00\u822c\u4e0d\u63a8\u8350\u4f5c\u4e3a\u9996\u9009\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u548c\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e8c\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5185\u7f6e\u7684<code>itertools.chain<\/code>\u6a21\u5757\u6765\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u9010\u4e00\u6dfb\u52a0\u5230\u4e00\u7ef4\u5217\u8868\u4e2d\u3002\u800c\u4f7f\u7528<code>itertools.chain<\/code>\u5219\u53ef\u4ee5\u76f4\u63a5\u5c06\u591a\u4e2a\u5217\u8868\u8fde\u63a5\u8d77\u6765\uff0c\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u65b9\u6cd5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\"># \u65b9\u6cd51\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\ntwo_dimensional_list = [[1, 2, 3], [4, 5], [6]]\none_dimensional_list = [item for sublist in two_dimensional_list for item in sublist]\n\n# \u65b9\u6cd52\uff1a\u4f7f\u7528 itertools.chain\nimport itertools\ntwo_dimensional_list = [[1, 2, 3], [4, 5], [6]]\none_dimensional_list = list(itertools.chain.from_iterable(two_dimensional_list))\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u8f6c\u6362\u5217\u8868\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u4e8c\u7ef4\u5217\u8868\u5230\u4e00\u7ef4\u5217\u8868\u7684\u8f6c\u6362\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u3001\u5bf9\u975e\u5217\u8868\u5143\u7d20\u7684\u64cd\u4f5c\u4ee5\u53ca\u6570\u636e\u7c7b\u578b\u4e0d\u4e00\u81f4\u3002\u786e\u4fdd\u5904\u7406\u7684\u6bcf\u4e2a\u5b50\u5217\u8868\u90fd\u662f\u5217\u8868\u7c7b\u578b\uff0c\u907f\u514d\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7c7b\u578b\u9519\u8bef\u3002\u4f7f\u7528\u5408\u9002\u7684\u65b9\u6cd5\u5982<code>itertools.chain<\/code>\u53ef\u4ee5\u51cf\u5c11\u8fd9\u4e9b\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u4efb\u4f55\u5e93\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e2a\u8f6c\u6362\u8fc7\u7a0b\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>itertools<\/code>\u6a21\u5757\u5916\uff0cNumPy\u5e93\u4e5f\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6570\u7ec4\u7684\u7ef4\u5ea6\u8f6c\u6362\u3002\u901a\u8fc7\u4f7f\u7528<code>numpy.flatten()<\/code>\u6216<code>numpy.ravel()<\/code>\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002\u8fd9\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3aNumPy\u7684\u6027\u80fd\u4f18\u4e8e\u6807\u51c6Python\u5217\u8868\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-python\">import numpy as np\ntwo_dimensional_array = np.array([[1, 2, 3], [4, 5], [6]])\none_dimensional_array = two_dimensional_array.flatten()\n<\/code><\/pre>\n<p><strong>\u8f6c\u6362\u540e\u5982\u4f55\u5904\u7406\u4e00\u7ef4\u5217\u8868\u4e2d\u7684\u6570\u636e\uff1f<\/strong><br \/>\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u591a\u79cd\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u5feb\u901f\u8ba1\u7b97\u603b\u548c\uff0c\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u7b5b\u9009\u7279\u5b9a\u5143\u7d20\uff0c\u6216\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u5bf9\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u4e00\u7ef4\u5217\u8868\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u6781\u4e3a\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5982\u4f55\u5c06\u4e8c\u7ef4\u5217\u8868\u53d8\u6210\u4e00\u7ef4\u7684\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528 itertools.chAIn\u3001\u4f7f\u7528 numpy [&hellip;]","protected":false},"author":3,"featured_media":1143597,"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\/1143586"}],"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=1143586"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143586\/revisions"}],"predecessor-version":[{"id":1143598,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143586\/revisions\/1143598"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1143597"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}