{"id":1100390,"date":"2025-01-08T15:39:30","date_gmt":"2025-01-08T07:39:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100390.html"},"modified":"2025-01-08T15:39:33","modified_gmt":"2025-01-08T07:39:33","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e7%9f%a9%e9%98%b5%e8%a1%8c%e5%92%8c%e5%88%97%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100390.html","title":{"rendered":"python\u5982\u4f55\u6c42\u77e9\u9635\u884c\u548c\u5217\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063641\/f69f2ab4-a81f-49a9-a078-bec04b39f3df.webp\" alt=\"python\u5982\u4f55\u6c42\u77e9\u9635\u884c\u548c\u5217\u6570\" \/><\/p>\n<p><p> <strong>Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u3001\u4ee5\u53ca\u5185\u7f6e\u7684\u5217\u8868\u64cd\u4f5c\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570<\/strong>\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7NumPy\u5e93\u3002NumPy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\uff08ndarray\uff09\uff0c\u64cd\u4f5c\u65b9\u4fbf\u4e14\u6027\u80fd\u4f18\u8d8a\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\uff1a\u901a\u8fc7NumPy\u5e93\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u5e93\u65f6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165NumPy\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>shape<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\uff0c<code>shape<\/code>\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u77e9\u9635\u7ef4\u5ea6\u7684\u5143\u7ec4\uff0c\u7b2c\u4e00\u4e2a\u503c\u662f\u884c\u6570\uff0c\u7b2c\u4e8c\u4e2a\u503c\u662f\u5217\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u9ad8\u6548\uff0c\u800c\u4e14\u4ee3\u7801\u7b80\u6d01\u6613\u8bfb\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u6765\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7NumPy\u5e93\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570<\/h3>\n<\/p>\n<p><p><strong>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93<\/strong>\uff0c\u63d0\u4f9b\u4e86\u652f\u6301\u5927\u89c4\u6a21\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u7684\u529f\u80fd\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5NumPy<\/strong>\uff1a\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5NumPy\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165NumPy\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u77e9\u9635<\/strong>\uff1a\u4f7f\u7528NumPy\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff0c\u6216\u5c06\u73b0\u6709\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570<\/strong>\uff1a\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows, cols = matrix.shape<\/p>\n<p>print(&quot;\u884c\u6570:&quot;, rows)<\/p>\n<p>print(&quot;\u5217\u6570:&quot;, cols)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u901a\u8fc7Pandas\u5e93\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570<\/h3>\n<\/p>\n<p><p><strong>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93<\/strong>\uff0c\u7279\u522b\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pandas\u7684DataFrame\u5bf9\u8c61\u6765\u5904\u7406\u548c\u64cd\u4f5c\u6570\u636e\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Pandas<\/strong>\uff1a\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5Pandas\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165Pandas\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaDataFrame<\/strong>\uff1a\u4f7f\u7528Pandas\u521b\u5efa\u4e00\u4e2aDataFrame\uff0c\u6216\u5c06\u73b0\u6709\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;A&#39;: [1, 4, 7], &#39;B&#39;: [2, 5, 8], &#39;C&#39;: [3, 6, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6DataFrame\u7684\u884c\u6570\u548c\u5217\u6570<\/strong>\uff1a\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u83b7\u53d6DataFrame\u7684\u7ef4\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows, cols = df.shape<\/p>\n<p>print(&quot;\u884c\u6570:&quot;, rows)<\/p>\n<p>print(&quot;\u5217\u6570:&quot;, cols)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u5185\u7f6e\u5217\u8868\u64cd\u4f5c\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570<\/h3>\n<\/p>\n<p><p><strong>\u5373\u4f7f\u4e0d\u4f7f\u7528\u4efb\u4f55\u5916\u90e8\u5e93<\/strong>\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684\u5217\u8868\u64cd\u4f5c\u6765\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u77e9\u9635<\/strong>\uff1a\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u884c\u6570<\/strong>\uff1a\u4f7f\u7528<code>len<\/code>\u51fd\u6570\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = len(matrix)<\/p>\n<p>print(&quot;\u884c\u6570:&quot;, rows)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u5217\u6570<\/strong>\uff1a\u83b7\u53d6\u77e9\u9635\u7b2c\u4e00\u884c\u7684\u957f\u5ea6\u6765\u786e\u5b9a\u5217\u6570\uff08\u5047\u8bbe\u77e9\u9635\u662f\u89c4\u6574\u7684\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cols = len(matrix[0])<\/p>\n<p>print(&quot;\u5217\u6570:&quot;, cols)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u66f4\u591a\u77e9\u9635\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p><strong>NumPy\u4e0d\u4ec5\u53ef\u4ee5\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570<\/strong>\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong>\uff1a\u5c06\u77e9\u9635\u7684\u884c\u548c\u5217\u4e92\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transposed_matrix = np.transpose(matrix)<\/p>\n<p>print(&quot;\u8f6c\u7f6e\u77e9\u9635:\\n&quot;, transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u76f8\u52a0<\/strong>\uff1a\u5c06\u4e24\u4e2a\u77e9\u9635\u5bf9\u5e94\u5143\u7d20\u76f8\u52a0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])<\/p>\n<p>sum_matrix = np.add(matrix, matrix2)<\/p>\n<p>print(&quot;\u77e9\u9635\u76f8\u52a0\u7ed3\u679c:\\n&quot;, sum_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u76f8\u4e58<\/strong>\uff1a\u77e9\u9635\u4e58\u6cd5\uff08\u70b9\u79ef\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">product_matrix = np.dot(matrix, matrix2)<\/p>\n<p>print(&quot;\u77e9\u9635\u76f8\u4e58\u7ed3\u679c:\\n&quot;, product_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8ba1\u7b97\u884c\u548c<\/strong>\uff1a\u8ba1\u7b97\u6bcf\u4e00\u884c\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">row_sums = np.sum(matrix, axis=1)<\/p>\n<p>print(&quot;\u6bcf\u4e00\u884c\u7684\u548c:&quot;, row_sums)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8ba1\u7b97\u5217\u548c<\/strong>\uff1a\u8ba1\u7b97\u6bcf\u4e00\u5217\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">col_sums = np.sum(matrix, axis=0)<\/p>\n<p>print(&quot;\u6bcf\u4e00\u5217\u7684\u548c:&quot;, col_sums)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5904\u7406\u548c\u64cd\u4f5c\u5927\u91cf\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u9645\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u6765\u5904\u7406\u6570\u636e\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u9884\u5904\u7406<\/strong>\uff1a\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u6570\u636e\u9884\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u73af\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u6216Pandas\u6765\u8bfb\u53d6\u6570\u636e\u96c6\uff0c\u83b7\u53d6\u6570\u636e\u7684\u7ef4\u5ea6\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>rows, cols = data.shape<\/p>\n<p>print(&quot;\u6570\u636e\u96c6\u7684\u884c\u6570:&quot;, rows)<\/p>\n<p>print(&quot;\u6570\u636e\u96c6\u7684\u5217\u6570:&quot;, cols)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong>\uff1a\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\uff0c\u56fe\u50cf\u901a\u5e38\u8868\u793a\u4e3a\u77e9\u9635\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u6765\u5904\u7406\u56fe\u50cf\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u4e00\u5f20\u7070\u5ea6\u56fe\u50cf\u5e76\u83b7\u53d6\u5176\u7ef4\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from PIL import Image<\/p>\n<p>image = Image.open(&#39;image.jpg&#39;).convert(&#39;L&#39;)<\/p>\n<p>image_matrix = np.array(image)<\/p>\n<p>rows, cols = image_matrix.shape<\/p>\n<p>print(&quot;\u56fe\u50cf\u7684\u9ad8\u5ea6:&quot;, rows)<\/p>\n<p>print(&quot;\u56fe\u50cf\u7684\u5bbd\u5ea6:&quot;, cols)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u8ba1\u7b97<\/strong>\uff1a\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u5e94\u7528\u4e2d\uff0c\u77e9\u9635\u8ba1\u7b97\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u4f8b\u5982\uff0c\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>A = np.array([[3, 1], [1, 2]])<\/p>\n<p>B = np.array([9, 8])<\/p>\n<p>solution = np.linalg.solve(A, B)<\/p>\n<p>print(&quot;\u7ebf\u6027\u65b9\u7a0b\u7ec4\u7684\u89e3:&quot;, solution)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u6765\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002<strong>NumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u548c\u7b80\u6d01\u7684\u89e3\u51b3\u65b9\u6848<\/strong>\uff0c\u662f\u5904\u7406\u77e9\u9635\u7684\u9996\u9009\u5de5\u5177\u3002Pandas\u5e93\u540c\u6837\u5f3a\u5927\uff0c\u7279\u522b\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u800c\u5185\u7f6e\u7684\u5217\u8868\u64cd\u4f5c\u5c3d\u7ba1\u4e0d\u5982\u524d\u4e24\u8005\u9ad8\u6548\uff0c\u4f46\u5728\u7b80\u5355\u7684\u573a\u666f\u4e0b\u4e5f\u80fd\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u80fd\u591f\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u7684\u77e9\u9635\u64cd\u4f5c\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5904\u7406\u77e9\u9635\u3002\u901a\u8fc7<code>shape<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u77e9\u9635<code>A<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>A.shape<\/code>\u6765\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u884c\u6570\u548c\u5217\u6570\u7684\u5143\u7ec4\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nA = np.array([[1, 2, 3], [4, 5, 6]])\nrows, cols = A.shape\nprint(&quot;\u884c\u6570:&quot;, rows)\nprint(&quot;\u5217\u6570:&quot;, cols)\n<\/code><\/pre>\n<p><strong>\u5728\u6ca1\u6709\u4f7f\u7528NumPy\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u83b7\u53d6\u5217\u8868\u7684\u884c\u6570\u548c\u5217\u6570\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u6ca1\u6709\u4f7f\u7528NumPy\u5e93\uff0c\u800c\u662f\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u8868\u793a\u77e9\u9635\uff0c\u83b7\u53d6\u884c\u6570\u548c\u5217\u6570\u540c\u6837\u7b80\u5355\u3002\u884c\u6570\u53ef\u4ee5\u901a\u8fc7<code>len()<\/code>\u51fd\u6570\u83b7\u53d6\uff0c\u800c\u5217\u6570\u5219\u901a\u8fc7\u53d6\u7b2c\u4e00\u884c\u7684\u957f\u5ea6\u6765\u786e\u5b9a\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6]]\nrows = len(matrix)\ncols = len(matrix[0]) if rows &gt; 0 else 0\nprint(&quot;\u884c\u6570:&quot;, rows)\nprint(&quot;\u5217\u6570:&quot;, cols)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u77e9\u9635\u7684\u884c\u5217\u6570\u5bf9\u540e\u7eed\u64cd\u4f5c\u6709\u54ea\u4e9b\u5f71\u54cd\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u65f6\uff0c\u884c\u6570\u548c\u5217\u6570\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u51b3\u5b9a\u4e86\u8fd0\u7b97\u7684\u53ef\u884c\u6027\u3002\u4f8b\u5982\uff0c\u5728\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\u65f6\uff0c\u7b2c\u4e00\u4e2a\u77e9\u9635\u7684\u5217\u6570\u5fc5\u987b\u7b49\u4e8e\u7b2c\u4e8c\u4e2a\u77e9\u9635\u7684\u884c\u6570\u3002\u4e86\u89e3\u884c\u5217\u6570\u6709\u52a9\u4e8e\u907f\u514d\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u5e76\u786e\u4fdd\u7b97\u6cd5\u7684\u6b63\u786e\u6027\u4e0e\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u3001\u4ee5\u53ca\u5185\u7f6e\u7684\u5217\u8868\u64cd\u4f5c\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\uff0c\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1100403,"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\/1100390"}],"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=1100390"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100390\/revisions"}],"predecessor-version":[{"id":1100405,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100390\/revisions\/1100405"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100403"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}