{"id":213655,"date":"2021-03-24T10:57:06","date_gmt":"2021-03-24T02:57:06","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=213655"},"modified":"2021-03-16T08:57:59","modified_gmt":"2021-03-16T00:57:59","slug":"merge-python-list","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/merge-python-list.html","title":{"rendered":"\u8fd912\u79cd\u65b9\u6cd5\u8f7b\u677e\u5408\u5e76Python\u4e2d\u7684\u5217\u8868"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6211\u559c\u6b2212\u8fd9\u4e2a\u6570\u5b57\uff0c\u56e0\u4e3a\u6709\u592a\u591a\u7684\u4e8b\u60c5\u4e0e12\u6709\u5173\u3002\u4e00\u5e74\u670912\u4e2a\u6708;\u53e4\u4ee3\u752812\u4e2a\u65f6\u8fb0(\u5730\u652f)\u8868\u793a\u4e00\u5929\u7684\u65f6\u95f4\u3002\u7528\u4e00\u7eaa\u8868\u793a12\u5e74;\u5e0c\u814a\u670912\u4e3b\u795e;\u9ec4\u9053\u670912\u5bab;\u6492\u8fe6\u5229\u4e9a.\u897f\u7434\u5148\u751f\u7684\u300a\u5730\u7403\u7f16\u5e74\u53f2\u300b\u4e2d\u63cf\u8ff0\u7684\u7b2c12\u4e2a\u5929\u4f53(\u5c3c\u6bd4\u9c81);\u53e6\u5916\uff0c\u670b\u53cb\u3001\u7231\u4eba\u3001\u604b\u4eba\u3001\u5bb6\u4eba\u7684\u7b14\u753b\u90fd\u662f12\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u6211\u559c\u6b2212\u8fd9\u4e2a\u6570\u5b57\uff0c\u56e0\u4e3a\u6709\u592a\u591a\u7684\u4e8b\u60c5\u4e0e12\u6709\u5173\u3002\u4e00\u5e74\u670912\u4e2a\u6708;\u53e4\u4ee3\u752812\u4e2a\u65f6\u8fb0(\u5730\u652f)\u8868\u793a\u4e00\u5929\u7684\u65f6\u95f4\u3002\u7528\u4e00\u7eaa\u8868\u793a12\u5e74;\u5e0c\u814a\u670912\u4e3b\u795e;\u9ec4\u9053\u670912\u5bab;\u6492\u8fe6\u5229\u4e9a.\u897f\u7434\u5148\u751f\u7684\u300a\u5730\u7403\u7f16\u5e74\u53f2\u300b\u4e2d\u63cf\u8ff0\u7684\u7b2c12\u4e2a\u5929\u4f53(\u5c3c\u6bd4\u9c81);\u53e6\u5916\uff0c\u670b\u53cb\u3001\u7231\u4eba\u3001\u604b\u4eba\u3001\u5bb6\u4eba\u7684\u7b14\u753b\u90fd\u662f12\u3002\u6240\u4ee512\u6ce8\u5b9a\u662f\u4e00\u4e2a\u4ee4\u4eba\u96be\u5fd8\u7684\u6570\u5b57\uff0c\u73b0\u5728\u6211\u518d\u4e3a12\u52a0\u4e00\u79cd\u96be\u5fd8\u7684\u89e3\u91ca\uff1a\u752812\u79cd\u65b9\u6cd5\u5408\u5e76Python\u5217\u8868\u3002<\/p>\n

\u5176\u5b9ePython\u8bed\u8a00\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u76f4\u63a5\u4f7f\u7528\u52a0\u53f7(+)\u5373\u53ef\u3002\u4e0d\u8fc7\u5728\u5f88\u591a\u573a\u666f\u4e0b\uff0c\u5e76\u4e0d\u592a\u9002\u5408\u4f7f\u7528\"+\"\u3002\u5217\u8868\u4e2d\u7684\u503c\u7531\u4e8e\u67d0\u4e9b\u539f\u56e0\u662f\u5206\u6563\u5f00\u7684\uff0c\u6216\u9700\u8981\u53bb\u91cd\uff0c\u6216\u8005\u6b63\u5904\u4e8e\u8fed\u4ee3\u4e2d\uff0c\u6240\u4ee5\u672c\u6587\u5c06\u4e3a\u8bfb\u8005\u5c55\u793a\u5982\u679c\u7528\u591a\u8fbe12\u79cd\u65b9\u6cd5\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u3002<\/p>\n

\u65b9\u6cd51\uff1a\u5b87\u5b99\u7b2c\u4e00\u52a0\u53f7\u5927\u6cd5\u8fd9\u662f\u6700\u7b80\u5355\u7684\u5408\u5e76Python\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/span><\/div>\n
list1 = [1, 2, 3] \r\nlist2 = [4, 5, 6] \r\n \r\nresult = list1 + list2 \r\n# [1, 2, 3, 4, 5, 6] \r\nprint(result)<\/pre>\n

\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u7528\u591a\u89e3\u91ca\uff0c\u76f4\u63a5\u52a0\u5c31\u5b8c\u4e86\uff0c\u65e2\u7136\u52a0\u4e24\u4e2a\u5217\u8868\u53ef\u4ee5\uff0c\u52a010000\u4e2a\u5217\u8868\u5f53\u7136\u4e5f\u53ef\u4ee5\uff0c\u6216\u8005\u653e\u5230\u5faa\u73af\u91cc\u4e0d\u65ad\u7d2f\u52a0\u3002<\/p>\n

\u65b9\u6cd52\uff1a\u81ea\u52a8\u62c6\u7bb1\u548c\u88c5\u7bb1\u7684\u661f\u53f7\u5927\u6cd5<\/strong><\/span><\/div>\n

\u4ecePython 3.5\u5f00\u59cb\uff0c\u661f\u53f7(*)\u5c31\u6709\u4e86\u7279\u6b8a\u7684\u7528\u9014\uff0c\u5c06\u4e00\u4e2a\u5217\u8868\u62c6\u5f00\uff0c\u6216\u5c06\u591a\u4e2a\u503c\u7ec4\u88c5\u6210\u5143\u7ec4\u3002\u5982\u679c\u5c06\u661f\u53f7\u7528\u4f5c\u5217\u8868\u5143\u7d20\uff0c\u5e76\u4e14\u8fd9\u4e2a\u5217\u8868\u5143\u7d20\u4e5f\u662f\u4e00\u4e2a\u5217\u8868\u7684\u8bdd\uff0c\u90a3\u4e48\u5c31\u4f1a\u76f4\u63a5\u5c06\u5217\u8868\u4e2d\u7684\u503c\u4f5c\u4e3a\u5143\u7d20\u63d2\u5165\u4e0a\u4e00\u7ea7\u7684\u5217\u8868\u4e2d\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

list1 = [1, 2, 3] \r\nlist2 = [4, 5, 6] \r\n \r\nresult = list1 + list2 \r\n# [1, 2, 3, 4, 5, 6] \r\nprint(result) \r\n<\/pre>\n

PS\uff1a\u53cc\u661f(**)\u53ef\u4ee5\u62c6\u88c5\u5b57\u5178\uff0c\u5982\u679c\u5c06\u5355\u661f\u548c\u53cc\u661f\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\uff0c\u90a3\u4e48\u5c31\u662f\u88c5\u7bb1\uff0c\u53ef\u4ee5\u5c06\u79bb\u6563\u7684\u503c\u7ec4\u88c5\u6210\u5143\u7ec4(\u5355\u661f)\u548c\u5b57\u5178(\u53cc\u661f)\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

import itertools \r\nlist1 = [1, 2, 3] \r\nlist2 = [4, 5, 6] \r\n# \u76f4\u63a5\u5408\u5e76 \r\n# [1, 2, 3, 4, 5, 6] \r\nresult = [item for item in itertools.chain(list1, list2)] \r\nprint(result) \r\n# \u5408\u5e76\u4e24\u4e2a\u5217\u8868\u7684\u540c\u65f6\uff0c\u6bcf\u4e00\u4e2a\u5217\u8868\u5143\u7d20\u7684\u503c\u52a01 \r\n# [2, 3, 4, 5, 6, 7] \r\nresult = [item + 1 for item in itertools.chain(list1, list2)] \r\nprint(result)<\/pre>\n
\u65b9\u6cd53\uff1a\u53ef\u63a7\u5408\u5e76\uff0c\u820d\u201c\u8fed\u4ee3\u201d\u5176\u8c01<\/strong><\/span><\/div>\n

\u524d\u9762\u4e24\u79cd\u5408\u5e76\u5217\u8868\u7684\u65b9\u5f0f\u56fa\u7136\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u95ee\u9898\u662f\uff0c\u53ea\u80fd\u505a\u5230\u7b80\u5355\u7684\u5408\u5e76\uff0c\u5982\u679c\u8981\u505a\u66f4\u590d\u6742\u7684\u5408\u5e76(\u5982\u5728\u5408\u5e76\u7684\u8fc7\u7a0b\u4e2d\u52a0\u5de5\u7279\u5b9a\u7684\u5217\u8868\u5143\u7d20)\u5c31\u65e0\u6cd5\u505a\u5230\u4e86\u3002\u6240\u4ee5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u7684\u65b9\u5f0f\u5355\u72ec\u5904\u7406\u6bcf\u4e00\u4e2a\u5217\u8868\u5143\u7d20\uff0c\u6211\u79f0\u8fd9\u79cd\u5408\u5e76\u65b9\u5f0f\u4e3a\u53ef\u63a7\u5408\u5e76\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

import itertools \r\nlist1 = [1, 2, 3] \r\nlist2 = [4, 5, 6] \r\n# \u76f4\u63a5\u5408\u5e76 \r\n# [1, 2, 3, 4, 5, 6] \r\nresult = [item for item in itertools.chain(list1, list2)] \r\nprint(result) \r\n# \u5408\u5e76\u4e24\u4e2a\u5217\u8868\u7684\u540c\u65f6\uff0c\u6bcf\u4e00\u4e2a\u5217\u8868\u5143\u7d20\u7684\u503c\u52a01 \r\n# [2, 3, 4, 5, 6, 7] \r\nresult = [item + 1 for item in itertools.chain(list1, list2)] \r\nprint(result)<\/pre>\n
\u65b9\u6cd54\uff1a\u5f3a\u884c\u8f6c\u6362\u7684\u7528\u5904<\/strong><\/span><\/div>\n

\u901a\u8fc7chain\u7c7b\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u53d8\u6210\u4e00\u4e2achain\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u5c06chain\u5bf9\u8c61\u8f6c\u6362\u4e3alist\u5bf9\u8c61\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

from itertools import chain \r\nlist1 = [1, 2, 3] \r\nlist2 = [4, 5, 6] \r\nresult = list(chain(list1, list2)) \r\n# [1, 2, 3, 4, 5, 6] \r\nprint(result) \r\n<\/pre>\n

chain\u7c7b\u6784\u9020\u65b9\u6cd5\u7684\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n

def __init__(self, *iterables) \r\n<\/pre>\n

\u663e\u800c\u6613\u89c1\uff0c\u6784\u9020\u65b9\u6cd5\u7684iterables\u53c2\u6570\u4f7f\u7528\u4e86\u5355\u661f(*)\uff0c\u6240\u4ee5\u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u591a\u4e2a\u5217\u8868\u53c2\u6570\uff0c\u4f8b\u5982\uff0cchain(list1, list2, list3,list4,list5)\u662f\u5408\u6cd5\u7684\uff0c\u56e0\u6b64\uff0c\u672c\u65b9\u6cd5\u53ef\u4ee5\u5408\u5e76\u4efb\u610f\u591a\u4e2a\u5217\u8868\u3002<\/p>\n

\u65b9\u6cd55\uff1a\u6211\u4e0d\u9700\u8981\u91cd\u590d\u7684\u503c<\/strong><\/span><\/div>\n

\u6709\u4e00\u79cd\u7279\u6b8a\u7684\u5408\u5e76\u5217\u8868\u65b9\u5f0f\uff0c\u5c31\u662f\u53bb\u91cd\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u5408\u5e76\u7684\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u4e2d\u6709\u91cd\u590d\u7684\u503c\uff0c\u90a3\u4e48\u53ea\u4fdd\u7559\u4e00\u4e2a\u76f8\u540c\u7684\u503c\u5373\u53ef\u3002\u5176\u5b9e\u5c31\u662f\u5c06\u5408\u5e76\u540e\u7684\u7ed3\u679c\u53d8\u6210\u96c6\u5408\uff0c\u56e0\u6b64\uff0c\u53ef\u4ee5\u7528\u96c6\u5408\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

list1 = [1, 2, 3] \r\nlist2 = [4, 3, 6] \r\nresult = list(set(list1 + list2)) \r\n# [1, 2, 3, 4, 6] \r\nprint(result) \r\n\r\n<\/pre>\n

\u8fd9\u79cd\u5408\u5e76\u5217\u8868\u7684\u65b9\u5f0f\u5c3d\u7ba1\u4f7f\u7528\u4e86\u52a0\u53f7(+)\uff0c\u4f46\u8fd8\u4f7f\u7528set\uff0c\u6240\u4ee5\u5e94\u8be5\u5c5e\u4e8e\u4e00\u79cd\u65b0\u7684\u5408\u5e76\u65b9\u5f0f\uff0c\u56e0\u4e3a\u8fd9\u79cd\u5408\u5e76\u65b9\u5f0f\u6ee1\u8db3\u4e86\u7279\u6b8a\u7684\u9700\u6c42\uff1a\u53bb\u91cd\u3002<\/p>\n

\u65b9\u6cd56\uff1a\u751f\u6210\u5668\u5927\u6cd5<\/strong><\/span><\/div>\n

\u524d\u9762\u7684\u51e0\u79cd\u65b9\u5f0f\u90fd\u662f\u4f7f\u7528\u4e86Python\u4e2d\u7684\u73b0\u6210\u673a\u5236\uff0c\u73b0\u5728\u6765\u70b9\u590d\u6742\u7684\uff1a\u81ea\u5b9a\u4e49\u8f6c\u6362\u51fd\u6570\u3002<\/p>\n

\u8fd9\u79cd\u5408\u5e76\u5217\u8868\u7684\u65b9\u5f0f\u6d89\u53ca\u5230\u5982\u4e0b\u51e0\u79cd\u6280\u672f\uff1a<\/p>\n

1. \u81ea\u5b9a\u4e49Python\u51fd\u6570<\/p>\n

2. \u5355\u661f(*)\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570<\/p>\n

3. Python\u751f\u6210\u5668(Generator)<\/p>\n

4. \u7c7b\u578b\u8f6c\u6362\u5b9e\u73b0<\/p>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

list1 = [1, 2, 3] \r\nlist2 = [4, 5, 6] \r\nlist3 = [7, 8, 9] \r\nlist4 = [10, 11, 12] \r\nlist5 = [\"hello\", 20.1, True] \r\n# \u5408\u5e76\u51fd\u6570\uff0c\u4e5f\u662f\u4e00\u4e2a\u751f\u6210\u5668 \r\ndef merge(*iters): \r\n    for it in iters: \r\n        yield from it \r\nresult = list(merge(list1, list2, 'abcd', [20, 21, 22],list3,list4,list5)) \r\n# [1, 2, 3, 4, 5, 6, 'a', 'b', 'c', 'd', 20, 21, 22, 7, 8, 9, 10, 11, 12, 'hello', 20.1, True] \r\nprint(result)<\/pre>\n

\u8fd9\u6bb5\u4ee3\u7801\u7684merge\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5f62\u5f0f\u7684\u5408\u5e76\u51fd\u6570\uff0c\u800c\u4e14\u4f7f\u7528\u4e86\u5355\u661f(*)\u4f5c\u4e3a\u53c2\u6570\u7c7b\u578b\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f20\u5165\u4efb\u610f\u591a\u4e2a\u5217\u8868\u3002\u672c\u4f8b\u5408\u5e76\u4e867\u4e2a\u5217\u8868\u3002\u5176\u4e2d'abcd'\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5217\u8868\uff0c\u6bcf\u4e00\u4e2a\u5217\u8868\u5143\u7d20\u662f\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n

\u65b9\u6cd57\uff1a\u53c8\u770b\u5230for in\u8868\u8fbe\u5f0f\u4e86<\/strong><\/span><\/div>\n

Python\u7b80\u76f4\u5c06for\u505a\u5230\u4e86\u6781\u81f4\uff0c\u63d0\u4f9b\u4e86for in\u8868\u8fbe\u5f0f\u3002\u8981\u6ce8\u610f\uff0c\u8fd9\u662f\u8868\u8fbe\u5f0f\uff0c\u4e0d\u662f\u8bed\u53e5\u3002\u6240\u4ee5\u53ef\u4ee5\u7528\u5728\u5176\u4ed6\u8868\u8fbe\u5f0f\u4e2d\uff0c\u4f8b\u5982\uff0c\u7528for in\u8868\u8fbe\u5f0f\u751f\u6210\u4e00\u4e2a\u5217\u8868\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

list1 = [1, 2, 3] \r\nlist2 = [4, 5, 6] \r\n# \u5982\u679c\u662f\u5b57\u6bcd\uff0c\u4f1a\u8f93\u51fa\u5bf9\u5e94\u7684ASCII \r\nresult = [ord(item) if str(item).isalpha() else item for item in (list1 + list2 + list('abcd') + [20, 21, 22])] \r\n# [1, 2, 3, 4, 5, 6, 97, 98, 99, 100, 20, 21, 22] \r\nprint(result)<\/pre>\n

\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u4e8e\u590d\u5236\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u800c\u4e14\u53ef\u4ee5\u5728\u5408\u5e76\u7684\u8fc7\u7a0b\u4e2d\u4fee\u6539\u7279\u5b9a\u7684\u5217\u8868\u503c\u3002<\/p>\n

\u65b9\u6cd58\uff1a\u81ea\u8eab\u4e5f\u53ef\u4ee5\u88ab\u4fee\u6539<\/strong><\/span><\/div>\n

\u5728\u5408\u5e76\u5217\u8868\u65f6\uff0c\u5982\u679c\u5e0c\u671b\u4e00\u4e2a\u5217\u8868\u672c\u8eab\u88ab\u4fee\u6539\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5408\u5e76A\u548cB\u4e24\u4e2a\u5217\u8868\u540e\uff0cA\u672c\u8eab\u53d8\u6210\u4e86\u6700\u7ec8\u7684\u4fee\u6539\u7ed3\u679c\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5c06B\u8ffd\u52a0\u5230A\u7684\u540e\u9762\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

list1 = [1, 2, 3] \r\nlist2 = [4, 5, 6] \r\nresult = [] \r\nresult.extend(list1) \r\nresult.extend(list2) \r\n# [1, 2, 3, 4, 5, 6] \r\nprint(result) \r\n# [1, 2, 3, 4, 5, 6] \r\nlist1.extend(list2) \r\nprint(list1)<\/pre>\n

\u5982\u679c\u5e0c\u671b\u4e0d\u4fee\u6539\u53c2\u4e0e\u5408\u5e76\u7684\u5217\u8868\uff0c\u90a3\u4e48\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u5217\u8868\u3002<\/p>\n

\u65b9\u6cd59\uff1aPython\u51fd\u6570\u5e93\u662f\u4e2a\u597d\u4e1c\u897f\uff0c\u5230\u5904\u90fd\u662f\u5b9d\u85cf<\/strong><\/span><\/div>\n

Python\u6709\u4e00\u4e2a\u975e\u5e38\u5e9e\u5927\u7684\u51fd\u6570\u5e93\uff0c\u5176\u4e2d\u4e0d\u4e4f\u7528\u4e8e\u5408\u5e76\u5217\u8868\u7684\u51fd\u6570\uff0c\u5176\u4e2doperator\u6a21\u5757\u4e2d\u7684add\u51fd\u6570\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\uff0c\u5176\u5b9eadd\u5185\u90e8\u4f7f\u7528\u4e86\u52a0\u53f7(+)\u5408\u5e76\u5217\u8868\uff0c\u4e0d\u8fc7\u8fd9\u4e5f\u5e94\u8be5\u7b97\u662f\u4e00\u79cd\u65b9\u6cd5\uff0c\u56e0\u4e3a\u4ee5\u540eadd\u51fd\u6570\u53ef\u80fd\u4f1a\u4f7f\u7528\u5176\u4ed6\u7684\u65b9\u5f0f\u5408\u5e76\u5217\u8868\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

import operator \r\nlist1 = [1, 2, 3] \r\nlist2 = [4, 5, 6] \r\nresult = operator.add(list1, list2) \r\n# [1, 2, 3, 4, 5, 6] \r\nprint(result)<\/pre>\n
\u65b9\u6cd510\uff1a\u8fdc\u5728\u5929\u8fb9\uff0c\u8fd1\u5728\u773c\u524d<\/strong><\/span><\/div>\n

\u524d\u9762\u4ecb\u7ecd\u4e86\u4e00\u5806\u7528\u4e8e\u5408\u5e76\u5217\u8868\u7684API\uff0c\u5176\u5b9e\u5217\u8868\u7c7b(list)\u672c\u8eab\u5c31\u6709\u4e00\u4e2a__add__\u65b9\u6cd5\uff0c\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u5217\u8868\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

list1 = [1,2,3] \r\nlist2 = [4,5,6] \r\n \r\nresult = list.__add__(list1, list2) \r\n# [1, 2, 3, 4, 5, 6] \r\nprint(result)<\/pre>\n
\u65b9\u6cd511\uff1a\u4f20\u7edf\u65b9\u5f0f<\/strong><\/span><\/div>\n

\u6765\u4e2a\u6700\u4f20\u7edf\u7684\u65b9\u5f0f\u4ecb\u7ecd\u4e86\u8fd9\u4e48\u591a\u5408\u5e76\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u5176\u5b9e\u6700\u4f20\u7edf\u7684\u8fd8\u662f\u4e00\u4e2a\u5143\u7d20\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\uff0c\u4e5f\u5c31\u662f\u5217\u8868\u7684append\u65b9\u6cd5\u3002\u90a3\u4e48\u53ef\u80fd\u5f88\u591a\u540c\u5b66\u8981\u95ee\uff0c\u6709\u8fd9\u4e48\u591a\u597d\u7684\u65b9\u5f0f\uff0c\u4e3a\u5565\u8981\u4e00\u4e2a\u5143\u7d20\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5462?\u5c82\u4e0d\u662f\u5f71\u54cd\u6548\u7387?\u5176\u5b9e\u8fd9\u4e5f\u8981\u770b\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e9b\u573a\u666f\uff0c\u5217\u8868\u7684\u503c\u5df2\u7ecf\u88ab\u62c6\u5f00\u4e86(\u4e3a\u4e86\u5904\u7406\u5176\u4ed6\u7684\u4e1a\u52a1)\uff0c\u90a3\u4e48\u5c31\u987a\u9053\u4f7f\u7528append\u65b9\u6cd5\u6328\u4e2a\u6dfb\u52a0\u4e86\uff0c\u53cd\u6b63\u5df2\u7ecf\u88ab\u62c6\u5f00\u4e86\uff0c\u4e0d\u52a0\u767d\u4e0d\u52a0\u3002<\/p>\n

\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\n\r\nlist1 = [1,2,3] \r\nlist2 = [4,5,6] \r\nresult = [] \r\nfor elem in list1: \r\n    result.append(elem) \r\nfor elem in list2: \r\n    result.append(elem) \r\n# [1, 2, 3, 4, 5, 6] \r\nprint(result) \r\n<\/pre>\n
\u65b9\u6cd512\uff1a\u5408\u5e76\u65b9\u5f0f\u4e0d\u591f\uff0c\u5916\u63f4\u6765\u51d1<\/strong><\/span><\/div>\n

\u5176\u5b9ePython\u4e2d\u5408\u5e76\u5217\u8868\u7684\u65b9\u5f0f\u4e5f\u5c31\u8fd9\u4e48\u591a\uff0c\u597d\u50cf\u53ea\u670911\u79cd\uff0c\u524d\u9762\u90fd\u8bf4\u4e86\uff0c\u670912\u79cd\uff0c\u4e3a\u4e86\u51d1\u591f12\u79cd\uff0c\u8fd9\u91cc\u8bf7\u4e86\u4e00\u4e2a\u5916\u63f4\uff0c\u8fd9\u5c31\u662fNumPy\uff0c\u8fd9\u4e2a\u5e93\u4e3b\u8981\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\uff0c\u5bf9\u6570\u636e\u7684\u5904\u7406\u6bd4\u8f83\u5f3a\u5927\uff0c\u7528NumPy\u5408\u5e76Python\u5217\u8868\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

import numpy \r\nlist1 = [1,2,3] \r\nlist2 = [4,5,6] \r\n \r\nresult = numpy.concatenate([list1,list2]).tolist() \r\nprint(result)<\/pre>\n

\u7531\u4e8enumpy.concatenate\u51fd\u6570\u8fd4\u56de\u4e86numpy.ndarray\u7c7b\u578b\uff0c\u6240\u4ee5\u8981\u5f97\u5230Python\u5217\u8868\u5bf9\u8c61\uff0c\u8fd8\u9700\u8981\u4f7f\u7528tolist\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002NumPy\u662f\u7b2c\u4e09\u65b9\u5e93\uff0c\u6240\u4ee5\u9700\u8981\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n

pip install numpy \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u6211\u559c\u6b2212\u8fd9\u4e2a\u6570\u5b57\uff0c\u56e0\u4e3a\u6709\u592a\u591a\u7684\u4e8b\u60c5\u4e0e12\u6709\u5173\u3002\u4e00\u5e74\u670912\u4e2a\u6708;\u53e4\u4ee3\u752812\u4e2a\u65f6\u8fb0(\u5730\u652f)\u8868\u793a\u4e00\u5929\u7684\u65f6\u95f4\u3002\u7528\u4e00\u7eaa […]<\/p>\n","protected":false},"author":317,"featured_media":206651,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-213655","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/213655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=213655"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/213655\/revisions"}],"predecessor-version":[{"id":213708,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/213655\/revisions\/213708"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/206651"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=213655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=213655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=213655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}