{"id":1120305,"date":"2025-01-08T18:54:51","date_gmt":"2025-01-08T10:54:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120305.html"},"modified":"2025-01-08T18:54:53","modified_gmt":"2025-01-08T10:54:53","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%8a%8a%e5%88%97%e8%a1%a8%e8%bd%ac%e6%8d%a2%e6%88%90%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120305.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u628a\u5217\u8868\u8f6c\u6362\u6210\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083030\/ab03050f-bf0b-47e8-b563-c1fab5c4dc1d.webp\" alt=\"python\u4e2d\u5982\u4f55\u628a\u5217\u8868\u8f6c\u6362\u6210\u77e9\u9635\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u6210\u77e9\u9635\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/p>\n<\/p>\n<p><p><strong>NumPy\u5e93<\/strong>\uff1aNumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u65b9\u6cd5\u3002\u901a\u8fc7NumPy\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u5217\u8868\u8f6c\u6362\u6210\u77e9\u9635\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u76f4\u63a5\u8f6c\u6362\u6210NumPy\u6570\u7ec4\uff0c\u4ece\u800c\u5b9e\u73b0\u77e9\u9635\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5NumPy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528numpy.array()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>NumPy\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570<code>numpy.array()<\/code>\uff0c\u53ef\u4ee5\u5c06Python\u4e2d\u7684\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u8fdb\u800c\u5b9e\u73b0\u77e9\u9635\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>list_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff08\u77e9\u9635\uff09<\/strong><\/h2>\n<p>matrix = np.array(list_data)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>list_data<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5b50\u5217\u8868\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\u3002\u901a\u8fc7<code>np.array(list_data)<\/code>\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u5217\u8868\u8f6c\u6362\u6210\u4e86NumPy\u6570\u7ec4\uff08\u77e9\u9635\uff09\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528reshape()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u662f\u4e00\u7ef4\u7684\uff0c\u4e14\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528<code>reshape()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u4e00\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>list_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a3x3\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array(list_data).reshape(3, 3)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>list_data<\/code>\u662f\u4e00\u7ef4\u7684\uff0c\u901a\u8fc7<code>reshape(3, 3)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a3\u884c3\u5217\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528NumPy\u5e93\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65b9\u5f0f\u624b\u52a8\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4e00\u7ef4\u5217\u8868<\/p>\n<p>list_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a3x3\u77e9\u9635<\/strong><\/h2>\n<p>matrix = [list_data[i:i+3] for i in range(0, len(list_data), 3)]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>list_data<\/code>\u662f\u4e00\u7ef4\u7684\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a3\u884c3\u5217\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u5b89\u88c5Pandas\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528pandas.DataFrame()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>pandas.DataFrame()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\uff0c\u4ece\u800c\u5b9e\u73b0\u77e9\u9635\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>list_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>matrix = pd.DataFrame(list_data)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>list_data<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5b50\u5217\u8868\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\u3002\u901a\u8fc7<code>pd.DataFrame(list_data)<\/code>\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\uff08\u77e9\u9635\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5d4c\u5957\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\uff0c\u4e5f\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4e00\u7ef4\u5217\u8868<\/p>\n<p>list_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a3x3\u77e9\u9635<\/strong><\/h2>\n<p>matrix = []<\/p>\n<p>for i in range(0, len(list_data), 3):<\/p>\n<p>    row = []<\/p>\n<p>    for j in range(3):<\/p>\n<p>        row.append(list_data[i + j])<\/p>\n<p>    matrix.append(row)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>list_data<\/code>\u662f\u4e00\u7ef4\u7684\uff0c\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a3\u884c3\u5217\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528zip()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7<code>zip()<\/code>\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4e00\u7ef4\u5217\u8868<\/p>\n<p>list_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a3x3\u77e9\u9635<\/strong><\/h2>\n<p>matrix = list(zip(*[iter(list_data)]*3))<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>list_data<\/code>\u662f\u4e00\u7ef4\u7684\uff0c\u901a\u8fc7<code>zip(*[iter(list_data)]*3)<\/code>\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a3\u884c3\u5217\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5728Python\u4e2d\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001Pandas\u5e93\u3001\u5d4c\u5957\u5faa\u73af\u548czip()\u51fd\u6570\u7b49\u3002<strong>\u5176\u4e2dNumPy\u5e93\u662f\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u7684\u6570\u636e\u548c\u77e9\u9635\u64cd\u4f5c\u3002\u800c\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5d4c\u5957\u5faa\u73af\u5219\u9002\u5408\u4e00\u4e9b\u7b80\u5355\u7684\u573a\u666f\u3002\u5982\u679c\u5bf9\u6570\u636e\u5904\u7406\u6709\u66f4\u9ad8\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Pandas\u5e93\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u60a8\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u89e3\u51b3\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u5c06\u5217\u8868\u8f6c\u6362\u6210\u77e9\u9635\uff1f<\/strong><br \/>Python\u4e2d\u5e38\u7528\u7684\u5e93\u5305\u62ecNumPy\u548cPandas\u3002NumPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u800cPandas\u5219\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u4f7f\u7528NumPy\u7684<code>reshape<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\uff0c\u800cPandas\u7684<code>DataFrame<\/code>\u53ef\u4ee5\u76f4\u63a5\u5c06\u5217\u8868\u8f6c\u4e3a\u8868\u683c\u5f62\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5217\u8868\u80fd\u591f\u6210\u529f\u8f6c\u6362\u4e3a\u77e9\u9635\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u5217\u8868\u5230\u77e9\u9635\u7684\u8f6c\u6362\u65f6\uff0c\u786e\u4fdd\u5217\u8868\u7684\u5143\u7d20\u4e2a\u6570\u80fd\u591f\u88ab\u76ee\u6807\u77e9\u9635\u7684\u884c\u5217\u6570\u6574\u9664\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2am\u884cn\u5217\u7684\u77e9\u9635\uff0c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u603b\u6570\u5e94\u4e3am*n\u3002\u5982\u679c\u4e0d\u6ee1\u8db3\u8fd9\u4e2a\u6761\u4ef6\uff0c\u8f6c\u6362\u5c06\u4f1a\u5931\u8d25\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u5982\u4f55\u8bbf\u95ee\u77e9\u9635\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u6216\u884c\u5217\uff1f<\/strong><br \/>\u4e00\u65e6\u5217\u8868\u6210\u529f\u8f6c\u6362\u4e3a\u77e9\u9635\uff0c\u8bbf\u95ee\u7279\u5b9a\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u5b9e\u73b0\u3002\u5728NumPy\u4e2d\uff0c\u4f7f\u7528<code>matrix[row_index, column_index]<\/code>\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u67d0\u4e2a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u800c\u5982\u679c\u4f7f\u7528Pandas\u7684<code>DataFrame<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>dataframe.iloc[row_index, column_index]<\/code>\u6765\u83b7\u53d6\u7279\u5b9a\u7684\u6570\u636e\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u6570\u636e\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u6210\u77e9\u9635\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f NumP [&hellip;]","protected":false},"author":3,"featured_media":1120313,"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\/1120305"}],"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=1120305"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120305\/revisions"}],"predecessor-version":[{"id":1120314,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120305\/revisions\/1120314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1120313"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}