{"id":1078353,"date":"2025-01-08T12:10:48","date_gmt":"2025-01-08T04:10:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078353.html"},"modified":"2025-01-08T12:10:50","modified_gmt":"2025-01-08T04:10:50","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%9e%e6%8e%a5%e4%b8%a4%e4%b8%aa%e5%88%97%e8%a1%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078353.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8fde\u63a5\u4e24\u4e2a\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181943\/1e522138-c139-4b40-95ea-f49a657a8985.webp\" alt=\"python\u4e2d\u5982\u4f55\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528extend()\u65b9\u6cd5\u3001\u4f7f\u7528\u661f\u53f7\uff08*\uff09\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n()\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u5355\u548c\u76f4\u89c2\u7684\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5c06\u4e24\u4e2a\u539f\u59cb\u5217\u8868\u7684\u5143\u7d20\u4f9d\u6b21\u653e\u5165\u5176\u4e2d\u3002\u8be6\u7ec6\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u8fde\u63a5\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u7684\u5217\u8868\uff0c\u800c\u662f\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002\u56e0\u6b64\uff0c\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u6301\u539f\u59cb\u5217\u8868\u4e0d\u53d8\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = list1 + list2<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>list2 = [&#39;d&#39;, &#39;e&#39;, &#39;f&#39;]<\/p>\n<p>combined_list = list1 + list2<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u8bed\u6cd5\u7b80\u5355\uff0c\u5bb9\u6613\u7406\u89e3\u548c\u4f7f\u7528\u3002\u7f3a\u70b9\u662f\u6bcf\u6b21\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u8fde\u63a5\u5217\u8868\u65f6\uff0c\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u5bf9\u8c61\uff0c\u56e0\u6b64\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u5217\u8868\u65f6\uff0c\u53ef\u80fd\u4f1a\u5360\u7528\u8f83\u591a\u7684\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528extend()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>extend()\u65b9\u6cd5\u7528\u4e8e\u5c06\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u672b\u5c3e\u3002\u4e0e\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u4e0d\u540c\u7684\u662f\uff0cextend()\u65b9\u6cd5\u662f\u5c31\u5730\u64cd\u4f5c\uff0c\u5373\u5b83\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u59cb\u5217\u8868\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>list1.extend(list2)<\/p>\n<p>print(list1)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528extend()\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u5b83\u4e0d\u4f1a\u521b\u5efa\u65b0\u7684\u5217\u8868\u5bf9\u8c61\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u5217\u8868\u65f6\uff0c\u5b83\u7684\u5185\u5b58\u5360\u7528\u76f8\u5bf9\u8f83\u5c0f\u3002\u7f3a\u70b9\u662f\u5b83\u4f1a\u4fee\u6539\u539f\u59cb\u5217\u8868\uff0c\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u59cb\u5217\u8868\u4e0d\u53d8\uff0c\u5219\u9700\u8981\u5148\u521b\u5efa\u526f\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u661f\u53f7\uff08*\uff09\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u5728Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u661f\u53f7\uff08*\uff09\u8fd0\u7b97\u7b26\u89e3\u5305\u5217\u8868\u5e76\u5c06\u5b83\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u8bed\u6cd5\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = [*list1, *list2]<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u8bed\u6cd5\u7b80\u6d01\uff0c\u5e76\u4e14\u53ef\u4ee5\u7528\u4e8e\u89e3\u5305\u4efb\u610f\u6570\u91cf\u7684\u5217\u8868\u3002\u7f3a\u70b9\u662f\u5b83\u9700\u8981Python 3.5\u6216\u66f4\u9ad8\u7248\u672c\u652f\u6301\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528itertools.chain()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>itertools\u6a21\u5757\u7684chain()\u65b9\u6cd5\u7528\u4e8e\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fde\u63a5\u6210\u4e00\u4e2a\u8fde\u7eed\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u5b83\u4e0d\u4f1a\u521b\u5efa\u65b0\u7684\u5217\u8868\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = list(itertools.chain(list1, list2))<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528itertools.chain()\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u5b83\u975e\u5e38\u9ad8\u6548\uff0c\u5c24\u5176\u9002\u5408\u5904\u7406\u975e\u5e38\u5927\u7684\u5217\u8868\uff0c\u56e0\u4e3a\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u8282\u7701\u4e86\u5185\u5b58\u3002\u7f3a\u70b9\u662f\u9700\u8981\u5bfc\u5165itertools\u6a21\u5757\uff0c\u5e76\u4e14\u9700\u8981\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5982\u679c\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u5217\u8868\u521b\u5efa\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u8fde\u63a5\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u5217\u8868\u7684\u8fde\u63a5\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = [item for sublist in [list1, list2] for item in sublist]<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u8bed\u6cd5\u7075\u6d3b\uff0c\u9002\u5408\u590d\u6742\u7684\u5217\u8868\u64cd\u4f5c\u3002\u7f3a\u70b9\u662f\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u4f4e\uff0c\u4e0d\u5982\u524d\u9762\u7684\u51e0\u79cd\u65b9\u6cd5\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u5217\u8868\u4e2d\u5305\u542b\u7684\u662f\u6570\u503c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4e14\u4f60\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u503c\u8fd0\u7b97\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528numpy\u5e93\u3002numpy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u8fde\u63a5\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = np.concatenate((list1, list2)).tolist()<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528numpy\u5e93\u7684\u4f18\u70b9\u662f\u5b83\u5728\u5904\u7406\u6570\u503c\u6570\u636e\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u7f3a\u70b9\u662f\u9700\u8981\u5b89\u88c5numpy\u5e93\uff0c\u5e76\u4e14\u4ec5\u9002\u7528\u4e8e\u6570\u503c\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u524d\u9762\u7684\u51e0\u79cd\u65b9\u6cd5\u90fd\u5f88\u7b80\u6d01\uff0c\u4f46\u6709\u65f6\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5e95\u5c42\u903b\u8f91\uff0c\u4f7f\u7528for\u5faa\u73af\u6765\u624b\u52a8\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = []<\/p>\n<p>for item in list1:<\/p>\n<p>    combined_list.append(item)<\/p>\n<p>for item in list2:<\/p>\n<p>    combined_list.append(item)<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u7684\u4f18\u70b9\u662f\u5b83\u7684\u64cd\u4f5c\u6b65\u9aa4\u6e05\u6670\uff0c\u4fbf\u4e8e\u7406\u89e3\u548c\u8c03\u8bd5\u3002\u7f3a\u70b9\u662f\u4ee3\u7801\u8f83\u4e3a\u5197\u957f\uff0c\u4e0d\u5982\u524d\u9762\u7684\u51e0\u79cd\u65b9\u6cd5\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528sum()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u5e38\u89c1\uff0c\u4f46sum()\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8fde\u63a5\u591a\u4e2a\u5217\u8868\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u6548\u7387\u8f83\u4f4e\uff0c\u4e0d\u63a8\u8350\u5728\u6027\u80fd\u654f\u611f\u7684\u573a\u666f\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = sum([list1, list2], [])<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u4f46\u7f3a\u70b9\u662f\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u4e3asum()\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528yield\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4e0d\u9700\u8981\u4e00\u6b21\u6027\u751f\u6210\u6574\u4e2a\u8fde\u63a5\u540e\u7684\u5217\u8868\uff0c\u800c\u662f\u5e0c\u671b\u6309\u9700\u751f\u6210\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528yield\u751f\u6210\u5668\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u8d85\u5927\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_lists(list1, list2):<\/p>\n<p>    for item in list1:<\/p>\n<p>        yield item<\/p>\n<p>    for item in list2:<\/p>\n<p>        yield item<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = list(merge_lists(list1, list2))<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528yield\u751f\u6210\u5668\u7684\u4f18\u70b9\u662f\u8282\u7701\u5185\u5b58\uff0c\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u3002\u7f3a\u70b9\u662f\u4ee3\u7801\u8f83\u4e3a\u590d\u6742\uff0c\u4e0d\u5982\u524d\u9762\u7684\u51e0\u79cd\u65b9\u6cd5\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26<\/strong>\uff1a\u8bed\u6cd5\u7b80\u5355\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\uff0c\u4f46\u4f1a\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002<\/li>\n<li><strong>extend()\u65b9\u6cd5<\/strong>\uff1a\u5c31\u5730\u64cd\u4f5c\uff0c\u8282\u7701\u5185\u5b58\uff0c\u4f46\u4f1a\u4fee\u6539\u539f\u59cb\u5217\u8868\u3002<\/li>\n<li><strong>\u661f\u53f7\uff08*\uff09\u8fd0\u7b97\u7b26<\/strong>\uff1a\u8bed\u6cd5\u7b80\u6d01\uff0c\u4f46\u9700\u8981Python 3.5\u6216\u66f4\u9ad8\u7248\u672c\u3002<\/li>\n<li><strong>itertools.chain()\u65b9\u6cd5<\/strong>\uff1a\u9ad8\u6548\u8282\u7701\u5185\u5b58\uff0c\u4f46\u9700\u8981\u5bfc\u5165\u6a21\u5757\u3002<\/li>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u7075\u6d3b\u9002\u5408\u590d\u6742\u64cd\u4f5c\uff0c\u4f46\u53ef\u8bfb\u6027\u8f83\u4f4e\u3002<\/li>\n<li><strong>numpy\u5e93<\/strong>\uff1a\u9002\u5408\u6570\u503c\u6570\u636e\u5904\u7406\uff0c\u4f46\u9700\u8981\u5b89\u88c5\u5e93\u3002<\/li>\n<li><strong>for\u5faa\u73af<\/strong>\uff1a\u64cd\u4f5c\u6b65\u9aa4\u6e05\u6670\uff0c\u9002\u5408\u7406\u89e3\u5e95\u5c42\u903b\u8f91\uff0c\u4f46\u4ee3\u7801\u5197\u957f\u3002<\/li>\n<li><strong>sum()\u51fd\u6570<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u4f46\u6548\u7387\u8f83\u4f4e\u3002<\/li>\n<li><strong>yield\u751f\u6210\u5668<\/strong>\uff1a\u8282\u7701\u5185\u5b58\uff0c\u9002\u5408\u5927\u6570\u636e\uff0c\u4f46\u4ee3\u7801\u8f83\u590d\u6742\u3002<\/li>\n<\/ul>\n<p><p>\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8fde\u63a5\u5217\u8868\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5408\u5e76\u4e24\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5408\u5e76\u4e24\u4e2a\u5217\u8868\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u52a0\u6cd5\u8fd0\u7b97\u7b26\uff08<code>+<\/code>\uff09\uff0c\u4f8b\u5982\uff1a<code>list1 + list2<\/code>\u3002\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>extend()<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u5217\u8868\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6216\u8005\u4f7f\u7528<code>itertools.chain()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5408\u5e76\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\u65f6\u662f\u5426\u4f1a\u5f71\u54cd\u539f\u59cb\u5217\u8868\uff1f<\/strong><br \/>\u5f53\u4f7f\u7528<code>+<\/code>\u8fd0\u7b97\u7b26\u5408\u5e76\u4e24\u4e2a\u5217\u8868\u65f6\uff0c\u539f\u59cb\u5217\u8868\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u5982\u679c\u4f7f\u7528<code>extend()<\/code>\u65b9\u6cd5\uff0c\u5219\u4f1a\u76f4\u63a5\u4fee\u6539\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u5217\u8868\uff0c\u5e76\u5c06\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5176\u4e2d\u3002\u56e0\u6b64\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u662f\u5426\u5e0c\u671b\u4fdd\u7559\u539f\u59cb\u5217\u8868\u4e0d\u53d8\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u5728\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\u65f6\u5e0c\u671b\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08<code>set<\/code>\uff09\u6765\u5b9e\u73b0\u3002\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\u518d\u8fdb\u884c\u5408\u5e76\uff0c\u6700\u540e\u518d\u8f6c\u6362\u56de\u5217\u8868\uff0c\u4f8b\u5982\uff1a<code>list(set(list1) | set(list2))<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\uff0c\u4f46\u8bf7\u6ce8\u610f\uff0c\u96c6\u5408\u4e0d\u4fdd\u6301\u5143\u7d20\u7684\u987a\u5e8f\u3002\u5982\u679c\u987a\u5e8f\u5f88\u91cd\u8981\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>collections.OrderedDict<\/code>\u6765\u4fdd\u6301\u987a\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528extend()\u65b9\u6cd5\u3001\u4f7f\u7528\u661f\u53f7\uff08*\uff09\u8fd0\u7b97\u7b26\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1078361,"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\/1078353"}],"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=1078353"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078353\/revisions"}],"predecessor-version":[{"id":1078363,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078353\/revisions\/1078363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1078361"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1078353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1078353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1078353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}