{"id":1161618,"date":"2025-01-13T19:18:31","date_gmt":"2025-01-13T11:18:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161618.html"},"modified":"2025-01-13T19:18:34","modified_gmt":"2025-01-13T11:18:34","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e8%a1%a8%e8%bd%ac%e5%8c%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161618.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u5316"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202824\/ec70d098-5d97-4602-b72a-b71b49e2a592.webp\" alt=\"python\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u5316\" \/><\/p>\n<p><p> \u8981\u5c06Python\u4e2d\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6216\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001\u5b57\u5178\u3001JSON\u7b49\u3002<strong>\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3001\u4f7f\u7528<code>tuple()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3001\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3001\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\uff08\u5982\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u952e\u503c\u5bf9\uff09\u3001\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5c06\u5217\u8868\u8f6c\u6362\u4e3aJSON\u683c\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u60c5\u51b5\u662f\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5143\u7d20\u4e4b\u95f4\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5206\u9694\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u53ef\u4ee5\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u7136\u540e\uff0c\u9009\u62e9\u4e00\u4e2a\u5206\u9694\u7b26\u5e76\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b<\/strong><\/h2>\n<p>str_list = [str(item) for item in my_list]<\/p>\n<h2><strong>\u4f7f\u7528join()\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>separator = &#39;, &#39;<\/p>\n<p>result = separator.join(str_list)<\/p>\n<p>print(result)  # \u8f93\u51fa: &quot;1, 2, 3, 4, 5&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6216\u683c\u5f0f\u7684\u5404\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<code>join()<\/code>\u65b9\u6cd5\u662f\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>join()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5143\u7d20\u4e4b\u95f4\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5206\u9694\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u4f7f\u7528join()\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>separator = &#39;, &#39;<\/p>\n<p>result = separator.join(my_list)<\/p>\n<p>print(result)  # \u8f93\u51fa: &quot;apple, banana, cherry&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u9017\u53f7\u548c\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u5c06\u975e\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e0d\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u800c\u662f\u5176\u4ed6\u7c7b\u578b\uff08\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\uff09\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5148\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u7136\u540e\u518d\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u5176\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b<\/strong><\/h2>\n<p>str_list = [str(item) for item in my_list]<\/p>\n<h2><strong>\u4f7f\u7528join()\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>separator = &#39;, &#39;<\/p>\n<p>result = separator.join(str_list)<\/p>\n<p>print(result)  # \u8f93\u51fa: &quot;1, 2, 3, 4, 5&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u7136\u540e\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u5176\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5143\u7ec4\u662f\u4e00\u79cd\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<code>tuple()<\/code>\u51fd\u6570\u662f\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u7684\u5e38\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>tuple()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>tuple()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528tuple()\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4<\/strong><\/h2>\n<p>result = tuple(my_list)<\/p>\n<p>print(result)  # \u8f93\u51fa: (1, 2, 3, 4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>tuple()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002<code>set()<\/code>\u51fd\u6570\u662f\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u7684\u5e38\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>set()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>set()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 5, 4]<\/p>\n<h2><strong>\u4f7f\u7528set()\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408<\/strong><\/h2>\n<p>result = set(my_list)<\/p>\n<p>print(result)  # \u8f93\u51fa: {1, 2, 3, 4, 5}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u662f\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u5982\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>dict()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868\uff08\u952e\u503c\u5bf9\uff09<\/p>\n<p>my_list = [(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 25), (&#39;city&#39;, &#39;New York&#39;)]<\/p>\n<h2><strong>\u4f7f\u7528dict()\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>result = dict(my_list)<\/p>\n<p>print(result)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u5c06\u5217\u8868\uff08\u952e\u503c\u5bf9\uff09\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3aJSON\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cJSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u5c06\u5217\u8868\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u3002<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5c06\u5217\u8868\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>json.dumps()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06Python\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff09\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528json.dumps()\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3aJSON\u683c\u5f0f<\/strong><\/h2>\n<p>result = json.dumps(my_list)<\/p>\n<p>print(result)  # \u8f93\u51fa: &quot;[1, 2, 3, 4, 5]&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3aPandas\u6570\u636e\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0cPandas\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5e93\u3002Pandas\u63d0\u4f9b\u4e86\u4e24\u79cd\u4e3b\u8981\u7684\u6570\u636e\u7ed3\u6784\uff1a<code>Series<\/code>\u548c<code>DataFrame<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Pandas\u5c06\u5217\u8868\u8f6c\u6362\u4e3aSeries<\/h3>\n<\/p>\n<p><p><code>Series<\/code>\u662f\u4e00\u79cd\u4e00\u7ef4\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u770b\u4f5c\u662f\u5e26\u6709\u6807\u7b7e\u7684\u4e00\u7ef4\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3aPandas <code>Series<\/code>\u7684\u793a\u4f8b\uff1a<\/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>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u5c06\u5217\u8868\u8f6c\u6362\u4e3aSeries<\/strong><\/h2>\n<p>result = pd.Series(my_list)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u5c06\u5217\u8868\u8f6c\u6362\u4e3a<code>Series<\/code>\uff0c\u5e76\u4e14\u53ef\u4ee5\u770b\u5230\u7ed3\u679c\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u6709\u4e00\u4e2a\u6807\u7b7e\uff08\u7d22\u5f15\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Pandas\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame<\/h3>\n<\/p>\n<p><p><code>DataFrame<\/code>\u662f\u4e00\u79cd\u4e8c\u7ef4\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u770b\u4f5c\u662f\u5e26\u6709\u884c\u6807\u7b7e\u548c\u5217\u6807\u7b7e\u7684\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3aPandas <code>DataFrame<\/code>\u7684\u793a\u4f8b\uff1a<\/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>my_list = [[&#39;Alice&#39;, 25], [&#39;Bob&#39;, 30], [&#39;Charlie&#39;, 35]]<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>result = pd.DataFrame(my_list, columns=[&#39;Name&#39;, &#39;Age&#39;])<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u5c06\u5217\u8868\u8f6c\u6362\u4e3a<code>DataFrame<\/code>\uff0c\u5e76\u4e14\u6307\u5b9a\u4e86\u5217\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4<\/p>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0cNumpy\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5e93\u3002Numpy\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u7684\u652f\u6301\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Numpy\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4<\/h3>\n<\/p>\n<p><p><code>numpy.array()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u8f93\u5165\u6570\u636e\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4\u7684\u793a\u4f8b\uff1a<\/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>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528Numpy\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/h2>\n<p>result = np.array(my_list)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Numpy\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3aSQL\u6570\u636e\u5e93\u8868<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5b58\u50a8\u548c\u7ba1\u7406\u8fc7\u7a0b\u4e2d\uff0cSQL\u6570\u636e\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u7684\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>sqlite3<\/code>\u6a21\u5757\u5c06\u5217\u8868\u8f6c\u6362\u4e3aSQL\u6570\u636e\u5e93\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528sqlite3\u5c06\u5217\u8868\u8f6c\u6362\u4e3aSQL\u6570\u636e\u5e93\u8868<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3aSQL\u6570\u636e\u5e93\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>my_list = [(&#39;Alice&#39;, 25), (&#39;Bob&#39;, 30), (&#39;Charlie&#39;, 35)]<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93\uff08\u5982\u679c\u6570\u636e\u5e93\u4e0d\u5b58\u5728\uff0c\u5c06\u81ea\u52a8\u521b\u5efa\uff09<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u8868\uff08\u5982\u679c\u8868\u4e0d\u5b58\u5728\uff09<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS users<\/p>\n<p>                  (name TEXT, age INTEGER)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u5217\u8868\u4e2d\u7684\u6570\u636e<\/strong><\/h2>\n<p>cursor.executemany(&#39;INSERT INTO users (name, age) VALUES (?, ?)&#39;, my_list)<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>sqlite3<\/code>\u6a21\u5757\u5c06\u5217\u8868\u8f6c\u6362\u4e3aSQL\u6570\u636e\u5e93\u8868\uff0c\u5e76\u63d2\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3aCSV\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u8fc7\u7a0b\u4e2d\uff0cCSV\uff08Comma-Separated Values\uff09\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u7528\u7684\u683c\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>csv<\/code>\u6a21\u5757\u5c06\u5217\u8868\u8f6c\u6362\u4e3aCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528csv\u6a21\u5757\u5c06\u5217\u8868\u8f6c\u6362\u4e3aCSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3aCSV\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>my_list = [[&#39;Name&#39;, &#39;Age&#39;], [&#39;Alice&#39;, 25], [&#39;Bob&#39;, 30], [&#39;Charlie&#39;, 35]]<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2aCSV\u6587\u4ef6\u8fdb\u884c\u5199\u5165<\/strong><\/h2>\n<p>with open(&#39;example.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    writer = csv.writer(csvfile)<\/p>\n<p>    # \u5199\u5165\u5217\u8868\u4e2d\u7684\u6570\u636e<\/p>\n<p>    writer.writerows(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u5c06\u5217\u8868\u8f6c\u6362\u4e3aCSV\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3aXML\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u8fc7\u7a0b\u4e2d\uff0cXML\uff08eXtensible Markup Language\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u683c\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>xml.etree.ElementTree<\/code>\u6a21\u5757\u5c06\u5217\u8868\u8f6c\u6362\u4e3aXML\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528xml.etree.ElementTree\u5c06\u5217\u8868\u8f6c\u6362\u4e3aXML\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5217\u8868\u8f6c\u6362\u4e3aXML\u683c\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>my_list = [{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}, {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}, {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35}]<\/p>\n<h2><strong>\u521b\u5efaXML\u7684\u6839\u5143\u7d20<\/strong><\/h2>\n<p>root = ET.Element(&#39;users&#39;)<\/p>\n<h2><strong>\u5c06\u5217\u8868\u4e2d\u7684\u6570\u636e\u6dfb\u52a0\u5230XML\u4e2d<\/strong><\/h2>\n<p>for item in my_list:<\/p>\n<p>    user = ET.SubElement(root, &#39;user&#39;)<\/p>\n<p>    for key, value in item.items():<\/p>\n<p>        element = ET.SubElement(user, key)<\/p>\n<p>        element.text = str(value)<\/p>\n<h2><strong>\u5c06XML\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>xml_str = ET.tostring(root, encoding=&#39;unicode&#39;)<\/p>\n<p>print(xml_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>xml.etree.ElementTree<\/code>\u6a21\u5757\u5c06\u5217\u8868\u8f6c\u6362\u4e3aXML\u683c\u5f0f\uff0c\u5e76\u751f\u6210XML\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6216\u683c\u5f0f\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001\u5b57\u5178\u3001JSON\u683c\u5f0f\u3001Pandas\u6570\u636e\u7ed3\u6784\u3001Numpy\u6570\u7ec4\u3001SQL\u6570\u636e\u5e93\u8868\u3001CSV\u6587\u4ef6\u548cXML\u683c\u5f0f<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u6570\u636e\u5904\u7406\u3001\u5206\u6790\u548c\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u975e\u5e38\u5b9e\u7528\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u5982\u679c\u5143\u7d20\u662f\u5176\u4ed6\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5176\u8f6c\u6362\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;Hello&#39;, &#39;World&#39;]\nresult = &#39; &#39;.join(my_list)  # \u8f93\u51fa: &#39;Hello World&#39;\n<\/code><\/pre>\n<p>\u5982\u679c\u9700\u8981\u5c06\u5143\u7d20\u7528\u7279\u5b9a\u7684\u5206\u9694\u7b26\u8fde\u63a5\uff0c\u53ef\u4ee5\u5728<code>join()<\/code>\u4e2d\u6307\u5b9a\uff0c\u5982<code>&#39;,&#39;<\/code>\u7b49\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff1f<\/strong><br \/>\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u8fd9\u5728\u9700\u8981\u6d88\u9664\u91cd\u590d\u5143\u7d20\u65f6\u7279\u522b\u6709\u7528\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 2, 3]\nresult = set(my_list)  # \u8f93\u51fa: {1, 2, 3}\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u96c6\u5408\u7684\u5143\u7d20\u662f\u65e0\u5e8f\u7684\uff0c\u56e0\u6b64\u8f6c\u6362\u540e\u5143\u7d20\u7684\u987a\u5e8f\u53ef\u80fd\u4f1a\u6539\u53d8\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u5c06\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u3002\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u4e3a\u957f\u5ea6\u4e3a2\u7684\u5143\u7ec4\u6216\u5217\u8868\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [(&#39;a&#39;, 1), (&#39;b&#39;, 2), (&#39;c&#39;, 3)]\nresult = dict(my_list)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u5c06\u952e\u503c\u5bf9\u8fdb\u884c\u5feb\u901f\u8f6c\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u4e2d\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6216\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1161628,"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\/1161618"}],"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=1161618"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161618\/revisions"}],"predecessor-version":[{"id":1161630,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161618\/revisions\/1161630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161628"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}