{"id":1110758,"date":"2025-01-08T17:22:44","date_gmt":"2025-01-08T09:22:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110758.html"},"modified":"2025-01-08T17:22:59","modified_gmt":"2025-01-08T09:22:59","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%ba%8c%e4%bd%8d%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1110758.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210\u4e8c\u4f4d\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073455\/22dda68e-2b9c-4525-8dcd-f52761106c38.webp\" alt=\"python\u5982\u4f55\u751f\u6210\u4e8c\u4f4d\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u5219\u662f\u6700\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u521d\u5b66\u8005\u7406\u89e3\u548c\u4f7f\u7528\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u6765\u8bf4\u660e\u5982\u4f55\u751f\u6210\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u751f\u6210\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u3002\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u4e5f\u662f\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a2x3\u7684\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array = [[1, 2, 3], [4, 5, 6]]<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u6790\uff1a<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u624b\u52a8\u521b\u5efa\u4e86\u4e00\u4e2a2\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u6bcf\u4e00\u884c\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u8fd9\u4e9b\u5217\u8868\u53c8\u7ec4\u5408\u6210\u4e00\u4e2a\u5927\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u4f46\u5bf9\u4e8e\u9700\u8981\u751f\u6210\u5927\u578b\u6570\u7ec4\u6216\u9700\u8981\u52a8\u6001\u751f\u6210\u6570\u7ec4\u7684\u60c5\u51b5\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5e76\u4e0d\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u4e8c\u7ef4\u6570\u7ec4\u3002\u5b83\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u901f\u5ea6\u4e5f\u6bd4\u8f83\u5feb\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a3x4\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a0<\/p>\n<p>rows, cols = 3, 4<\/p>\n<p>array = [[0 for _ in range(cols)] for _ in range(rows)]<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u6790\uff1a<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5d4c\u5957\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u5916\u5c42\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u6bcf\u4e00\u884c\uff0c\u5185\u5c42\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u6bcf\u4e00\u884c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u521d\u59cb\u5316\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u6570\u7ec4\u7684\u65b9\u6cd5\u3002\u4f7f\u7528NumPy\u751f\u6210\u4e8c\u7ef4\u6570\u7ec4\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u529f\u80fd\u5f3a\u5927\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u6570\u7ec4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a2x3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a0<\/strong><\/h2>\n<p>array = np.zeros((2, 3))<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u6790\uff1a<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86NumPy\u7684<code>zeros<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a2\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5c06\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a0\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u6570\u7ec4\u7684\u51fd\u6570\uff0c\u5982<code>zeros<\/code>\u3001<code>ones<\/code>\u3001<code>full<\/code>\u3001<code>arange<\/code>\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528NumPy\u751f\u6210\u968f\u673a\u6570\u7ec4\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a3x3\u7684\u968f\u673a\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array = np.random.rand(3, 3)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u6790\uff1a<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86NumPy\u7684<code>random.rand<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a3\u884c3\u5217\u7684\u968f\u673a\u4e8c\u7ef4\u6570\u7ec4\u3002NumPy\u7684\u968f\u673a\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u751f\u6210\u5404\u79cd\u968f\u673a\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u590d\u6742\u4e8c\u7ef4\u6570\u7ec4<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u7b80\u5355\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u590d\u6742\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u5b83\u7684\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u7684\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a3x3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u5b83\u7684\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u7684\u548c<\/p>\n<p>rows, cols = 3, 3<\/p>\n<p>array = [[i + j for j in range(cols)] for i in range(rows)]<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u6790\uff1a<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5d4c\u5957\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a3\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u5b83\u7684\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u7684\u548c\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u751f\u6210\u5404\u79cd\u590d\u6742\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528NumPy\u751f\u6210\u7279\u5b9a\u5f62\u72b6\u7684\u6570\u7ec4<\/h2>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u7279\u5b9a\u5f62\u72b6\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u5982\u751f\u6210\u5355\u4f4d\u77e9\u9635\u3001\u5bf9\u89d2\u77e9\u9635\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a3x3\u7684\u5355\u4f4d\u77e9\u9635<\/p>\n<p>identity_matrix = np.eye(3)<\/p>\n<p>print(identity_matrix)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3\u7684\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix = np.diag([1, 2, 3])<\/p>\n<p>print(diagonal_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u6790\uff1a<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86NumPy\u7684<code>eye<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a3\u884c3\u5217\u7684\u5355\u4f4d\u77e9\u9635\uff0c\u4f7f\u7528<code>diag<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a3\u884c3\u5217\u7684\u5bf9\u89d2\u77e9\u9635\u3002NumPy\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u7279\u5b9a\u5f62\u72b6\u6570\u7ec4\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528NumPy\u751f\u6210\u7b49\u5dee\u6570\u5217\u548c\u7f51\u683c\u6570\u7ec4<\/h2>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u751f\u6210\u7b49\u5dee\u6570\u5217\u548c\u7f51\u683c\u6570\u7ec4\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u4e00\u7ef4\u548c\u4e8c\u7ef4\u7684\u7b49\u5dee\u6570\u5217\u548c\u7f51\u683c\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a\u4e00\u7ef4\u7b49\u5dee\u6570\u5217<\/p>\n<p>arange_array = np.arange(0, 10, 2)<\/p>\n<p>print(arange_array)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u6570\u7ec4<\/strong><\/h2>\n<p>x = np.linspace(0, 2, 3)<\/p>\n<p>y = np.linspace(0, 2, 3)<\/p>\n<p>xx, yy = np.meshgrid(x, y)<\/p>\n<p>print(xx)<\/p>\n<p>print(yy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u6790\uff1a<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86NumPy\u7684<code>arange<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a\u4e00\u7ef4\u7b49\u5dee\u6570\u5217\uff0c\u4f7f\u7528<code>linspace<\/code>\u548c<code>meshgrid<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e8c\u7ef4\u7684\u7f51\u683c\u6570\u7ec4\u3002NumPy\u7684\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u7b49\u5dee\u6570\u5217\u548c\u7f51\u683c\u6570\u7ec4\uff0c\u9002\u5408\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u7ec4\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p>NumPy\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u8fd0\u7b97\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u7ec4\u7684\u52a0\u51cf\u4e58\u9664\u3001\u77e9\u9635\u8fd0\u7b97\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e24\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>a = np.array([[1, 2], [3, 4]])<\/p>\n<p>b = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u6570\u7ec4\u52a0\u6cd5<\/strong><\/h2>\n<p>sum_array = np.add(a, b)<\/p>\n<p>print(sum_array)<\/p>\n<h2><strong>\u6570\u7ec4\u4e58\u6cd5<\/strong><\/h2>\n<p>product_array = np.multiply(a, b)<\/p>\n<p>print(product_array)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>matrix_product = np.dot(a, b)<\/p>\n<p>print(matrix_product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u6790\uff1a<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u751f\u6210\u4e86\u4e24\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u8fdb\u884c\u4e86\u6570\u7ec4\u7684\u52a0\u6cd5\u3001\u4e58\u6cd5\u548c\u77e9\u9635\u4e58\u6cd5\u3002NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u8fd0\u7b97\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u6570\u7ec4\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u751f\u6210\u4e8c\u7ef4\u6570\u7ec4\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001NumPy\u5e93\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u5408\u4e0d\u540c\u7684\u573a\u666f\u3002\u5bf9\u4e8e\u9700\u8981\u9ad8\u6548\u548c\u590d\u6742\u6570\u7ec4\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528NumPy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u751f\u6210\u548c\u64cd\u4f5c\u529f\u80fd\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bfb\u8005\u80fd\u591f\u638c\u63e1Python\u751f\u6210\u4e8c\u7ef4\u6570\u7ec4\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u5e76\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u5927\u5c0f\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u5927\u5c0f\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u7684\u5217\u8868\u6765\u5b9e\u73b0\uff0c\u6bd4\u5982\u4f7f\u7528 <code>rows<\/code> \u548c <code>columns<\/code> \u7684\u53d8\u91cf\u6765\u5b9a\u4e49\u6570\u7ec4\u7684\u884c\u6570\u548c\u5217\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">rows, columns = 3, 4\narray_2d = [[0 for _ in range(columns)] for _ in range(rows)]\nprint(array_2d)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a3\u884c4\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u586b\u5145\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u586b\u5145\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6765\u904d\u5386\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\uff0c\u4ece\u800c\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">rows, columns = 3, 4\narray_2d = [[0 for _ in range(columns)] for _ in range(rows)]\nfor i in range(rows):\n    for j in range(columns):\n        array_2d[i][j] = i * j  # \u4f8b\u5982\uff0c\u8d4b\u503c\u4e3a\u884c\u7d22\u5f15\u4e0e\u5217\u7d22\u5f15\u7684\u4e58\u79ef\nprint(array_2d)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u586b\u5145\u6570\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u4e3a\u5176\u884c\u7d22\u5f15\u4e0e\u5217\u7d22\u5f15\u7684\u4e58\u79ef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528NumPy\u5e93\u751f\u6210\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u3002\u4f7f\u7528NumPy\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>numpy.array()<\/code> \u6216 <code>numpy.zeros()<\/code> \u6765\u521b\u5efa\u6570\u7ec4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u4f7f\u7528numpy.array\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\narray_2d = np.array([[1, 2, 3], [4, 5, 6]])\nprint(array_2d)\n\n# \u4f7f\u7528numpy.zeros\u521b\u5efa\u4e00\u4e2a3x4\u7684\u4e8c\u7ef4\u6570\u7ec4\narray_zeros = np.zeros((3, 4))\nprint(array_zeros)\n<\/code><\/pre>\n<p>\u4f7f\u7528NumPy\u4e0d\u4ec5\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u586b\u5145\u6570\u7ec4\uff0c\u8fd8\u80fd\u5229\u7528\u5b83\u63d0\u4f9b\u7684\u4e30\u5bcc\u51fd\u6570\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u64cd\u4f5c\u548c\u6570\u7ec4\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528NumPy [&hellip;]","protected":false},"author":3,"featured_media":1110786,"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\/1110758"}],"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=1110758"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110758\/revisions"}],"predecessor-version":[{"id":1110790,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110758\/revisions\/1110790"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110786"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}