{"id":1029946,"date":"2024-12-31T11:12:43","date_gmt":"2024-12-31T03:12:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1029946.html"},"modified":"2024-12-31T11:12:45","modified_gmt":"2024-12-31T03:12:45","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e4%ba%8c%e7%bb%b4%e7%a9%ba%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1029946.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u4e8c\u7ef4\u7a7a\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/2bdb6869-fa71-46d6-9ad1-e465464c00de.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u4e8c\u7ef4\u7a7a\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>Python\u5b9a\u4e49\u4e8c\u7ef4\u7a7a\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u3001NumPy\u5e93\u3001List comprehensions\u7b49\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u5b9a\u4e49\u4e8c\u7ef4\u7a7a\u6570\u7ec4<\/strong><\/li>\n<li><strong>\u4f7f\u7528List comprehensions\u521b\u5efa\u4e8c\u7ef4\u7a7a\u6570\u7ec4<\/strong><\/li>\n<li><strong>\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u4e8c\u7ef4\u7a7a\u6570\u7ec4<\/strong><\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u6bcf\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u5b9a\u4e49\u4e8c\u7ef4\u7a7a\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5217\u8868\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u8868\u793a\u4e8c\u7ef4\u6570\u7ec4\u3002\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u7a7a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u4e8c\u7ef4\u7a7a\u6570\u7ec4<\/p>\n<p>rows, cols = 3, 3<\/p>\n<p>array = [[None for _ in range(cols)] for _ in range(rows)]<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u7a7a\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u521d\u59cb\u5316\u4e3a<code>None<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u884c\u6570\u548c\u5217\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528List comprehensions\u521b\u5efa\u4e8c\u7ef4\u7a7a\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>List comprehensions\u662fPython\u4e2d\u521b\u5efa\u5217\u8868\u7684\u4e00\u79cd\u7b80\u6d01\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5404\u79cd\u89c4\u5219\u7684\u5217\u8868\uff0c\u5305\u62ec\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a4x5\u7684\u4e8c\u7ef4\u7a7a\u6570\u7ec4<\/p>\n<p>rows, cols = 4, 5<\/p>\n<p>array = [[0] * cols for _ in range(rows)]<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a4&#215;5\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u521d\u59cb\u5316\u4e3a<code>0<\/code>\u3002\u540c\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u884c\u6570\u548c\u5217\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u4e8c\u7ef4\u7a7a\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u63d0\u4f9b\u4e86\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u7684\u9ad8\u6548\u63a5\u53e3\u3002\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x4\u7684\u4e8c\u7ef4\u7a7a\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.empty((3, 4))<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>np.empty<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;4\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>np.empty<\/code>\u521b\u5efa\u7684\u6570\u7ec4\u5143\u7d20\u662f\u672a\u521d\u59cb\u5316\u7684\uff0c\u56e0\u6b64\u53ef\u80fd\u5305\u542b\u4efb\u610f\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u7279\u5b9a\u521d\u59cb\u5316\u503c\u7684\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>np.empty<\/code>\u521b\u5efa\u672a\u521d\u59cb\u5316\u7684\u6570\u7ec4\u5916\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u51fd\u6570\u6765\u521b\u5efa\u521d\u59cb\u5316\u4e3a\u7279\u5b9a\u503c\u7684\u6570\u7ec4\uff0c\u4f8b\u5982<code>np.zeros<\/code>\u548c<code>np.ones<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x4\u5e76\u521d\u59cb\u5316\u4e3a0\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_zeros = np.zeros((3, 4))<\/p>\n<p>print(array_zeros)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x4\u5e76\u521d\u59cb\u5316\u4e3a1\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_ones = np.ones((3, 4))<\/p>\n<p>print(array_ones)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u793a\u4f8b\u4e2d\uff0c<code>np.zeros<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;4\u5e76\u521d\u59cb\u5316\u4e3a0\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u800c<code>np.ones<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;4\u5e76\u521d\u59cb\u5316\u4e3a1\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u8fd9\u4e9b\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u7528\u4e8e\u9700\u8981\u7279\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u7279\u5b9a\u6570\u636e\u7c7b\u578b\u7684\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u6570\u7ec4\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u9700\u8981\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x4\u5e76\u521d\u59cb\u5316\u4e3a0\u7684\u6574\u6570\u7c7b\u578b\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_int = np.zeros((3, 4), dtype=int)<\/p>\n<p>print(array_int)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x4\u5e76\u521d\u59cb\u5316\u4e3a0\u7684\u6d6e\u70b9\u7c7b\u578b\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_float = np.zeros((3, 4), dtype=float)<\/p>\n<p>print(array_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>dtype<\/code>\u53c2\u6570\u6307\u5b9a\u4e86\u6570\u7ec4\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5206\u522b\u521b\u5efa\u4e86\u6574\u6570\u7c7b\u578b\u548c\u6d6e\u70b9\u7c7b\u578b\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5217\u8868\u548cNumPy\u7ed3\u5408\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5df2\u6709\u7684\u5217\u8868\u6570\u636e\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>np.array<\/code>\u51fd\u6570\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>array = np.array(nested_list)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3a\u4e86NumPy\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u4ecePython\u7684\u539f\u751f\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001NumPy\u7684\u9ad8\u7ea7\u6570\u7ec4\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>NumPy\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u7684\u529f\u80fd\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u6570\u7ec4\u64cd\u4f5c\uff0c\u4f8b\u5982\u6570\u7ec4\u5207\u7247\u3001\u5f62\u72b6\u53d8\u6362\u548c\u77e9\u9635\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u7ec4\u5207\u7247<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x4\u7684\u6570\u7ec4\u5e76\u8fdb\u884c\u5207\u7247\u64cd\u4f5c<\/strong><\/h2>\n<p>array = np.arange(12).reshape(3, 4)<\/p>\n<p>print(&quot;Original array:&quot;)<\/p>\n<p>print(array)<\/p>\n<h2><strong>\u63d0\u53d6\u5b50\u6570\u7ec4<\/strong><\/h2>\n<p>sub_array = array[:2, 1:3]<\/p>\n<p>print(&quot;Sub-array:&quot;)<\/p>\n<p>print(sub_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>np.arange<\/code>\u751f\u6210\u4e86\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u5e76\u901a\u8fc7<code>reshape<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a3&#215;4\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u63d0\u53d6\u4e86\u4e00\u4e2a\u5b50\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>\u5f62\u72b6\u53d8\u6362<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x4\u7684\u6570\u7ec4\u5e76\u8fdb\u884c\u5f62\u72b6\u53d8\u6362<\/strong><\/h2>\n<p>array = np.arange(12).reshape(3, 4)<\/p>\n<p>print(&quot;Original array:&quot;)<\/p>\n<p>print(array)<\/p>\n<h2><strong>\u5c06\u6570\u7ec4\u53d8\u6362\u4e3a2x6<\/strong><\/h2>\n<p>reshaped_array = array.reshape(2, 6)<\/p>\n<p>print(&quot;Reshaped array:&quot;)<\/p>\n<p>print(reshaped_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a3&#215;4\u7684\u6570\u7ec4\u53d8\u6362\u4e3a2&#215;6\u7684\u6570\u7ec4\u3002NumPy\u7684<code>reshape<\/code>\u51fd\u6570\u4f7f\u5f97\u6570\u7ec4\u5f62\u72b6\u53d8\u6362\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u77e9\u9635\u8fd0\u7b97<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u5e76\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>array1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>array2 = np.array([[5, 6], [7, 8]])<\/p>\n<p>result = np.dot(array1, array2)<\/p>\n<p>print(&quot;Matrix multiplication result:&quot;)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>np.dot<\/code>\u51fd\u6570\u8fdb\u884c\u4e86\u4e24\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u77e9\u9635\u4e58\u6cd5\u3002\u8fd9\u5c55\u793a\u4e86NumPy\u5728\u8fdb\u884c\u590d\u6742\u6570\u5b66\u8fd0\u7b97\u65f6\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u51e0\u79cd\u5728Python\u4e2d\u5b9a\u4e49\u4e8c\u7ef4\u7a7a\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548cNumPy\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p><strong>\u5d4c\u5957\u5217\u8868\u548c\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u4e8e\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u64cd\u4f5c\u548c\u521d\u5b66\u8005\u4f7f\u7528\u3002\u800c<strong>NumPy\u5e93<\/strong>\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u548c\u591a\u529f\u80fd\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u548c\u8fdb\u884c\u590d\u6742\u6570\u5b66\u8fd0\u7b97\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\uff0cNumPy\u65e0\u7591\u662f\u9996\u9009\u5de5\u5177\u3002\u800c\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5b58\u50a8\u548c\u64cd\u4f5c\uff0c\u5d4c\u5957\u5217\u8868\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u5b8c\u5168\u80fd\u591f\u80dc\u4efb\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\u7a7a\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\uff08list\uff09\u6765\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u5927\u5c0f\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>rows<\/code> \u548c <code>cols<\/code> \u6765\u5b9a\u4e49\u884c\u548c\u5217\u7684\u6570\u91cf\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">rows, cols = 3, 4  # 3\u884c4\u5217\narray_2d = [[0 for _ in range(cols)] for _ in range(rows)]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u4f1a\u521b\u5efa\u4e00\u4e2a3\u884c4\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u68c0\u67e5\u4e8c\u7ef4\u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u5176\u884c\u6570\u548c\u5217\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6570\u7ec4\u540d\u4e3a<code>array_2d<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5224\u65ad\uff1a  <\/p>\n<pre><code class=\"language-python\">if len(array_2d) == 0 or len(array_2d[0]) == 0:\n    print(&quot;\u4e8c\u7ef4\u6570\u7ec4\u4e3a\u7a7a&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u68c0\u67e5\u6570\u7ec4\u662f\u5426\u6ca1\u6709\u884c\u6216\u5217\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5411\u4e8c\u7ef4\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\uff1f<\/strong><br \/>\u5411\u4e8c\u7ef4\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u8981\u5728<code>array_2d<\/code>\u7684\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u6dfb\u52a0\u4e00\u4e2a\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">array_2d[0][1] = 5  # \u5c065\u6dfb\u52a0\u5230\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u6dfb\u52a0\u503c\u4e4b\u524d\uff0c\u76ee\u6807\u4f4d\u7f6e\u5df2\u7ecf\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u7d22\u5f15\u9519\u8bef\u3002\u82e5\u8981\u52a8\u6001\u6dfb\u52a0\u65b0\u884c\u6216\u65b0\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u6216<code>insert()<\/code>\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9a\u4e49\u4e8c\u7ef4\u7a7a\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u3001NumPy\u5e93\u3001List comprehensions\u7b49\u591a [&hellip;]","protected":false},"author":3,"featured_media":1029953,"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\/1029946"}],"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=1029946"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029946\/revisions"}],"predecessor-version":[{"id":1029956,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029946\/revisions\/1029956"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1029953"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1029946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1029946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1029946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}