{"id":1120965,"date":"2025-01-08T19:03:57","date_gmt":"2025-01-08T11:03:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120965.html"},"modified":"2025-01-08T19:03:59","modified_gmt":"2025-01-08T11:03:59","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e7%9f%a9%e9%98%b5%e5%9c%a8%e6%9f%90%e4%bd%8d%e7%bd%ae%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120965.html","title":{"rendered":"python\u5982\u4f55\u53d6\u77e9\u9635\u5728\u67d0\u4f4d\u7f6e\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083416\/d91d7ef3-a9d9-4729-9e73-93c2d0bb94d2.webp\" alt=\"python\u5982\u4f55\u53d6\u77e9\u9635\u5728\u67d0\u4f4d\u7f6e\u7684\u503c\" \/><\/p>\n<p><p> <strong>Python\u53d6\u77e9\u9635\u5728\u67d0\u4f4d\u7f6e\u7684\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7d22\u5f15\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u4f8b\u5b50\u6765\u5e2e\u52a9\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u64cd\u4f5c\u77e9\u9635\u53ef\u4ee5\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u6709\u4f7f\u7528\u7d22\u5f15\u3001\u4f7f\u7528NumPy\u5e93\u4ee5\u53ca\u4f7f\u7528Pandas\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u7684\u7c7b\u578b\u6765\u51b3\u5b9a\u3002\u4e0b\u9762\u5c06\u5206\u522b\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u7d22\u5f15<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u77e9\u9635\u53ef\u4ee5\u7528\u5d4c\u5957\u5217\u8868\u6765\u8868\u793a\u3002\u5bf9\u4e8e\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\u77e9\u9635\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7d22\u5f15\u6765\u83b7\u53d6\u67d0\u4e2a\u4f4d\u7f6e\u7684\u503c\u3002\u4e0b\u9762\u662f\u5177\u4f53\u7684\u65b9\u6cd5\u548c\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><h2>1\u3001\u5d4c\u5957\u5217\u8868\u8868\u793a\u77e9\u9635<\/h2>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u5f0f\u6765\u8868\u793a\u77e9\u9635\u3002\u6bcf\u4e2a\u5185\u5c42\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\uff0c\u5916\u5c42\u5217\u8868\u5305\u542b\u6240\u6709\u884c\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><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u7d22\u5f15\u83b7\u53d6\u503c<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u77e9\u9635\u4e2d\u67d0\u4f4d\u7f6e\u7684\u503c\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = matrix[1][2]  # \u83b7\u53d6\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u503c<\/p>\n<p>print(value)  # \u8f93\u51fa 6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5c0f\u578b\u77e9\u9635\u548c\u7b80\u5355\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/strong><\/h2>\n<p><p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u7279\u522b\u64c5\u957f\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5b89\u88c5NumPy<\/h2>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u521b\u5efaNumPy\u6570\u7ec4\u8868\u793a\u77e9\u9635<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u6570\u7ec4\u8868\u793a\u77e9\u9635\uff0c\u6bd4\u5d4c\u5957\u5217\u8868\u66f4\u9ad8\u6548\u4e14\u529f\u80fd\u66f4\u5f3a\u5927\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><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528\u7d22\u5f15\u83b7\u53d6\u503c<\/h2>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u83b7\u53d6\u503c\uff0c\u8bed\u6cd5\u4e0e\u5d4c\u5957\u5217\u8868\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = matrix[1, 2]  # \u83b7\u53d6\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u503c<\/p>\n<p>print(value)  # \u8f93\u51fa 6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u8fdb\u884c\u77e9\u9635\u64cd\u4f5c\uff0c\u5982\u77e9\u9635\u52a0\u51cf\u4e58\u9664\u3001\u8f6c\u7f6e\u3001\u6c42\u9006\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/strong><\/h2>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\uff0cDataFrame\u662fPandas\u4e2d\u7528\u4e8e\u8868\u793a\u4e8c\u7ef4\u6570\u636e\u7684\u4e3b\u8981\u6570\u636e\u7ed3\u6784\u3002\u4f7f\u7528Pandas\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5b89\u88c5Pandas<\/h2>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5Pandas\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u521b\u5efaDataFrame\u8868\u793a\u77e9\u9635<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pandas\u7684DataFrame\u6765\u8868\u793a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>matrix = pd.DataFrame([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528iloc\u83b7\u53d6\u503c<\/h2>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86iloc\u5c5e\u6027\u6765\u901a\u8fc7\u884c\u5217\u7d22\u5f15\u83b7\u53d6\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = matrix.iloc[1, 2]  # \u83b7\u53d6\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u503c<\/p>\n<p>print(value)  # \u8f93\u51fa 6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0eNumPy\u7c7b\u4f3c\uff0cPandas\u4e5f\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\uff0c\u5982\u6570\u636e\u7b5b\u9009\u3001\u5206\u7ec4\u3001\u805a\u5408\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528SciPy\u5e93<\/strong><\/h2>\n<p><p>SciPy\u662f\u57fa\u4e8eNumPy\u7684\u9ad8\u7ea7\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u9ad8\u7ea7\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5b89\u88c5SciPy<\/h2>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5SciPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scipy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u521b\u5efa\u7a00\u758f\u77e9\u9635<\/h2>\n<\/p>\n<p><p>SciPy\u7279\u522b\u64c5\u957f\u5904\u7406\u7a00\u758f\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528scipy.sparse\u6a21\u5757\u6765\u521b\u5efa\u7a00\u758f\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.sparse import csr_matrix<\/p>\n<p>matrix = csr_matrix([<\/p>\n<p>    [1, 0, 0],<\/p>\n<p>    [0, 5, 0],<\/p>\n<p>    [0, 0, 9]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528\u7d22\u5f15\u83b7\u53d6\u503c<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7tocoo()\u65b9\u6cd5\u5c06\u7a00\u758f\u77e9\u9635\u8f6c\u6362\u4e3aCOO\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528row\u548ccol\u5c5e\u6027\u6765\u83b7\u53d6\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_coo = matrix.tocoo()<\/p>\n<p>value = matrix_coo.data[(matrix_coo.row == 1) &amp; (matrix_coo.col == 2)]<\/p>\n<p>print(value)  # \u8f93\u51fa []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3\uff1a<\/strong> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u77e9\u9635\u5728\u67d0\u4f4d\u7f6e\u7684\u503c\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001Pandas\u5e93\u4ee5\u53caSciPy\u5e93\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u89c4\u6a21\u548c\u64cd\u4f5c\u7684\u590d\u6742\u6027\u6765\u51b3\u5b9a\u3002\u5bf9\u4e8e\u7b80\u5355\u5c0f\u578b\u77e9\u9635\uff0c\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u5373\u53ef\uff1b\u5bf9\u4e8e\u5927\u89c4\u6a21\u77e9\u9635\u548c\u590d\u6742\u64cd\u4f5c\uff0c\u5efa\u8bae\u4f7f\u7528NumPy\u3001Pandas\u6216SciPy\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u77e9\u9635\u7684\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002\u901a\u8fc7\u7d22\u5f15\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u77e9\u9635\u4e2d\u7279\u5b9a\u4f4d\u7f6e\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>matrix<\/code>\u76842D\u6570\u7ec4\uff0c\u8981\u83b7\u53d6\u4f4d\u4e8e\u7b2ci\u884c\u7b2cj\u5217\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matrix[i][j]<\/code>\u6216<code>matrix[i, j]<\/code>\u7684\u65b9\u5f0f\u3002\u786e\u4fdd\u884c\u548c\u5217\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u53d6\u77e9\u9635\u503c\u65f6\uff0cNumPy\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>NumPy\u662fPython\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5bf9\u8c61\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528NumPy\u5904\u7406\u77e9\u9635\u65f6\uff0c\u4e0d\u4ec5\u4ee3\u7801\u7b80\u6d01\u4e14\u6267\u884c\u6548\u7387\u9ad8\u3002\u901a\u8fc7NumPy\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u5207\u7247\u3001\u5e7f\u64ad\u548c\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u63d0\u5347\u8ba1\u7b97\u901f\u5ea6\uff0c\u5c24\u5176\u662f\u5728\u5927\u6570\u636e\u96c6\u4e0a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u77e9\u9635\u8d8a\u754c\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u8bbf\u95ee\u77e9\u9635\u65f6\uff0c\u786e\u4fdd\u7d22\u5f15\u5728\u6709\u6548\u8303\u56f4\u5185\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u82e5\u8bd5\u56fe\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u7d22\u5f15\uff0c\u5c06\u5f15\u53d1<code>IndexError<\/code>\u3002\u4e3a\u907f\u514d\u6b64\u7c7b\u9519\u8bef\uff0c\u53ef\u4ee5\u5728\u8bbf\u95ee\u524d\u68c0\u67e5\u884c\u548c\u5217\u7684\u5927\u5c0f\uff0c\u786e\u4fdd\u6240\u9700\u7684\u7d22\u5f15\u5728\u77e9\u9635\u7684\u7ef4\u5ea6\u8303\u56f4\u5185\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u548c\u5904\u7406\u6f5c\u5728\u7684\u8d8a\u754c\u9519\u8bef\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53d6\u77e9\u9635\u5728\u67d0\u4f4d\u7f6e\u7684\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7d22\u5f15\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93\u3002 \u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55 [&hellip;]","protected":false},"author":3,"featured_media":1120973,"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\/1120965"}],"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=1120965"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120965\/revisions"}],"predecessor-version":[{"id":1120974,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120965\/revisions\/1120974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1120973"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}