{"id":1024132,"date":"2024-12-30T14:11:11","date_gmt":"2024-12-30T06:11:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1024132.html"},"modified":"2024-12-30T14:11:13","modified_gmt":"2024-12-30T06:11:13","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e7%9f%a9%e9%98%b5%e8%b5%8b%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1024132.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u77e9\u9635\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1a6337a6-9992-42bb-8576-1866c21c19d2.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u7ed9\u77e9\u9635\u8d4b\u503c\" \/><\/p>\n<p><p> Python\u4e2d\u7ed9\u77e9\u9635\u8d4b\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u6216Pandas\u5e93\u7b49\u3002<strong>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93<\/strong>\u662f\u5e38\u89c1\u7684\u51e0\u79cd\u65b9\u5f0f\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u77e9\u9635\u8868\u793a\u65b9\u5f0f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u53c8\u662f\u4e00\u4e2a\u5217\u8868\u6765\u521b\u5efa\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a2x2\u7684\u77e9\u9635<\/p>\n<p>matrix = [[1, 2],<\/p>\n<p>          [3, 4]]<\/p>\n<h2><strong>\u7ed9\u77e9\u9635\u8d4b\u503c<\/strong><\/h2>\n<p>matrix[0][1] = 5  # \u5c06\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u503c\u6539\u4e3a5<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u61c2\uff0c\u9002\u7528\u4e8e\u5c0f\u578b\u77e9\u9635\u6216\u6559\u5b66\u793a\u4f8b\u3002\u4f46\u5b83\u7684\u7f3a\u70b9\u662f\u5bf9\u4e8e\u5927\u578b\u77e9\u9635\u64cd\u4f5c\u4e0d\u591f\u9ad8\u6548\uff0c\u4e14\u7f3a\u4e4f\u4e13\u95e8\u7684\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u7684\u5f3a\u5927\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u79d1\u5b66\u8ba1\u7b97\u51fd\u6570\uff0c\u662f\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7684\u57fa\u7840\u5e93\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528NumPy\u521b\u5efa\u548c\u8d4b\u503c\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2x2\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2],<\/p>\n<p>                   [3, 4]])<\/p>\n<h2><strong>\u7ed9\u77e9\u9635\u8d4b\u503c<\/strong><\/h2>\n<p>matrix[0, 1] = 5  # \u5c06\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u503c\u6539\u4e3a5<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u5e93\u7684\u4f18\u70b9\u662f\u9ad8\u6548\u3001\u529f\u80fd\u5f3a\u5927\uff0c\u652f\u6301\u5927\u91cf\u77e9\u9635\u64cd\u4f5c\u51fd\u6570<\/strong>\uff0c\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u8f6c\u7f6e\u3001\u6c42\u9006\u7b49\u3002\u5bf9\u4e8e\u9700\u8981\u5927\u91cf\u77e9\u9635\u8fd0\u7b97\u7684\u573a\u666f\uff0c\u5982\u79d1\u5b66\u8ba1\u7b97\u548c\u673a\u5668\u5b66\u4e60\uff0cNumPy\u662f\u9996\u9009\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u5229\u5668\uff0c\u5b83\u7684DataFrame\u7ed3\u6784\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8868\u793a\u548c\u64cd\u4f5c\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pandas\u521b\u5efa\u548c\u8d4b\u503c\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2x2\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = pd.DataFrame([[1, 2],<\/p>\n<p>                       [3, 4]])<\/p>\n<h2><strong>\u7ed9\u77e9\u9635\u8d4b\u503c<\/strong><\/h2>\n<p>matrix.iloc[0, 1] = 5  # \u5c06\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u503c\u6539\u4e3a5<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684\u4f18\u70b9\u662f<strong>\u529f\u80fd\u5f3a\u5927\uff0c\u652f\u6301\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c<\/strong>\uff0c\u4e14\u4e0e\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u5de5\u5177\u96c6\u6210\u826f\u597d\u3002Pandas\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u548c\u5206\u6790\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u77e9\u9635\u8d4b\u503c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e09\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u77e9\u9635\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Scipy\u5e93\u3001TensorFlow\u5e93\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Scipy\u521b\u5efa\u548c\u8d4b\u503c\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.sparse import lil_matrix<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2x2\u7684\u7a00\u758f\u77e9\u9635<\/strong><\/h2>\n<p>matrix = lil_matrix((2, 2))<\/p>\n<h2><strong>\u7ed9\u77e9\u9635\u8d4b\u503c<\/strong><\/h2>\n<p>matrix[0, 1] = 5  # \u5c06\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u503c\u6539\u4e3a5<\/p>\n<p>print(matrix.toarray())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Scipy\u7684\u4f18\u70b9\u662f<strong>\u652f\u6301\u7a00\u758f\u77e9\u9635<\/strong>\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u5927\u89c4\u6a21\u7a00\u758f\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u77e9\u9635\u8d4b\u503c\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u77e9\u9635\u8d4b\u503c\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u53ca\u5176\u63a8\u8350\u7684\u77e9\u9635\u8d4b\u503c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5c0f\u578b\u77e9\u9635\u64cd\u4f5c<\/strong>\uff1a\u5bf9\u4e8e\u5c0f\u578b\u77e9\u9635\u6216\u6559\u5b66\u793a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u79d1\u5b66\u8ba1\u7b97\u548c\u673a\u5668\u5b66\u4e60<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u5927\u91cf\u77e9\u9635\u8fd0\u7b97\u7684\u573a\u666f\uff0c\u5982\u79d1\u5b66\u8ba1\u7b97\u548c\u673a\u5668\u5b66\u4e60\uff0c\u63a8\u8350\u4f7f\u7528NumPy\u3002NumPy\u9ad8\u6548\u4e14\u529f\u80fd\u5f3a\u5927\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u90e8\u5206\u79d1\u5b66\u8ba1\u7b97\u548c\u673a\u5668\u5b66\u4e60\u7684\u9700\u6c42\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u6790\u548c\u5904\u7406<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u548c\u5206\u6790\u7684\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528Pandas\u3002Pandas\u7684DataFrame\u7ed3\u6784\u529f\u80fd\u5f3a\u5927\uff0c\u652f\u6301\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/li>\n<li><strong>\u7a00\u758f\u77e9\u9635\u64cd\u4f5c<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u7a00\u758f\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528Scipy\u3002Scipy\u652f\u6301\u7a00\u758f\u77e9\u9635\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u64cd\u4f5c\u5927\u89c4\u6a21\u7a00\u758f\u77e9\u9635\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7ed9\u77e9\u9635\u8d4b\u503c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\u3002<strong>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93<\/strong>\u662f\u5e38\u89c1\u4e14\u5b9e\u7528\u7684\u65b9\u6cd5\u3002\u5d4c\u5957\u5217\u8868\u9002\u7528\u4e8e\u5c0f\u578b\u77e9\u9635\u64cd\u4f5c\uff0cNumPy\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u673a\u5668\u5b66\u4e60\uff0cPandas\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0cScipy\u9002\u7528\u4e8e\u7a00\u758f\u77e9\u9635\u64cd\u4f5c\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u4f60\u80fd\u638c\u63e1\u4e0d\u540c\u65b9\u6cd5\u7684\u4f7f\u7528\uff0c\u5e76\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\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\u548c\u521d\u59cb\u5316\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u548c\u521d\u59cb\u5316\u77e9\u9635\u3002\u901a\u8fc7<code>numpy.array()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u6216\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmatrix = np.array([[1, 2, 3], [4, 5, 6]])\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a2&#215;3\u7684\u77e9\u9635\uff0c\u5143\u7d20\u4e3a1\u52306\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539\u77e9\u9635\u7684\u67d0\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u8be5\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u5df2\u6709\u4e00\u4e2a\u77e9\u9635<code>matrix<\/code>\uff0c\u60f3\u8981\u5c06\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\u4fee\u6539\u4e3a10\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix[0, 1] = 10\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u77e9\u9635\u7684\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u5143\u7d20\u5c06\u4f1a\u88ab\u66f4\u65b0\u4e3a10\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7528\u4e8e\u77e9\u9635\u64cd\u4f5c\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u8fdb\u884c\u77e9\u9635\u64cd\u4f5c\u3002\u4f8b\u5982\uff0cSciPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ebf\u6027\u4ee3\u6570\u529f\u80fd\uff0c\u800cPandas\u5219\u9002\u5408\u5904\u7406\u5e26\u6807\u7b7e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u5904\u7406\u77e9\u9635\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7ed9\u77e9\u9635\u8d4b\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u6216Pandas\u5e93\u7b49\u3002\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1024147,"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\/1024132"}],"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=1024132"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024132\/revisions"}],"predecessor-version":[{"id":1024151,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024132\/revisions\/1024151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1024147"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1024132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1024132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1024132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}