{"id":1115545,"date":"2025-01-08T18:07:47","date_gmt":"2025-01-08T10:07:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115545.html"},"modified":"2025-01-08T18:07:51","modified_gmt":"2025-01-08T10:07:51","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86tuple%e8%bd%ac%e5%8c%96%e4%b8%ba%e7%9f%a9%e9%98%b5-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1115545.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06tuple\u8f6c\u5316\u4e3a\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080244\/c18b54bb-c458-4d14-9917-8f313b314e63.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06tuple\u8f6c\u5316\u4e3a\u77e9\u9635\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u5c06\u5143\u7ec4\uff08tuple\uff09\u8f6c\u6362\u4e3a\u77e9\u9635\uff08matrix\uff09\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528NumPy\u5e93\u3002NumPy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u80fd\u591f\u8f7b\u677e\u5730\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002<strong>\u901a\u8fc7\u4f7f\u7528NumPy\u7684array()\u51fd\u6570\u3001reshape()\u51fd\u6570\u3001\u4ee5\u53calist comprehension\u65b9\u6cd5<\/strong>\uff0c\u6211\u4eec\u80fd\u591f\u5feb\u901f\u5b9e\u73b0\u4ece\u5143\u7ec4\u5230\u77e9\u9635\u7684\u8f6c\u6362\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93\u7684array()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u662fPython\u4e2d\u5904\u7406\u6570\u503c\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5bf9\u8c61\u2014\u2014ndarray\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u7684array()\u51fd\u6570\u5c06\u4e00\u4e2a\u5143\u7ec4\u76f4\u63a5\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u7136\u540e\u518d\u6839\u636e\u9700\u8981\u8fdb\u884creshape\u64cd\u4f5c\u5c06\u5176\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u5143\u7ec4<\/strong><\/h2>\n<p>tuple_data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))<\/p>\n<h2><strong>\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>numpy_array = np.array(tuple_data)<\/p>\n<h2><strong>\u6253\u5370NumPy\u6570\u7ec4<\/strong><\/h2>\n<p>print(numpy_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86NumPy\u5e93\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5143\u7ec4<code>tuple_data<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>np.array()<\/code>\u51fd\u6570\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728<code>numpy_array<\/code>\u53d8\u91cf\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370<code>numpy_array<\/code>\u4ee5\u67e5\u770b\u8f6c\u6362\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u7684reshape()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u7684\u5143\u7ec4\u6570\u636e\u662f\u4e00\u7ef4\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u7684reshape()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u77e9\u9635\u5f62\u5f0f\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u4e00\u7ef4\u5143\u7ec4\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a3&#215;3\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u4e00\u7ef4\u5143\u7ec4<\/strong><\/h2>\n<p>tuple_data = (1, 2, 3, 4, 5, 6, 7, 8, 9)<\/p>\n<h2><strong>\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>numpy_array = np.array(tuple_data)<\/p>\n<h2><strong>\u4f7f\u7528reshape()\u51fd\u6570\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a3x3\u77e9\u9635<\/strong><\/h2>\n<p>matrix = numpy_array.reshape(3, 3)<\/p>\n<h2><strong>\u6253\u5370\u77e9\u9635<\/strong><\/h2>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e00\u7ef4\u5143\u7ec4<code>tuple_data<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>np.array()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>reshape(3, 3)<\/code>\u51fd\u6570\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a3&#215;3\u7684\u77e9\u9635\uff0c\u6700\u540e\u6253\u5370\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528list comprehension\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u4e0d\u60f3\u4f9d\u8d56NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684list comprehension\u65b9\u6cd5\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5143\u7ec4<\/p>\n<p>tuple_data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))<\/p>\n<h2><strong>\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>list_data = [list(row) for row in tuple_data]<\/p>\n<h2><strong>\u6253\u5370\u5217\u8868<\/strong><\/h2>\n<p>print(list_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5143\u7ec4<code>tuple_data<\/code>\uff0c\u7136\u540e\u4f7f\u7528list comprehension\u5c06\u5143\u7ec4\u7684\u6bcf\u4e00\u884c\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728<code>list_data<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370<code>list_data<\/code>\u4ee5\u67e5\u770b\u8f6c\u6362\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u5b9e\u73b0\u77e9\u9635\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u6765\u5b9e\u73b0\u5143\u7ec4\u5230\u77e9\u9635\u7684\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5143\u7ec4<\/p>\n<p>tuple_data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868<\/strong><\/h2>\n<p>matrix = []<\/p>\n<h2><strong>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>for row in tuple_data:<\/p>\n<p>    matrix_row = []<\/p>\n<p>    for elem in row:<\/p>\n<p>        matrix_row.append(elem)<\/p>\n<p>    matrix.append(matrix_row)<\/p>\n<h2><strong>\u6253\u5370\u77e9\u9635<\/strong><\/h2>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5143\u7ec4<code>tuple_data<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u5c06\u5143\u7ec4\u7684\u6bcf\u4e00\u884c\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5e76\u5c06\u7ed3\u679c\u6dfb\u52a0\u5230<code>matrix<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370<code>matrix<\/code>\u4ee5\u67e5\u770b\u8f6c\u6362\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u4e2d\u7684\u5143\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<strong>\u4f7f\u7528NumPy\u5e93\u7684array()\u51fd\u6570\u548creshape()\u51fd\u6570\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u5f0f<\/strong>\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528Python\u5185\u7f6e\u7684list comprehension\u65b9\u6cd5\u6216\u5d4c\u5957\u5faa\u73af\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u6548\u679c\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0c<strong>NumPy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f7f\u5f97\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u9ad8\u6548<\/strong>\u3002\u5982\u679c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u77e9\u9635\u64cd\u4f5c\uff0c\u5efa\u8bae\u5b66\u4e60\u548c\u4f7f\u7528NumPy\u5e93\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5143\u7ec4\uff08tuple\uff09\u8f6c\u6362\u4e3a\u77e9\u9635\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u901a\u5e38\u6d89\u53ca\u4f7f\u7528NumPy\u5e93\u3002NumPy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u53ef\u4ee5\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u793a\u4f8b\u5143\u7ec4\ntuple_data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))\n\n# \u8f6c\u6362\u4e3a\u77e9\u9635\nmatrix = np.array(tuple_data)\n\nprint(matrix)\n<\/code><\/pre>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><\/p>\n<p>\u5728\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u5143\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5b50\u5143\u7ec4\uff08\u884c\uff09\u5177\u6709\u76f8\u540c\u7684\u957f\u5ea6\u3002\u5982\u679c\u5b50\u5143\u7ec4\u957f\u5ea6\u4e0d\u4e00\u81f4\uff0cNumPy\u4f1a\u81ea\u52a8\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u4e0d\u89c4\u5219\u7684\u6570\u7ec4\uff0c\u800c\u4e0d\u662f\u77e9\u9635\u3002\u4e3a\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u6700\u597d\u5728\u8f6c\u6362\u4e4b\u524d\u68c0\u67e5\u5143\u7ec4\u7684\u7ed3\u6784\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5f62\u72b6\u662f\u6b63\u786e\u7684\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u5b9e\u73b0\u5143\u7ec4\u5230\u77e9\u9635\u7684\u8f6c\u6362\uff1f<\/strong><\/p>\n<p>\u9664\u4e86NumPy\uff0c\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Pandas\u5e93\uff0c\u53ef\u4ee5\u5148\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3aDataFrame\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Pandas\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u793a\u4f8b\u5143\u7ec4\ntuple_data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))\n\n# \u8f6c\u6362\u4e3aDataFrame\ndf = pd.DataFrame(tuple_data)\n\n# \u8f6c\u6362\u4e3a\u77e9\u9635\nmatrix = df.values\n\nprint(matrix)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u8f6c\u6362\u540e\u7684\u77e9\u9635\u7684\u7ef4\u5ea6\u548c\u5f62\u72b6\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528NumPy\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u6765\u68c0\u67e5\u77e9\u9635\u7684\u7ef4\u5ea6\u548c\u5f62\u72b6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">print(matrix.shape)  # \u8f93\u51fa\u77e9\u9635\u7684\u5f62\u72b6\n<\/code><\/pre>\n<p>\u5bf9\u4e8ePandas DataFrame\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u884c\u548c\u5217\u7684\u6570\u91cf\uff1a<\/p>\n<pre><code class=\"language-python\">print(df.shape)  # \u8f93\u51faDataFrame\u7684\u884c\u6570\u548c\u5217\u6570\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u4fe1\u606f\u5bf9\u4e8e\u4e86\u89e3\u6570\u636e\u7684\u7ed3\u6784\u548c\u540e\u7eed\u64cd\u4f5c\u975e\u5e38\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5143\u7ec4\uff08tuple\uff09\u8f6c\u6362\u4e3a\u77e9\u9635\uff08matrix\uff09\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528NumPy\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1115554,"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\/1115545"}],"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=1115545"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115545\/revisions"}],"predecessor-version":[{"id":1115556,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115545\/revisions\/1115556"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115554"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}