{"id":982510,"date":"2024-12-27T07:11:24","date_gmt":"2024-12-26T23:11:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/982510.html"},"modified":"2024-12-27T07:11:26","modified_gmt":"2024-12-26T23:11:26","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e7%9f%a9%e9%98%b5%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/982510.html","title":{"rendered":"python\u5982\u4f55\u53d6\u77e9\u9635\u957f\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210951\/555b07f5-a63c-4373-89a5-6dd0b4003f69.webp\" alt=\"python\u5982\u4f55\u53d6\u77e9\u9635\u957f\u5ea6\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u83b7\u53d6\u77e9\u9635\u957f\u5ea6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u3001\u5229\u7528<code>numpy<\/code>\u5e93\u3001\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027<\/strong>\u3002\u5176\u4e2d\uff0c<code>len()<\/code>\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\uff0c<code>numpy<\/code>\u5e93\u548c<code>shape<\/code>\u5c5e\u6027\u53ef\u4ee5\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>len()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>len()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5217\u8868\uff08\u6216\u5176\u4ed6\u7c7b\u4f3c\u7684\u6570\u636e\u7ed3\u6784\uff09\u7684\u957f\u5ea6\u3002\u5728\u5904\u7406\u77e9\u9635\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u3002\u77e9\u9635\u901a\u5e38\u662f\u4ee5\u5d4c\u5957\u5217\u8868\uff08\u5217\u8868\u7684\u5217\u8868\uff09\u5f62\u5f0f\u8868\u793a\u7684\uff0c\u56e0\u6b64<code>len(matrix)<\/code>\u5c06\u8fd4\u56de\u77e9\u9635\u4e2d\u7684\u884c\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>rows = len(matrix)  # \u83b7\u53d6\u884c\u6570<\/p>\n<p>print(&quot;Number of rows:&quot;, rows)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5229\u7528<code>numpy<\/code>\u5e93<\/p>\n<\/p>\n<p><p><code>numpy<\/code>\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5f3a\u5927\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u7684\u529f\u80fd\u3002<code>numpy<\/code>\u4e2d\u7684<code>array<\/code>\u5bf9\u8c61\u6709\u4e00\u4e2a<code>shape<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p>rows, cols = matrix.shape  # \u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570<\/p>\n<p>print(&quot;Number of rows:&quot;, rows)<\/p>\n<p>print(&quot;Number of columns:&quot;, cols)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>numpy<\/code>\u5e93\u4e0d\u4ec5\u80fd\u66f4\u65b9\u4fbf\u5730\u83b7\u53d6\u77e9\u9635\u7684\u5c3a\u5bf8\uff0c\u8fd8\u80fd\u6267\u884c\u5404\u79cd\u77e9\u9635\u8fd0\u7b97\uff0c\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\uff0c\u662f\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027<\/p>\n<\/p>\n<p><p>\u5f53\u77e9\u9635\u662f\u4f7f\u7528<code>numpy<\/code>\u5e93\u521b\u5efa\u7684<code>array<\/code>\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u77e9\u9635\u7684\u5f62\u72b6\u3002<code>shape<\/code>\u5c5e\u6027\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u77e9\u9635\u7684\u884c\u6570\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u5217\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u9ad8\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u83b7\u53d6\u77e9\u9635\u5c3a\u5bf8\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<h2><strong>\u4f7f\u7528 shape \u5c5e\u6027\u83b7\u53d6\u884c\u6570\u548c\u5217\u6570<\/strong><\/h2>\n<p>shape = matrix.shape<\/p>\n<p>print(&quot;Matrix shape:&quot;, shape)<\/p>\n<p>print(&quot;Number of rows:&quot;, shape[0])<\/p>\n<p>print(&quot;Number of columns:&quot;, shape[1])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u8fd9\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u5404\u6709\u4f18\u52a3\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u77e9\u9635\u64cd\u4f5c\uff0c\u76f4\u63a5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u53ef\u80fd\u662f\u6700\u5feb\u6377\u7684\u9009\u62e9\u3002\u800c\u5728\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u77e9\u9635\u8fd0\u7b97\u65f6\uff0c\u5229\u7528<code>numpy<\/code>\u5e93\u53ca\u5176<code>shape<\/code>\u5c5e\u6027\u5219\u663e\u5f97\u66f4\u52a0\u4e13\u4e1a\u548c\u9ad8\u6548\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u6709\u52a9\u4e8e\u5728Python\u4e2d\u66f4\u7075\u6d3b\u5730\u5904\u7406\u77e9\u9635\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e00\u4e2a\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002\u901a\u8fc7\u8c03\u7528\u77e9\u9635\u7684.shape\u5c5e\u6027\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u884c\u6570\u548c\u5217\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u77e9\u9635<code>A<\/code>\uff0c\u4f7f\u7528<code>A.shape[0]<\/code>\u83b7\u53d6\u884c\u6570\uff0c<code>A.shape[1]<\/code>\u83b7\u53d6\u5217\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4efb\u4f55\u7ef4\u5ea6\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u65f6\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u77e9\u9635\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u5224\u65ad\u4e00\u4e2a\u77e9\u9635\u662f\u5426\u4e3a\u7a7a\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u5176\u5f62\u72b6\u5c5e\u6027\u6765\u5b9e\u73b0\u3002\u5982\u679c\u4e00\u4e2a\u77e9\u9635\u7684\u5f62\u72b6\u4e3a(0, 0)\uff0c\u5219\u8868\u793a\u8be5\u77e9\u9635\u4e3a\u7a7a\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<code>if matrix.shape[0] == 0 and matrix.shape[1] == 0:<\/code>\u3002\u8fd9\u79cd\u5224\u65ad\u65b9\u5f0f\u9002\u7528\u4e8eNumPy\u6570\u7ec4\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u83b7\u53d6\u77e9\u9635\u7684\u603b\u5143\u7d20\u4e2a\u6570\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u77e9\u9635\u7684\u603b\u5143\u7d20\u4e2a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u4e2d\u7684.size\u5c5e\u6027\u3002\u8fd9\u4e2a\u5c5e\u6027\u8fd4\u56de\u77e9\u9635\u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u91cf\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u77e9\u9635<code>A<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>A.size<\/code>\u6765\u83b7\u53d6\u603b\u5143\u7d20\u4e2a\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\u4e5f\u540c\u6837\u9002\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u83b7\u53d6\u77e9\u9635\u957f\u5ea6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528len()\u51fd\u6570\u3001\u5229\u7528numpy\u5e93\u3001\u4f7f\u7528shape\u5c5e\u6027\u3002\u5176\u4e2d\uff0clen [&hellip;]","protected":false},"author":3,"featured_media":982516,"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\/982510"}],"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=982510"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982510\/revisions"}],"predecessor-version":[{"id":982517,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982510\/revisions\/982517"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/982516"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=982510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=982510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=982510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}