{"id":1052369,"date":"2024-12-31T14:24:25","date_gmt":"2024-12-31T06:24:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1052369.html"},"modified":"2024-12-31T14:24:27","modified_gmt":"2024-12-31T06:24:27","slug":"python%e4%ba%8c%e7%bb%b4%e5%85%83%e7%bb%84%e5%a6%82%e4%bd%95%e9%99%8d%e7%bb%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1052369.html","title":{"rendered":"python\u4e8c\u7ef4\u5143\u7ec4\u5982\u4f55\u964d\u7ef4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/190c00c2-2b23-470a-b2d9-adaa6ea370e6.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e8c\u7ef4\u5143\u7ec4\u5982\u4f55\u964d\u7ef4\" \/><\/p>\n<p><p> Python\u4e2d\u7684\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5143\u7ec4\u6216\u5217\u8868\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u76f4\u89c2<\/strong>\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\u6765\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u4e00\u4e2a\u65b0\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4\u3002\u7136\u540e\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\u3002\u6700\u540e\uff0c\u5982\u679c\u9700\u8981\uff0c\u5c06\u8be5\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4<\/p>\n<p>two_d_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>one_d_list = [element for sublist in two_d_tuple for element in sublist]<\/p>\n<h2><strong>\u5982\u679c\u9700\u8981\uff0c\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_tuple = tuple(one_d_list)<\/p>\n<p>print(one_d_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u6210\u529f\u5730\u5c06\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u4e3a\u4e00\u7ef4\u5143\u7ec4\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u66f4\u591a\u793a\u4f8b\u548c\u8be6\u7ec6\u7684\u89e3\u91ca\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u5904\u7406\u964d\u7ef4\u95ee\u9898\u3002\u5b83\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4<\/p>\n<p>two_d_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>one_d_list = [element for sublist in two_d_tuple for element in sublist]<\/p>\n<h2><strong>\u5982\u679c\u9700\u8981\uff0c\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_tuple = tuple(one_d_list)<\/p>\n<p>print(one_d_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5d4c\u5957\u7684for\u5faa\u73af\u3002\u7b2c\u4e00\u4e2afor\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b50\u5143\u7ec4\uff08\u5b50\u5217\u8868\uff09\uff0c\u800c\u7b2c\u4e8c\u4e2afor\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b50\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u65b0\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528itertools.chain<\/h3>\n<\/p>\n<p><p><code>itertools.chain<\/code> \u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u7528\u4e8e\u5c06\u591a\u4e2a\u8fed\u4ee3\u5668\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5c06\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u4e3a\u4e00\u7ef4\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>two_d_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<h2><strong>\u4f7f\u7528itertools.chain\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_tuple = tuple(itertools.chain(*two_d_tuple))<\/p>\n<p>print(one_d_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>itertools.chain<\/code> \u63a5\u53d7\u591a\u4e2a\u8fed\u4ee3\u5668\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u901a\u8fc7\u4f7f\u7528\u661f\u53f7\uff08*\uff09\u64cd\u4f5c\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e8c\u7ef4\u5143\u7ec4\u62c6\u5f00\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5b50\u5143\u7ec4\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 <code>itertools.chain<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>numpy<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>numpy<\/code> \u6765\u5c06\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u4e3a\u4e00\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\u4e8c\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>two_d_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<h2><strong>\u4f7f\u7528numpy\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>one_d_array = np.array(two_d_tuple).flatten()<\/p>\n<p>print(one_d_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>numpy.array<\/code> \u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff0c<code>flatten<\/code> \u65b9\u6cd5\u5c06\u5176\u964d\u7ef4\u4e3a\u4e00\u7ef4\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>numpy<\/code> \u8fd4\u56de\u7684\u662f <code>numpy.ndarray<\/code> \u7c7b\u578b\uff0c\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>tolist<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528reduce\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>reduce<\/code> \u662f <code>functools<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u8ba1\u7b97\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5c06\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u4e3a\u4e00\u7ef4\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>two_d_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<h2><strong>\u4f7f\u7528reduce\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_tuple = reduce(lambda x, y: x + y, two_d_tuple)<\/p>\n<p>print(one_d_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>reduce<\/code> \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u4e8c\u5143\u51fd\u6570\u548c\u4e00\u4e2a\u5e8f\u5217\u4f5c\u4e3a\u53c2\u6570\uff0c\u5bf9\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u8ba1\u7b97\u3002\u6211\u4eec\u4f7f\u7528 <code>lambda<\/code> \u51fd\u6570\u5c06\u4e24\u4e2a\u5b50\u5143\u7ec4\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u5b9e\u73b0\u964d\u7ef4\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>sum<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>sum<\/code> \u51fd\u6570\u901a\u5e38\u7528\u4e8e\u8ba1\u7b97\u5e8f\u5217\u7684\u603b\u548c\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8fde\u63a5\u5e8f\u5217\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5c06\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u4e3a\u4e00\u7ef4\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4<\/p>\n<p>two_d_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<h2><strong>\u4f7f\u7528sum\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_tuple = sum(two_d_tuple, ())<\/p>\n<p>print(one_d_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06 <code>sum<\/code> \u51fd\u6570\u7684\u8d77\u59cb\u503c\u8bbe\u7f6e\u4e3a\u7a7a\u5143\u7ec4 <code>()<\/code>\uff0c\u7136\u540e\u5b83\u4f1a\u5c06\u6bcf\u4e2a\u5b50\u5143\u7ec4\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u5b9e\u73b0\u964d\u7ef4\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u5b83\u4e0d\u4f1a\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u5143\u7d20\uff0c\u800c\u662f\u6309\u9700\u751f\u6210\u3002\u8fd9\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4<\/p>\n<p>two_d_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_generator = (element for sublist in two_d_tuple for element in sublist)<\/p>\n<h2><strong>\u5c06\u751f\u6210\u5668\u8f6c\u6362\u4e3a\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_tuple = tuple(one_d_generator)<\/p>\n<p>print(one_d_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u751f\u6210\u4e00\u7ef4\u5143\u7ec4\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4e0d\u4f1a\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u5143\u7d20\uff0c\u800c\u662f\u6309\u9700\u751f\u6210\u3002\u8fd9\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u6269\u5c55\u89e3\u5305<\/h3>\n<\/p>\n<p><p>\u6269\u5c55\u89e3\u5305\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\u6765\u5c06\u591a\u4e2a\u5e8f\u5217\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5c06\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u4e3a\u4e00\u7ef4\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4<\/p>\n<p>two_d_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<h2><strong>\u4f7f\u7528\u6269\u5c55\u89e3\u5305\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_tuple = (*two_d_tuple[0], *two_d_tuple[1], *two_d_tuple[2])<\/p>\n<p>print(one_d_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6269\u5c55\u89e3\u5305\u64cd\u4f5c\u7b26 <code>*<\/code> \u5c06\u6bcf\u4e2a\u5b50\u5143\u7ec4\u89e3\u5305\uff0c\u5e76\u5c06\u5b83\u4eec\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u5b9e\u73b0\u964d\u7ef4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5df2\u77e5\u5b50\u5143\u7ec4\u6570\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u5c06\u4efb\u610f\u6df1\u5ea6\u7684\u5d4c\u5957\u5143\u7ec4\u964d\u7ef4\u4e3a\u4e00\u7ef4\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5143\u7ec4<\/p>\n<p>nested_tuple = ((1, 2), (3, 4), (5, (6, 7)))<\/p>\n<h2><strong>\u5b9a\u4e49\u9012\u5f52\u51fd\u6570\u5c06\u5d4c\u5957\u5143\u7ec4\u964d\u7ef4<\/strong><\/h2>\n<p>def flatten_tuple(nested):<\/p>\n<p>    for element in nested:<\/p>\n<p>        if isinstance(element, (list, tuple)):<\/p>\n<p>            yield from flatten_tuple(element)<\/p>\n<p>        else:<\/p>\n<p>            yield element<\/p>\n<h2><strong>\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u5c06\u5d4c\u5957\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_tuple = tuple(flatten_tuple(nested_tuple))<\/p>\n<p>print(one_d_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 <code>flatten_tuple<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5d4c\u5957\u5143\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u6309\u9700\u751f\u6210\u4e00\u7ef4\u5143\u7ec4\u3002\u901a\u8fc7\u4f7f\u7528 <code>yield from<\/code> \u8bed\u6cd5\uff0c\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u5c06\u5d4c\u5957\u5143\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u89e3\u5305\u5e76\u751f\u6210\u4e00\u7ef4\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p><code>pandas<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u6765\u5c06\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u4e3a\u4e00\u7ef4\u6570\u7ec4\u6216\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>two_d_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<h2><strong>\u4f7f\u7528pandas\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>one_d_series = pd.Series([element for sublist in two_d_tuple for element in sublist])<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u5143\u7ec4<\/strong><\/h2>\n<p>one_d_list = one_d_series.tolist()<\/p>\n<p>one_d_tuple = tuple(one_d_list)<\/p>\n<p>print(one_d_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>pandas.Series<\/code> \u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 <code>tolist<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u6700\u540e\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u8003\u8651\u56e0\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u6765\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u4e8c\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>two_d_tuple = tuple((i, i+1) for i in range(10000))<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0d\u540c\u65b9\u6cd5\u7684\u6d4b\u8bd5\u51fd\u6570<\/strong><\/h2>\n<p>def list_comprehension():<\/p>\n<p>    return tuple([element for sublist in two_d_tuple for element in sublist])<\/p>\n<p>def itertools_chain():<\/p>\n<p>    return tuple(itertools.chain(*two_d_tuple))<\/p>\n<p>def numpy_flatten():<\/p>\n<p>    return np.array(two_d_tuple).flatten()<\/p>\n<p>def reduce_function():<\/p>\n<p>    return reduce(lambda x, y: x + y, two_d_tuple)<\/p>\n<p>def sum_function():<\/p>\n<p>    return sum(two_d_tuple, ())<\/p>\n<p>def generator_expression():<\/p>\n<p>    return tuple(element for sublist in two_d_tuple for element in sublist)<\/p>\n<h2><strong>\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd<\/strong><\/h2>\n<p>print(&quot;List Comprehension:&quot;, timeit.timeit(list_comprehension, number=10))<\/p>\n<p>print(&quot;itertools.chain:&quot;, timeit.timeit(itertools_chain, number=10))<\/p>\n<p>print(&quot;numpy.flatten:&quot;, timeit.timeit(numpy_flatten, number=10))<\/p>\n<p>print(&quot;Reduce Function:&quot;, timeit.timeit(reduce_function, number=10))<\/p>\n<p>print(&quot;Sum Function:&quot;, timeit.timeit(sum_function, number=10))<\/p>\n<p>print(&quot;Generator Expression:&quot;, timeit.timeit(generator_expression, number=10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5927\u4e8c\u7ef4\u5143\u7ec4\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e0d\u540c\u65b9\u6cd5\u7684\u6d4b\u8bd5\u51fd\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u6765\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u6211\u4eec\u9700\u6c42\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u591a\u79cd\u5c06Python\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u4e3a\u4e00\u7ef4\u5143\u7ec4\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5217\u8868\u63a8\u5bfc\u5f0f\u3001<code>itertools.chain<\/code>\u3001<code>numpy<\/code>\u3001<code>reduce<\/code>\u3001<code>sum<\/code>\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3001\u6269\u5c55\u89e3\u5305\u3001\u9012\u5f52\u51fd\u6570\u548c <code>pandas<\/code>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u6765\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u4ece\u800c\u9009\u62e9\u6700\u4f18\u65b9\u6cd5\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u80fd\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5143\u7ec4\uff1f<\/strong><br \/>\u8981\u5c06\u4e8c\u7ef4\u5143\u7ec4\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5143\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5143\u7ec4\u6784\u9020\u51fd\u6570\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u904d\u5386\u4e8c\u7ef4\u5143\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u9010\u4e2a\u6dfb\u52a0\u5230\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\u4e2d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">original_tuple = ((1, 2), (3, 4), (5, 6))\nflattened_tuple = tuple(item for subtuple in original_tuple for item in subtuple)\nprint(flattened_tuple)  # \u8f93\u51fa: (1, 2, 3, 4, 5, 6)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u6709\u6548\uff0c\u80fd\u591f\u5feb\u901f\u5b9e\u73b0\u964d\u7ef4\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u964d\u7ef4\u4e8c\u7ef4\u5143\u7ec4\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5916\uff0c\u964d\u7ef4\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools.chain<\/code>\u6765\u5b9e\u73b0\u5143\u7ec4\u7684\u964d\u7ef4\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u5c06\u4e8c\u7ef4\u5143\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u4e32\u8054\u8d77\u6765\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import itertools\n\noriginal_tuple = ((1, 2), (3, 4), (5, 6))\nflattened_tuple = tuple(itertools.chain.from_iterable(original_tuple))\nprint(flattened_tuple)  # \u8f93\u51fa: (1, 2, 3, 4, 5, 6)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u53ef\u80fd\u66f4\u9ad8\u6548\u3002<\/p>\n<p><strong>\u964d\u7ef4\u540e\u7684\u5143\u7ec4\u5728\u4f7f\u7528\u4e0a\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u964d\u7ef4\u540e\u7684\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539\u5176\u4e2d\u7684\u5143\u7d20\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u64cd\u4f5c\uff0c\u5e94\u8be5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\u6216\u4f7f\u7528\u5217\u8868\u8fdb\u884c\u4fee\u6539\u3002\u6b64\u5916\uff0c\u5728\u9009\u62e9\u964d\u7ef4\u65b9\u6cd5\u65f6\uff0c\u8981\u8003\u8651\u6570\u636e\u7684\u7ed3\u6784\u548c\u540e\u7eed\u4f7f\u7528\u573a\u666f\uff0c\u4ee5\u786e\u4fdd\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684\u4e8c\u7ef4\u5143\u7ec4\u964d\u7ef4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570itertools.chAIn\u7b49\u3002\u8fd9\u4e9b [&hellip;]","protected":false},"author":3,"featured_media":1052373,"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\/1052369"}],"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=1052369"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052369\/revisions"}],"predecessor-version":[{"id":1052375,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052369\/revisions\/1052375"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1052373"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1052369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1052369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1052369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}