{"id":1058573,"date":"2024-12-31T15:17:30","date_gmt":"2024-12-31T07:17:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058573.html"},"modified":"2024-12-31T15:17:33","modified_gmt":"2024-12-31T07:17:33","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e4%b8%a4%e4%b8%aa%e7%9f%a9%e9%98%b5%e7%9b%b8%e4%b9%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058573.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u4e24\u4e2a\u77e9\u9635\u76f8\u4e58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a15525c6-a918-4227-a85b-afe0a96b8f8c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u4e24\u4e2a\u77e9\u9635\u76f8\u4e58\" \/><\/p>\n<p><p> <strong>\u5b9e\u73b0Python\u4e2d\u7684\u77e9\u9635\u76f8\u4e58<\/strong>\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528\u57fa\u7840\u7684\u5d4c\u5957\u5faa\u73af\u3001NumPy\u5e93\u3001\u6216\u8005\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u51e0\u79cd\u5b9e\u73b0\u65b9\u6cd5\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u6765\u7b80\u5316\u548c\u4f18\u5316\u77e9\u9635\u76f8\u4e58\u64cd\u4f5c\u3002<strong>\u4e86\u89e3\u77e9\u9635\u7684\u57fa\u672c\u6982\u5ff5\u3001\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u5b9e\u73b0\u3001\u5229\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\u7684\u9ad8\u6548\u65b9\u6cd5<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u77e9\u9635\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\u4e4b\u524d\uff0c\u9996\u5148\u8981\u4e86\u89e3\u77e9\u9635\u7684\u57fa\u672c\u6982\u5ff5\u548c\u89c4\u5219\u3002\u77e9\u9635\u662f\u4e00\u79cd\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u53ef\u4ee5\u662f\u6570\u5b57\u3001\u53d8\u91cf\u6216\u5176\u4ed6\u77e9\u9635\u3002\u77e9\u9635\u76f8\u4e58\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u77e9\u9635A\u7684\u5217\u6570\u5fc5\u987b\u7b49\u4e8e\u77e9\u9635B\u7684\u884c\u6570\u3002<\/li>\n<li>\u7ed3\u679c\u77e9\u9635C\u7684\u884c\u6570\u7b49\u4e8e\u77e9\u9635A\u7684\u884c\u6570\uff0c\u5217\u6570\u7b49\u4e8e\u77e9\u9635B\u7684\u5217\u6570\u3002<\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u77e9\u9635A\u662f\u4e00\u4e2am\u00d7n\u7684\u77e9\u9635\uff0c\u77e9\u9635B\u662f\u4e00\u4e2an\u00d7p\u7684\u77e9\u9635\uff0c\u90a3\u4e48\u5b83\u4eec\u7684\u4e58\u79efC\u5c06\u662f\u4e00\u4e2am\u00d7p\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u5b9e\u73b0\u4e24\u4e2a\u77e9\u9635\u76f8\u4e58\u7684Python\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def matrix_multiply(A, B):<\/p>\n<p>    # \u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6<\/p>\n<p>    rows_A = len(A)<\/p>\n<p>    cols_A = len(A[0])<\/p>\n<p>    rows_B = len(B)<\/p>\n<p>    cols_B = len(B[0])<\/p>\n<p>    # \u786e\u4fdd\u77e9\u9635\u53ef\u4ee5\u76f8\u4e58<\/p>\n<p>    if cols_A != rows_B:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u65e0\u6cd5\u76f8\u4e58\u7684\u77e9\u9635\uff1aA\u7684\u5217\u6570\u5fc5\u987b\u7b49\u4e8eB\u7684\u884c\u6570&quot;)<\/p>\n<p>    # \u521d\u59cb\u5316\u7ed3\u679c\u77e9\u9635<\/p>\n<p>    result = [[0 for _ in range(cols_B)] for _ in range(rows_A)]<\/p>\n<p>    # \u8fdb\u884c\u77e9\u9635\u76f8\u4e58<\/p>\n<p>    for i in range(rows_A):<\/p>\n<p>        for j in range(cols_B):<\/p>\n<p>            for k in range(cols_A):<\/p>\n<p>                result[i][j] += A[i][k] * B[k][j]<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b\u77e9\u9635<\/strong><\/h2>\n<p>A = [[1, 2, 3], [4, 5, 6]]<\/p>\n<p>B = [[7, 8], [9, 10], [11, 12]]<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u4e58\u79ef<\/strong><\/h2>\n<p>C = matrix_multiply(A, B)<\/p>\n<p>print(C)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u76f8\u4e58<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5NumPy\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\uff0c\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u77e9\u9635<\/strong><\/h2>\n<p>A = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>B = np.array([[7, 8], [9, 10], [11, 12]])<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u4e58\u79ef<\/strong><\/h2>\n<p>C = np.dot(A, B)<\/p>\n<p>print(C)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\uff0c\u53ef\u4ee5\u7528\u5b83\u6765\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def matrix_multiply(A, B):<\/p>\n<p>    # \u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6<\/p>\n<p>    rows_A = len(A)<\/p>\n<p>    cols_A = len(A[0])<\/p>\n<p>    rows_B = len(B)<\/p>\n<p>    cols_B = len(B[0])<\/p>\n<p>    # \u786e\u4fdd\u77e9\u9635\u53ef\u4ee5\u76f8\u4e58<\/p>\n<p>    if cols_A != rows_B:<\/p>\n<p>        raise ValueError(&quot;\u65e0\u6cd5\u76f8\u4e58\u7684\u77e9\u9635\uff1aA\u7684\u5217\u6570\u5fc5\u987b\u7b49\u4e8eB\u7684\u884c\u6570&quot;)<\/p>\n<p>    # \u8fdb\u884c\u77e9\u9635\u76f8\u4e58<\/p>\n<p>    result = [[sum(A[i][k] * B[k][j] for k in range(cols_A)) for j in range(cols_B)] for i in range(rows_A)]<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b\u77e9\u9635<\/strong><\/h2>\n<p>A = [[1, 2, 3], [4, 5, 6]]<\/p>\n<p>B = [[7, 8], [9, 10], [11, 12]]<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u4e58\u79ef<\/strong><\/h2>\n<p>C = matrix_multiply(A, B)<\/p>\n<p>print(C)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u6bd4\u8f83\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>1\u3001\u5d4c\u5957\u5faa\u73af\u4e0eNumPy\u7684\u6027\u80fd\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u867d\u7136\u53ef\u4ee5\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\uff0c\u4f46\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u77e9\u9635\u65f6\uff0c\u6027\u80fd\u8f83\u5dee\u3002NumPy\u5e93\u91c7\u7528\u5e95\u5c42\u4f18\u5316\u548c\u5e76\u884c\u8ba1\u7b97\uff0c\u5904\u7406\u77e9\u9635\u8fd0\u7b97\u65f6\u66f4\u52a0\u9ad8\u6548\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6027\u80fd\u6bd4\u8f83\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import time<\/p>\n<h2><strong>\u751f\u6210\u5927\u89c4\u6a21\u968f\u673a\u77e9\u9635<\/strong><\/h2>\n<p>A = np.random.rand(1000, 1000)<\/p>\n<p>B = np.random.rand(1000, 1000)<\/p>\n<h2><strong>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8ba1\u7b97\u77e9\u9635\u4e58\u79ef<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>C1 = [[sum(A[i][k] * B[k][j] for k in range(len(A[0]))) for j in range(len(B[0]))] for i in range(len(A))]<\/p>\n<p>print(f&quot;\u5d4c\u5957\u5faa\u73af\u8017\u65f6\uff1a{time.time() - start_time:.2f}\u79d2&quot;)<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u77e9\u9635\u4e58\u79ef<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>C2 = np.dot(A, B)<\/p>\n<p>print(f&quot;NumPy\u5e93\u8017\u65f6\uff1a{time.time() - start_time:.2f}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5229\u7528\u5e76\u884c\u8ba1\u7b97\u4f18\u5316\u77e9\u9635\u76f8\u4e58<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u5927\u89c4\u6a21\u7684\u77e9\u9635\u8fd0\u7b97\uff0c\u53ef\u4ee5\u5229\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u8fdb\u4e00\u6b65\u4f18\u5316\u6027\u80fd\u3002NumPy\u5e93\u5df2\u7ecf\u5728\u5e95\u5c42\u8fdb\u884c\u4e86\u5e76\u884c\u4f18\u5316\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e76\u884c\u8ba1\u7b97\u5de5\u5177\uff0c\u5982\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u591a\u7ebf\u7a0b\u4f18\u5316\u77e9\u9635\u76f8\u4e58\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def matrix_multiply_parallel(A, B):<\/p>\n<p>    # \u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6<\/p>\n<p>    rows_A = len(A)<\/p>\n<p>    cols_A = len(A[0])<\/p>\n<p>    rows_B = len(B)<\/p>\n<p>    cols_B = len(B[0])<\/p>\n<p>    # \u786e\u4fdd\u77e9\u9635\u53ef\u4ee5\u76f8\u4e58<\/p>\n<p>    if cols_A != rows_B:<\/p>\n<p>        raise ValueError(&quot;\u65e0\u6cd5\u76f8\u4e58\u7684\u77e9\u9635\uff1aA\u7684\u5217\u6570\u5fc5\u987b\u7b49\u4e8eB\u7684\u884c\u6570&quot;)<\/p>\n<p>    # \u521d\u59cb\u5316\u7ed3\u679c\u77e9\u9635<\/p>\n<p>    result = np.zeros((rows_A, cols_B))<\/p>\n<p>    # \u5b9a\u4e49\u8ba1\u7b97\u5355\u4e2a\u5143\u7d20\u7684\u51fd\u6570<\/p>\n<p>    def compute_element(i, j):<\/p>\n<p>        return sum(A[i][k] * B[k][j] for k in range(cols_A))<\/p>\n<p>    # \u4f7f\u7528\u7ebf\u7a0b\u6c60\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97<\/p>\n<p>    with ThreadPoolExecutor() as executor:<\/p>\n<p>        futures = []<\/p>\n<p>        for i in range(rows_A):<\/p>\n<p>            for j in range(cols_B):<\/p>\n<p>                futures.append(executor.submit(compute_element, i, j))<\/p>\n<p>        for idx, future in enumerate(futures):<\/p>\n<p>            i = idx \/\/ cols_B<\/p>\n<p>            j = idx % cols_B<\/p>\n<p>            result[i, j] = future.result()<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b\u77e9\u9635<\/strong><\/h2>\n<p>A = np.random.rand(1000, 1000)<\/p>\n<p>B = np.random.rand(1000, 1000)<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u4e58\u79ef<\/strong><\/h2>\n<p>C = matrix_multiply_parallel(A, B)<\/p>\n<p>print(C)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51e0\u79cd\u5728Python\u4e2d\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u9996\u5148\u8ba8\u8bba\u4e86\u77e9\u9635\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u7136\u540e\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3001NumPy\u5e93\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u4e86\u77e9\u9635\u76f8\u4e58\u3002\u6700\u540e\uff0c\u6211\u4eec\u6bd4\u8f83\u4e86\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u5e76\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528\u5e76\u884c\u8ba1\u7b97\u4f18\u5316\u77e9\u9635\u76f8\u4e58\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u9ad8\u6548\u3002<\/strong>\u5bf9\u4e8e\u5927\u89c4\u6a21\u77e9\u9635\u8fd0\u7b97\uff0c\u53ef\u4ee5\u8003\u8651\u5229\u7528\u5e76\u884c\u8ba1\u7b97\u5de5\u5177\u8fdb\u4e00\u6b65\u4f18\u5316\u6027\u80fd\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u6700\u9002\u5408\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u63d0\u5347\u77e9\u9635\u8fd0\u7b97\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528<code>numpy.dot()<\/code>\u6216<code>@<\/code>\u8fd0\u7b97\u7b26\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nA = np.array([[1, 2], [3, 4]])\nB = np.array([[5, 6], [7, 8]])\nC = np.dot(A, B)  # \u6216\u8005\u4f7f\u7528 C = A @ B\nprint(C)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u8f93\u51fa\u77e9\u9635C\u7684\u4e58\u79ef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7ef4\u5ea6\u4e0d\u5339\u914d\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u77e9\u9635\u76f8\u4e58\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u7b2c\u4e00\u4e2a\u77e9\u9635\u7684\u5217\u6570\u4e0e\u7b2c\u4e8c\u4e2a\u77e9\u9635\u7684\u884c\u6570\u76f8\u7b49\u3002\u5982\u679c\u4e0d\u5339\u914d\uff0cNumPy\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u8fdb\u884c\u4e58\u6cd5\u4e4b\u524d\u68c0\u67e5\u77e9\u9635\u7684\u7ef4\u5ea6\u3002\u53ef\u4ee5\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">if A.shape[1] != B.shape[0]:\n    raise ValueError(&quot;\u77e9\u9635\u7ef4\u5ea6\u4e0d\u5339\u914d\uff0c\u65e0\u6cd5\u76f8\u4e58&quot;)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u7eafPython\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\uff0c\u800c\u4e0d\u4f9d\u8d56\u4e8e\u5916\u90e8\u5e93\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u867d\u7136\u4f7f\u7528NumPy\u66f4\u52a0\u9ad8\u6548\uff0c\u4f46\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u5728\u7eafPython\u4e2d\u5b9e\u73b0\u77e9\u9635\u4e58\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def matrix_multiply(A, B):\n    result = [[0 for _ in range(len(B[0]))] for _ in range(len(A))]\n    for i in range(len(A)):\n        for j in range(len(B[0])):\n            for k in range(len(B)):\n                result[i][j] += A[i][k] * B[k][j]\n    return result\n\nA = [[1, 2], [3, 4]]\nB = [[5, 6], [7, 8]]\nC = matrix_multiply(A, B)\nprint(C)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5b9e\u73b0\u4e86\u77e9\u9635\u4e58\u6cd5\u7684\u57fa\u672c\u903b\u8f91\uff0c\u8f93\u51fa\u7ed3\u679c\u4e0e\u4f7f\u7528NumPy\u76f8\u540c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9e\u73b0Python\u4e2d\u7684\u77e9\u9635\u76f8\u4e58\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528\u57fa\u7840\u7684\u5d4c\u5957\u5faa\u73af\u3001NumPy\u5e93\u3001\u6216\u8005\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002\u5728\u672c [&hellip;]","protected":false},"author":3,"featured_media":1058580,"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\/1058573"}],"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=1058573"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058573\/revisions"}],"predecessor-version":[{"id":1058581,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058573\/revisions\/1058581"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1058580"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1058573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1058573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1058573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}