{"id":1150564,"date":"2025-01-13T17:03:00","date_gmt":"2025-01-13T09:03:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1150564.html"},"modified":"2025-01-13T17:03:03","modified_gmt":"2025-01-13T09:03:03","slug":"python%e7%9f%a9%e9%98%b5%e5%a6%82%e4%bd%95%e6%89%a9%e5%b1%95%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1150564.html","title":{"rendered":"python\u77e9\u9635\u5982\u4f55\u6269\u5c55\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181023\/2051db30-4cf0-43de-a836-8a1ef8706b22.webp\" alt=\"python\u77e9\u9635\u5982\u4f55\u6269\u5c55\u77e9\u9635\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6269\u5c55\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528NumPy\u5e93\u7684concatenate\u3001vstack\u548chstack\u51fd\u6570\u3001\u4f7f\u7528NumPy\u7684append\u51fd\u6570\u3001\u4ee5\u53ca\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u3002<\/strong>\u5176\u4e2d\uff0cNumPy\u5e93\u7684concatenate\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u64cd\u4f5c\u65b9\u5f0f\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u7684concatenate\u51fd\u6570\u6765\u6269\u5c55\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>NumPy\u5e93\u662fPython\u4e2d\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u7684\u5f3a\u5927\u529f\u80fd\u3002\u5229\u7528NumPy\u5e93\u7684concatenate\u51fd\u6570\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u77e9\u9635\u8fdb\u884c\u7eb5\u5411\u6216\u6a2a\u5411\u6269\u5c55\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u4e24\u4e2a\u77e9\u9635\u8fdb\u884c\u7eb5\u5411\u62fc\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u4f7f\u7528concatenate\u51fd\u6570\u8fdb\u884c\u7eb5\u5411\u62fc\u63a5<\/strong><\/h2>\n<p>extended_matrix = np.concatenate((matrix1, matrix2), axis=0)<\/p>\n<p>print(extended_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001NUMPY\u5e93\u7684concatenate\u51fd\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u7684concatenate\u51fd\u6570\u662f\u7528\u4e8e\u62fc\u63a5\u6570\u7ec4\u548c\u77e9\u9635\u7684\u4e00\u4e2a\u91cd\u8981\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u6cbf\u7740\u6307\u5b9a\u7684\u8f74\u5c06\u591a\u4e2a\u6570\u7ec4\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002concatenate\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numpy.concatenate((a1, a2, ...), axis=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u53c2\u6570a1, a2, &#8230;\u662f\u9700\u8981\u62fc\u63a5\u7684\u6570\u7ec4\u6216\u77e9\u9635\uff0caxis\u53c2\u6570\u6307\u5b9a\u62fc\u63a5\u7684\u65b9\u5411\uff0c0\u8868\u793a\u6cbf\u7740\u884c\u65b9\u5411\u62fc\u63a5\uff0c1\u8868\u793a\u6cbf\u7740\u5217\u65b9\u5411\u62fc\u63a5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7eb5\u5411\u6269\u5c55\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u7eb5\u5411\u6269\u5c55\u77e9\u9635\u662f\u6307\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u77e9\u9635\u5728\u884c\u65b9\u5411\u4e0a\u8fdb\u884c\u62fc\u63a5\uff0c\u4f7f\u5f97\u62fc\u63a5\u540e\u7684\u77e9\u9635\u884c\u6570\u589e\u52a0\u800c\u5217\u6570\u4e0d\u53d8\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u4f7f\u7528concatenate\u51fd\u6570\u8fdb\u884c\u7eb5\u5411\u62fc\u63a5<\/strong><\/h2>\n<p>extended_matrix = np.concatenate((matrix1, matrix2), axis=0)<\/p>\n<p>print(extended_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmatrix1\u548cmatrix2\u662f\u4e24\u4e2a2&#215;2\u7684\u77e9\u9635\uff0c\u4f7f\u7528concatenate\u51fd\u6570\u6cbf\u7740\u884c\u65b9\u5411\uff08axis=0\uff09\u8fdb\u884c\u62fc\u63a5\u540e\uff0c\u5f97\u5230\u7684extended_matrix\u662f\u4e00\u4e2a4&#215;2\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6a2a\u5411\u6269\u5c55\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u6a2a\u5411\u6269\u5c55\u77e9\u9635\u662f\u6307\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u77e9\u9635\u5728\u5217\u65b9\u5411\u4e0a\u8fdb\u884c\u62fc\u63a5\uff0c\u4f7f\u5f97\u62fc\u63a5\u540e\u7684\u77e9\u9635\u5217\u6570\u589e\u52a0\u800c\u884c\u6570\u4e0d\u53d8\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u4f7f\u7528concatenate\u51fd\u6570\u8fdb\u884c\u6a2a\u5411\u62fc\u63a5<\/strong><\/h2>\n<p>extended_matrix = np.concatenate((matrix1, matrix2), axis=1)<\/p>\n<p>print(extended_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmatrix1\u548cmatrix2\u662f\u4e24\u4e2a2&#215;2\u7684\u77e9\u9635\uff0c\u4f7f\u7528concatenate\u51fd\u6570\u6cbf\u7740\u5217\u65b9\u5411\uff08axis=1\uff09\u8fdb\u884c\u62fc\u63a5\u540e\uff0c\u5f97\u5230\u7684extended_matrix\u662f\u4e00\u4e2a2&#215;4\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001NUMPY\u5e93\u7684vstack\u548chstack\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86concatenate\u51fd\u6570\uff0cNumPy\u5e93\u8fd8\u63d0\u4f9b\u4e86vstack\u548chstack\u51fd\u6570\uff0c\u7528\u4e8e\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u7eb5\u5411\u548c\u6a2a\u5411\u7684\u77e9\u9635\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528vstack\u8fdb\u884c\u7eb5\u5411\u6269\u5c55<\/h4>\n<\/p>\n<p><p>vstack\u51fd\u6570\u662f\u7528\u4e8e\u5c06\u591a\u4e2a\u6570\u7ec4\u6cbf\u7740\u884c\u65b9\u5411\u8fdb\u884c\u5806\u53e0\uff08\u7eb5\u5411\u62fc\u63a5\uff09\u7684\u5de5\u5177\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numpy.vstack(tup)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u53c2\u6570tup\u662f\u9700\u8981\u5806\u53e0\u7684\u6570\u7ec4\u6216\u77e9\u9635\u7684\u5143\u7ec4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u4f7f\u7528vstack\u51fd\u6570\u8fdb\u884c\u7eb5\u5411\u62fc\u63a5<\/strong><\/h2>\n<p>extended_matrix = np.vstack((matrix1, matrix2))<\/p>\n<p>print(extended_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmatrix1\u548cmatrix2\u662f\u4e24\u4e2a2&#215;2\u7684\u77e9\u9635\uff0c\u4f7f\u7528vstack\u51fd\u6570\u8fdb\u884c\u7eb5\u5411\u62fc\u63a5\u540e\uff0c\u5f97\u5230\u7684extended_matrix\u662f\u4e00\u4e2a4&#215;2\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528hstack\u8fdb\u884c\u6a2a\u5411\u6269\u5c55<\/h4>\n<\/p>\n<p><p>hstack\u51fd\u6570\u662f\u7528\u4e8e\u5c06\u591a\u4e2a\u6570\u7ec4\u6cbf\u7740\u5217\u65b9\u5411\u8fdb\u884c\u5806\u53e0\uff08\u6a2a\u5411\u62fc\u63a5\uff09\u7684\u5de5\u5177\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numpy.hstack(tup)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u53c2\u6570tup\u662f\u9700\u8981\u5806\u53e0\u7684\u6570\u7ec4\u6216\u77e9\u9635\u7684\u5143\u7ec4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u4f7f\u7528hstack\u51fd\u6570\u8fdb\u884c\u6a2a\u5411\u62fc\u63a5<\/strong><\/h2>\n<p>extended_matrix = np.hstack((matrix1, matrix2))<\/p>\n<p>print(extended_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmatrix1\u548cmatrix2\u662f\u4e24\u4e2a2&#215;2\u7684\u77e9\u9635\uff0c\u4f7f\u7528hstack\u51fd\u6570\u8fdb\u884c\u6a2a\u5411\u62fc\u63a5\u540e\uff0c\u5f97\u5230\u7684extended_matrix\u662f\u4e00\u4e2a2&#215;4\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001NUMPY\u5e93\u7684append\u51fd\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u7684append\u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u7ec4\u548c\u77e9\u9635\u62fc\u63a5\u7684\u91cd\u8981\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u7ec4\u6216\u77e9\u9635\u8ffd\u52a0\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u6216\u77e9\u9635\u7684\u672b\u5c3e\u3002append\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numpy.append(arr, values, axis=None)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u53c2\u6570arr\u662f\u9700\u8981\u8ffd\u52a0\u7684\u6570\u7ec4\u6216\u77e9\u9635\uff0cvalues\u662f\u9700\u8981\u8ffd\u52a0\u7684\u503c\uff0caxis\u53c2\u6570\u6307\u5b9a\u62fc\u63a5\u7684\u65b9\u5411\uff0cNone\u8868\u793a\u5c06values\u5c55\u5e73\u540e\u8ffd\u52a0\u5230arr\u7684\u672b\u5c3e\uff0c0\u8868\u793a\u6cbf\u7740\u884c\u65b9\u5411\u8ffd\u52a0\uff0c1\u8868\u793a\u6cbf\u7740\u5217\u65b9\u5411\u8ffd\u52a0\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7eb5\u5411\u6269\u5c55\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528append\u51fd\u6570\u8fdb\u884c\u7eb5\u5411\u6269\u5c55\u65f6\uff0c\u9700\u8981\u6307\u5b9aaxis\u53c2\u6570\u4e3a0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u4f7f\u7528append\u51fd\u6570\u8fdb\u884c\u7eb5\u5411\u62fc\u63a5<\/strong><\/h2>\n<p>extended_matrix = np.append(matrix1, matrix2, axis=0)<\/p>\n<p>print(extended_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmatrix1\u548cmatrix2\u662f\u4e24\u4e2a2&#215;2\u7684\u77e9\u9635\uff0c\u4f7f\u7528append\u51fd\u6570\u8fdb\u884c\u7eb5\u5411\u62fc\u63a5\u540e\uff0c\u5f97\u5230\u7684extended_matrix\u662f\u4e00\u4e2a4&#215;2\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6a2a\u5411\u6269\u5c55\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528append\u51fd\u6570\u8fdb\u884c\u6a2a\u5411\u6269\u5c55\u65f6\uff0c\u9700\u8981\u6307\u5b9aaxis\u53c2\u6570\u4e3a1\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u4f7f\u7528append\u51fd\u6570\u8fdb\u884c\u6a2a\u5411\u62fc\u63a5<\/strong><\/h2>\n<p>extended_matrix = np.append(matrix1, matrix2, axis=1)<\/p>\n<p>print(extended_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmatrix1\u548cmatrix2\u662f\u4e24\u4e2a2&#215;2\u7684\u77e9\u9635\uff0c\u4f7f\u7528append\u51fd\u6570\u8fdb\u884c\u6a2a\u5411\u62fc\u63a5\u540e\uff0c\u5f97\u5230\u7684extended_matrix\u662f\u4e00\u4e2a2&#215;4\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u8fdb\u884c\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528NumPy\u5e93\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5217\u8868\u63a8\u5bfc\u6765\u8fdb\u884c\u77e9\u9635\u7684\u6269\u5c55\u3002\u5217\u8868\u63a8\u5bfc\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\u6216\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7eb5\u5411\u6269\u5c55\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u8fdb\u884c\u7eb5\u5411\u6269\u5c55\u65f6\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u77e9\u9635\u7684\u884c\u62fc\u63a5\u5728\u4e00\u8d77\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/p>\n<p>matrix1 = [[1, 2], [3, 4]]<\/p>\n<p>matrix2 = [[5, 6], [7, 8]]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u8fdb\u884c\u7eb5\u5411\u62fc\u63a5<\/strong><\/h2>\n<p>extended_matrix = [row for row in matrix1] + [row for row in matrix2]<\/p>\n<p>print(extended_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmatrix1\u548cmatrix2\u662f\u4e24\u4e2a2&#215;2\u7684\u5217\u8868\u8868\u793a\u7684\u77e9\u9635\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u8fdb\u884c\u7eb5\u5411\u62fc\u63a5\u540e\uff0c\u5f97\u5230\u7684extended_matrix\u662f\u4e00\u4e2a4&#215;2\u7684\u5217\u8868\u8868\u793a\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6a2a\u5411\u6269\u5c55\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u8fdb\u884c\u6a2a\u5411\u6269\u5c55\u65f6\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u77e9\u9635\u7684\u5217\u62fc\u63a5\u5728\u4e00\u8d77\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/p>\n<p>matrix1 = [[1, 2], [3, 4]]<\/p>\n<p>matrix2 = [[5, 6], [7, 8]]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u8fdb\u884c\u6a2a\u5411\u62fc\u63a5<\/strong><\/h2>\n<p>extended_matrix = [row1 + row2 for row1, row2 in zip(matrix1, matrix2)]<\/p>\n<p>print(extended_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cmatrix1\u548cmatrix2\u662f\u4e24\u4e2a2&#215;2\u7684\u5217\u8868\u8868\u793a\u7684\u77e9\u9635\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u8fdb\u884c\u6a2a\u5411\u62fc\u63a5\u540e\uff0c\u5f97\u5230\u7684extended_matrix\u662f\u4e00\u4e2a2&#215;4\u7684\u5217\u8868\u8868\u793a\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6269\u5c55\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528NumPy\u5e93\u7684concatenate\u3001vstack\u548chstack\u51fd\u6570\u3002\u6b64\u5916\uff0cNumPy\u5e93\u7684append\u51fd\u6570\u548cPython\u7684\u5217\u8868\u63a8\u5bfc\u4e5f\u53ef\u4ee5\u7528\u4e8e\u77e9\u9635\u7684\u6269\u5c55\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u7279\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\u6548\u7387\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528NumPy\u5e93\u7684concatenate\u51fd\u6570<\/strong>\uff1a\u53ef\u4ee5\u7075\u6d3b\u5730\u6cbf\u7740\u6307\u5b9a\u7684\u8f74\u8fdb\u884c\u77e9\u9635\u62fc\u63a5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u591a\u4e2a\u77e9\u9635\u8fdb\u884c\u590d\u6742\u62fc\u63a5\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u4f7f\u7528NumPy\u5e93\u7684vstack\u548chstack\u51fd\u6570<\/strong>\uff1a\u63d0\u4f9b\u4e86\u66f4\u65b9\u4fbf\u7684\u63a5\u53e3\uff0c\u7528\u4e8e\u7eb5\u5411\u548c\u6a2a\u5411\u7684\u77e9\u9635\u62fc\u63a5\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u62fc\u63a5\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u4f7f\u7528NumPy\u5e93\u7684append\u51fd\u6570<\/strong>\uff1a\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u7ec4\u6216\u77e9\u9635\u8ffd\u52a0\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u6216\u77e9\u9635\u7684\u672b\u5c3e\uff0c\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u8ffd\u52a0\u6570\u636e\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc<\/strong>\uff1a\u53ef\u4ee5\u5229\u7528Python\u7684\u7b80\u6d01\u8bed\u6cd5\u8fdb\u884c\u77e9\u9635\u7684\u62fc\u63a5\uff0c\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u548c\u7b80\u5355\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u89c4\u6a21\u6765\u8fdb\u884c\u6743\u8861\u548c\u9009\u62e9\uff0c\u4ee5\u8fbe\u5230\u6700\u4f18\u7684\u6548\u679c\u3002\u901a\u8fc7\u5bf9\u4e0d\u540c\u65b9\u6cd5\u7684\u7406\u89e3\u548c\u638c\u63e1\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u77e9\u9635\u6269\u5c55\u95ee\u9898\uff0c\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6269\u5c55\u4e00\u4e2a\u77e9\u9635\u7684\u5c3a\u5bf8\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u6269\u5c55\u77e9\u9635\u7684\u5c3a\u5bf8\u3002\u901a\u8fc7<code>numpy.resize()<\/code>\u6216<code>numpy.pad()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u589e\u52a0\u77e9\u9635\u7684\u884c\u6570\u6216\u5217\u6570\u3002\u4f8b\u5982\uff0c<code>numpy.pad()<\/code>\u53ef\u4ee5\u5728\u77e9\u9635\u7684\u8fb9\u754c\u6dfb\u52a0\u96f6\u6216\u5176\u4ed6\u6307\u5b9a\u503c\uff0c\u800c<code>numpy.resize()<\/code>\u5219\u53ef\u4ee5\u6539\u53d8\u77e9\u9635\u7684\u5f62\u72b6\uff0c\u4f46\u6ce8\u610f\u65b0\u77e9\u9635\u7684\u5143\u7d20\u662f\u4ece\u539f\u59cb\u77e9\u9635\u590d\u5236\u7684\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u3002<\/p>\n<p><strong>\u6269\u5c55\u77e9\u9635\u65f6\u4f1a\u5f71\u54cd\u539f\u6709\u6570\u636e\u5417\uff1f<\/strong><br \/>\u6269\u5c55\u77e9\u9635\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e\u6240\u4f7f\u7528\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.pad()<\/code>\u6dfb\u52a0\u7684\u5143\u7d20\u662f\u65b0\u7684\uff0c\u53ef\u4ee5\u662f\u96f6\u6216\u5176\u4ed6\u503c\uff0c\u4e0d\u4f1a\u5f71\u54cd\u539f\u6709\u7684\u6570\u636e\u3002\u800c\u4f7f\u7528<code>numpy.resize()<\/code>\uff0c\u5982\u679c\u539f\u77e9\u9635\u7684\u5143\u7d20\u6570\u91cf\u5c11\u4e8e\u65b0\u77e9\u9635\u7684\u5143\u7d20\u6570\u91cf\uff0c\u53ef\u80fd\u4f1a\u91cd\u590d\u539f\u59cb\u5143\u7d20\uff0c\u8fd9\u6837\u4f1a\u6539\u53d8\u6570\u636e\u7684\u5206\u5e03\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u6269\u5c55\u77e9\u9635\u7684\u540c\u65f6\u8fdb\u884c\u6570\u636e\u586b\u5145\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u5728\u6269\u5c55\u77e9\u9635\u7684\u540c\u65f6\u8fdb\u884c\u6570\u636e\u586b\u5145\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.pad()<\/code>\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u586b\u5145\u7684\u503c\u3002\u8fd9\u4f7f\u5f97\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u81ea\u5b9a\u4e49\u6269\u5c55\u540e\u7684\u77e9\u9635\u5185\u5bb9\uff0c\u6bd4\u5982\u586b\u5145\u7279\u5b9a\u7684\u6570\u5b57\u6216\u4fdd\u6301\u67d0\u79cd\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6269\u5c55\u77e9\u9635\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1f<\/strong><br \/>\u6269\u5c55\u77e9\u9635\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u5c24\u5176\u662f\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6df1\u5ea6\u5b66\u4e60\u4e2d\u3002\u4f8b\u5982\uff0c\u5728\u56fe\u50cf\u5904\u7406\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u56fe\u50cf\u77e9\u9635\u6269\u5c55\u4ee5\u9002\u5e94\u6a21\u578b\u8f93\u5165\u8981\u6c42\uff0c\u6216\u8005\u5728\u6570\u636e\u9884\u5904\u7406\u65f6\u4e3a\u7f3a\u5931\u503c\u586b\u5145\uff0c\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6269\u5c55\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528NumPy\u5e93\u7684concatenate\u3001vstack\u548ch [&hellip;]","protected":false},"author":3,"featured_media":1150571,"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\/1150564"}],"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=1150564"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150564\/revisions"}],"predecessor-version":[{"id":1150574,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150564\/revisions\/1150574"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1150571"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1150564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1150564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1150564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}