{"id":1165549,"date":"2025-01-15T15:21:41","date_gmt":"2025-01-15T07:21:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165549.html"},"modified":"2025-01-15T15:21:44","modified_gmt":"2025-01-15T07:21:44","slug":"python%e5%a6%82%e4%bd%95%e6%9e%84%e5%bb%ba%e5%b5%8c%e5%a5%97%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1165549.html","title":{"rendered":"python\u5982\u4f55\u6784\u5efa\u5d4c\u5957\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205845\/d60960cc-d1c5-4e5e-a12c-e0a4cbfe3ed1.webp\" alt=\"python\u5982\u4f55\u6784\u5efa\u5d4c\u5957\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>Python\u6784\u5efa\u5d4c\u5957\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u5d4c\u5957\u3001numpy\u5e93\u7b49\u3002<\/strong> \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7b80\u6d01\u9ad8\u6548\u5730\u751f\u6210\u5d4c\u5957\u6570\u7ec4\uff0c\u5faa\u73af\u5d4c\u5957\u5219\u53ef\u4ee5\u7075\u6d3b\u5730\u6784\u5efa\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff0cnumpy\u5e93\u5219\u80fd\u591f\u5904\u7406\u5927\u89c4\u6a21\u7684\u591a\u7ef4\u6570\u7ec4\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u6784\u5efa\u5d4c\u5957\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u6784\u5efa\u5d4c\u5957\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u6570\u7ec4\u751f\u6210\u65b9\u5f0f\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u751f\u6210\u89c4\u5219\u7684\u5d4c\u5957\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_array = [[j for j in range(3)] for i in range(3)]<\/p>\n<p>print(nested_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4<code>[[0, 1, 2], [0, 1, 2], [0, 1, 2]]<\/code>\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u5728Python\u4e2d\u975e\u5e38\u5f3a\u5927\uff0c\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u66f4\u590d\u6742\u7684\u5d4c\u5957\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>\u591a\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u751f\u6210\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u5d4c\u5957\u591a\u4e2a\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u751f\u6210\u4e00\u4e2a3x3x3\u7684\u4e09\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_array = [[[k for k in range(3)] for j in range(3)] for i in range(3)]<\/p>\n<p>print(nested_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u751f\u6210\u4e86\u4e00\u4e2a3x3x3\u7684\u4e09\u7ef4\u6570\u7ec4<code>[[[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]]<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u4efb\u610f\u7ef4\u5ea6\u7684\u5d4c\u5957\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5faa\u73af\u5d4c\u5957\u6784\u5efa\u5d4c\u5957\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u5d4c\u5957\u662f\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u6784\u5efa\u590d\u6742\u7684\u5d4c\u5957\u6570\u7ec4\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_array = []<\/p>\n<p>for i in range(3):<\/p>\n<p>    row = []<\/p>\n<p>    for j in range(3):<\/p>\n<p>        row.append(j)<\/p>\n<p>    nested_array.append(row)<\/p>\n<p>print(nested_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u901a\u8fc7\u5faa\u73af\u5d4c\u5957\u751f\u6210\u4e86\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4<code>[[0, 1, 2], [0, 1, 2], [0, 1, 2]]<\/code>\u3002\u5faa\u73af\u5d4c\u5957\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u5728\u751f\u6210\u6570\u7ec4\u7684\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u66f4\u591a\u7684\u903b\u8f91\u64cd\u4f5c\uff0c\u4f8b\u5982\u6839\u636e\u6761\u4ef6\u751f\u6210\u4e0d\u540c\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u52a8\u6001\u751f\u6210\u5d4c\u5957\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5faa\u73af\u5d4c\u5957\u8fd8\u53ef\u4ee5\u7528\u4e8e\u52a8\u6001\u751f\u6210\u5d4c\u5957\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u7ef4\u5ea6\u751f\u6210\u4e00\u4e2a\u5d4c\u5957\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_nested_array(dimensions):<\/p>\n<p>    if len(dimensions) == 1:<\/p>\n<p>        return [0] * dimensions[0]<\/p>\n<p>    return [create_nested_array(dimensions[1:]) for _ in range(dimensions[0])]<\/p>\n<p>nested_array = create_nested_array([3, 3, 3])<\/p>\n<p>print(nested_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570<code>create_nested_array<\/code>\uff0c\u53ef\u4ee5\u6839\u636e\u8f93\u5165\u7684\u7ef4\u5ea6\u751f\u6210\u5d4c\u5957\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>[3, 3, 3]<\/code>\u751f\u6210\u4e00\u4e2a3x3x3\u7684\u4e09\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528numpy\u5e93\u6784\u5efa\u5d4c\u5957\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u7684\u591a\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>nested_array = np.zeros((3, 3), dtype=int)<\/p>\n<p>print(nested_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528numpy\u7684<code>zeros<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u5168\u90e8\u4e3a0\u3002numpy\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u751f\u6210\u6570\u7ec4\u7684\u51fd\u6570\uff0c\u4f8b\u5982<code>ones<\/code>\u3001<code>full<\/code>\u3001<code>arange<\/code>\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u751f\u6210\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>\u591a\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>numpy\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u591a\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u751f\u6210\u4e00\u4e2a3x3x3\u7684\u4e09\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_array = np.zeros((3, 3, 3), dtype=int)<\/p>\n<p>print(nested_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u751f\u6210\u4e86\u4e00\u4e2a3x3x3\u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u5168\u90e8\u4e3a0\u3002\u901a\u8fc7numpy\uff0c\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u4efb\u610f\u7ef4\u5ea6\u7684\u6570\u7ec4\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u4f8b\u5982<code>reshape<\/code>\u3001<code>transpose<\/code>\u3001<code>concatenate<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u7ec4\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>numpy\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u8fd0\u7b97\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5d4c\u5957\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5bf9\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u52a0\u6cd5\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>array2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])<\/p>\n<p>result = array1 + array2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5bf9\u4e24\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u4e86\u52a0\u6cd5\u8fd0\u7b97\uff0c\u7ed3\u679c\u4e3a<code>[[10, 10, 10], [10, 10, 10], [10, 10, 10]]<\/code>\u3002numpy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u8fd0\u7b97\u51fd\u6570\uff0c\u4f8b\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u3001\u77e9\u9635\u4e58\u6cd5\u7b49\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u5404\u79cd\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6784\u5efa\u590d\u6742\u5d4c\u5957\u6570\u7ec4\u793a\u4f8b<\/h3>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e00\uff1a\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5728\u6bcf\u4e2a\u5143\u7d20\u4e2d\u5d4c\u5957\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">nested_array = [[[j for j in range(3)] for _ in range(3)] for _ in range(3)]<\/p>\n<p>print(nested_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u751f\u6210\u4e86\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5728\u6bcf\u4e2a\u5143\u7d20\u4e2d\u5d4c\u5957\u4e86\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7ed3\u679c\u4e3a<code>[[[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e8c\uff1a\u751f\u6210\u4e00\u4e2a3x3x3\u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u5e76\u5728\u6bcf\u4e2a\u5143\u7d20\u4e2d\u5d4c\u5957\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">nested_array = [[[[k for k in range(3)] for _ in range(3)] for _ in range(3)] for _ in range(3)]<\/p>\n<p>print(nested_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u751f\u6210\u4e86\u4e00\u4e2a3x3x3\u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u5e76\u5728\u6bcf\u4e2a\u5143\u7d20\u4e2d\u5d4c\u5957\u4e86\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u7ed3\u679c\u4e3a<code>[[[[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]], [[[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]], [[[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]]]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5d4c\u5957\u6570\u7ec4\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u6570\u7ec4\u5728\u8bb8\u591a\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790\u4e0e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e0e\u5904\u7406\u9886\u57df\uff0c\u5d4c\u5957\u6570\u7ec4\u5e38\u7528\u4e8e\u5b58\u50a8\u548c\u5904\u7406\u591a\u7ef4\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4e8c\u7ef4\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u77e9\u9635\u6570\u636e\uff0c\u4e09\u7ef4\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u56fe\u50cf\u6570\u636e\uff0c\u56db\u7ef4\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u89c6\u9891\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>print(matrix)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3x3\u7684\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>image = np.zeros((3, 3, 3), dtype=int)<\/p>\n<p>print(image)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3x3x3\u7684\u56db\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>video = np.zeros((3, 3, 3, 3), dtype=int)<\/p>\n<p>print(video)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6570\u5b66\u4e0e\u79d1\u5b66\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u5b66\u4e0e\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\uff0c\u5d4c\u5957\u6570\u7ec4\u5e38\u7528\u4e8e\u8868\u793a\u548c\u64cd\u4f5c\u591a\u7ef4\u77e9\u9635\u548c\u5411\u91cf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u6570\u7ec4\u8868\u793a\u4e00\u4e2a\u4e09\u7ef4\u5411\u91cf\u7684\u96c6\u5408\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u4e09\u7ef4\u5411\u91cf\u7684\u96c6\u5408<\/strong><\/h2>\n<p>vectors = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>print(vectors)<\/p>\n<h2><strong>\u8ba1\u7b97\u5411\u91cf\u7684\u8303\u6570<\/strong><\/h2>\n<p>norms = np.linalg.norm(vectors, axis=1)<\/p>\n<p>print(norms)<\/p>\n<h2><strong>\u8ba1\u7b97\u5411\u91cf\u7684\u70b9\u79ef<\/strong><\/h2>\n<p>dot_product = np.dot(vectors[0], vectors[1])<\/p>\n<p>print(dot_product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66<\/h4>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u9886\u57df\uff0c\u5d4c\u5957\u6570\u7ec4\u5e38\u7528\u4e8e\u8868\u793a\u548c\u64cd\u4f5c\u56fe\u5f62\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u6570\u7ec4\u8868\u793a\u4e00\u4e2a\u4e09\u7ef4\u6a21\u578b\u7684\u9876\u70b9\u5750\u6807\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u56fe\u5f62\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u4e09\u7ef4\u6a21\u578b\u7684\u9876\u70b9\u5750\u6807<\/strong><\/h2>\n<p>vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])<\/p>\n<p>print(vertices)<\/p>\n<h2><strong>\u8fdb\u884c\u5e73\u79fb\u53d8\u6362<\/strong><\/h2>\n<p>translation_matrix = np.array([[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 0, 1]])<\/p>\n<p>translated_vertices = np.dot(np.hstack((vertices, np.ones((vertices.shape[0], 1)))), translation_matrix.T)<\/p>\n<p>print(translated_vertices)<\/p>\n<h2><strong>\u8fdb\u884c\u65cb\u8f6c\u53d8\u6362<\/strong><\/h2>\n<p>angle = np.pi \/ 4<\/p>\n<p>rotation_matrix = np.array([[np.cos(angle), -np.sin(angle), 0, 0], [np.sin(angle), np.cos(angle), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])<\/p>\n<p>rotated_vertices = np.dot(np.hstack((vertices, np.ones((vertices.shape[0], 1)))), rotation_matrix.T)<\/p>\n<p>print(rotated_vertices)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5d4c\u5957\u6570\u7ec4\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>\u6027\u80fd\u8003\u8651<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u5d4c\u5957\u6570\u7ec4\u65f6\uff0c\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\u3002numpy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u7ec4\u7684\u52a0\u6cd5\u8fd0\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u7684\u77e2\u91cf\u5316\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e24\u4e2a\u5927\u89c4\u6a21\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = np.random.rand(1000, 1000)<\/p>\n<p>array2 = np.random.rand(1000, 1000)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u8fdb\u884c\u52a0\u6cd5\u8fd0\u7b97<\/strong><\/h2>\n<p>result = array1 + array2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5185\u5b58\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5d4c\u5957\u6570\u7ec4\u53ef\u80fd\u5360\u7528\u5927\u91cf\u5185\u5b58\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002\u9700\u8981\u6ce8\u610f\u5185\u5b58\u7ba1\u7406\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u548c\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u7684\u5185\u5b58\u6620\u5c04\u529f\u80fd\uff0c\u5c06\u6570\u7ec4\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5185\u5b58\u6620\u5c04\u6587\u4ef6<\/strong><\/h2>\n<p>filename = &#39;large_array.dat&#39;<\/p>\n<p>shape = (1000, 1000)<\/p>\n<p>dtype = np.float64<\/p>\n<p>large_array = np.memmap(filename, dtype=dtype, mode=&#39;w+&#39;, shape=shape)<\/p>\n<h2><strong>\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>large_array[:] = np.random.rand(1000, 1000)<\/p>\n<p>print(large_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86Python\u6784\u5efa\u5d4c\u5957\u6570\u7ec4\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u5d4c\u5957\u548cnumpy\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u9002\u7528\u4e8e\u751f\u6210\u89c4\u5219\u7684\u5d4c\u5957\u6570\u7ec4\uff0c\u5faa\u73af\u5d4c\u5957\u9002\u7528\u4e8e\u751f\u6210\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff0cnumpy\u5e93\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u7684\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u5d4c\u5957\u6570\u7ec4\u5728\u6570\u636e\u5206\u6790\u4e0e\u5904\u7406\u3001\u6570\u5b66\u4e0e\u79d1\u5b66\u8ba1\u7b97\u3001\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u5728\u4f7f\u7528\u5d4c\u5957\u6570\u7ec4\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6027\u80fd\u548c\u5185\u5b58\u7ba1\u7406\u95ee\u9898\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u9ad8\u6548\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u6784\u5efa\u5d4c\u5957\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\u3002\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u51fa\u6240\u9700\u7684\u591a\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a2&#215;3\u7684\u6570\u7ec4\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<code>array = [[1, 2, 3], [4, 5, 6]]<\/code>\u3002\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6570\u7ec4\u3002\u53ef\u4ee5\u901a\u8fc7<code>import numpy as np<\/code>\u5bfc\u5165NumPy\uff0c\u5e76\u4f7f\u7528<code>np.array()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u548c\u4fee\u6539\u5d4c\u5957\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u8bbf\u95ee\u5d4c\u5957\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u76f8\u5bf9\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u83b7\u53d6\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6570\u7ec4<code>array = [[1, 2, 3], [4, 5, 6]]<\/code>\uff0c<code>array[0][1]<\/code>\u5c06\u8fd4\u56de2\u3002\u8981\u4fee\u6539\u67d0\u4e2a\u5143\u7d20\uff0c\u53ea\u9700\u4f7f\u7528\u7d22\u5f15\u5e76\u8d4b\u503c\u5373\u53ef\u3002\u4f8b\u5982\uff0c<code>array[1][2] = 10<\/code>\u5c06\u4f1a\u628a\u6570\u7ec4\u4e2d\u76846\u6539\u4e3a10\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u6784\u5efa\u5d4c\u5957\u6570\u7ec4\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u6784\u5efa\u5d4c\u5957\u6570\u7ec4\u65f6\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u4e00\u81f4\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\u548c\u6df7\u6dc6\u3002\u6b64\u5916\uff0c\u4f7f\u7528NumPy\u5e93\u65f6\uff0c\u6ce8\u610f\u6570\u636e\u7c7b\u578b\u7684\u4e00\u81f4\u6027\uff0c\u56e0\u4e3aNumPy\u4f1a\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u63d0\u5347\u4e3a\u7edf\u4e00\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u53ef\u80fd\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u3002\u5c3d\u91cf\u4fdd\u6301\u6570\u7ec4\u7ed3\u6784\u7684\u6e05\u6670\u548c\u6709\u5e8f\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u64cd\u4f5c\u548c\u7ef4\u62a4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6784\u5efa\u5d4c\u5957\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u5d4c\u5957\u3001numpy\u5e93\u7b49\u3002 \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7b80\u6d01\u9ad8\u6548\u5730\u751f [&hellip;]","protected":false},"author":3,"featured_media":1165553,"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\/1165549"}],"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=1165549"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165549\/revisions"}],"predecessor-version":[{"id":1165556,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165549\/revisions\/1165556"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1165553"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1165549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1165549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1165549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}