{"id":1130041,"date":"2025-01-08T20:35:20","date_gmt":"2025-01-08T12:35:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1130041.html"},"modified":"2025-01-08T20:35:22","modified_gmt":"2025-01-08T12:35:22","slug":"%e5%a6%82%e4%bd%95%e6%8a%8a%e6%95%b0%e7%bb%84%e8%bd%ac%e6%8d%a2%e6%88%90%e5%ad%97%e7%ac%a6%e4%b8%b2python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1130041.html","title":{"rendered":"\u5982\u4f55\u628a\u6570\u7ec4\u8f6c\u6362\u6210\u5b57\u7b26\u4e32python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100243\/18a98e04-dccd-4e8d-b441-55dfd3f35c77.webp\" alt=\"\u5982\u4f55\u628a\u6570\u7ec4\u8f6c\u6362\u6210\u5b57\u7b26\u4e32python\" \/><\/p>\n<p><p> <strong>\u5c06\u6570\u7ec4\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b8c\u6210\uff0c\u5305\u62ec\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u3001<code>str()<\/code>\u51fd\u6570\u3001\u5217\u8868\u89e3\u6790\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u9002\u7528\u573a\u666f\uff0c\u6bd4\u5982\uff1a\u9700\u8981\u7528\u7279\u5b9a\u5206\u9694\u7b26\u8fde\u63a5\u6570\u7ec4\u5143\u7d20\u65f6\u53ef\u4ee5\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u3001\u5feb\u901f\u8f6c\u6362\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u4ee3\u7801\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>join()<\/code> \u65b9\u6cd5\u662f\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u8fde\u63a5\u8d77\u6765\u3002\u5b83\u662f\u6700\u5e38\u7528\u7684\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e0e\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6570\u7ec4 <code>arr = [1, 2, 3, 4, 5]<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>str_arr = &#39;, &#39;.join(map(str, arr))<\/p>\n<p>print(str_arr)  # \u8f93\u51fa: &quot;1, 2, 3, 4, 5&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>map()<\/code> \u51fd\u6570\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u7528\u9017\u53f7\u548c\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u6307\u5b9a\u4efb\u610f\u7684\u5206\u9694\u7b26\u3002<\/li>\n<li><strong>\u6027\u80fd\u597d<\/strong>\uff1a\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c<code>join()<\/code> \u65b9\u6cd5\u7684\u6027\u80fd\u8f83\u597d\u3002<\/li>\n<\/ul>\n<p><p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u7528\u7279\u5b9a\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u8fde\u63a5\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<li>\u9700\u8981\u9ad8\u6548\u5730\u5904\u7406\u5927\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 <code>str()<\/code> \u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>str()<\/code> \u51fd\u6570\u53ef\u4ee5\u5c06\u4efb\u4f55\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u6570\u7ec4\uff0c<code>str()<\/code> \u51fd\u6570\u4f1a\u751f\u6210\u4e00\u4e2a\u7528\u65b9\u62ec\u53f7\u62ec\u8d77\u6765\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7528\u9017\u53f7\u548c\u7a7a\u683c\u5206\u9694\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e0e\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>str_arr = str(arr)<\/p>\n<p>print(str_arr)  # \u8f93\u51fa: &quot;[1, 2, 3, 4, 5]&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>str()<\/code> \u51fd\u6570\u76f4\u63a5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u76f4\u63a5<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<li><strong>\u901a\u7528\u6027\u9ad8<\/strong>\uff1a\u9002\u7528\u4e8e\u6240\u6709\u5bf9\u8c61\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u9700\u8981\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u3002<\/li>\n<li>\u9700\u8981\u5feb\u901f\u67e5\u770b\u6570\u7ec4\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408 <code>join()<\/code> \u65b9\u6cd5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u590d\u6742\u7684\u8f6c\u6362\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e0e\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6570\u7ec4 <code>arr = [1, 2, 3, 4, 5]<\/code>\uff0c\u5e76\u4e14\u6211\u4eec\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a0\u4e0a\u524d\u7f00 &quot;Num: &quot;\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>str_arr = &#39;, &#39;.join([f&#39;Num: {x}&#39; for x in arr])<\/p>\n<p>print(str_arr)  # \u8f93\u51fa: &quot;Num: 1, Num: 2, Num: 3, Num: 4, Num: 5&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5\u5c06\u5176\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u5e94\u7528\u4efb\u610f\u7684\u8f6c\u6362\u903b\u8f91\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027\u597d<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\u3002<\/li>\n<\/ul>\n<p><p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u590d\u6742\u7684\u5904\u7406\u3002<\/li>\n<li>\u9700\u8981\u751f\u6210\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u4f7f\u7528 <code>json.dumps()<\/code><\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\u6216\u8005\u5d4c\u5957\u7ed3\u6784\uff0c<code>json.dumps()<\/code> \u65b9\u6cd5\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e0e\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5d4c\u5957\u6570\u7ec4 <code>arr = [[1, 2], [3, 4], [5, 6]]<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>json.dumps()<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>arr = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>str_arr = json.dumps(arr)<\/p>\n<p>print(str_arr)  # \u8f93\u51fa: &quot;[[1, 2], [3, 4], [5, 6]]&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>json.dumps()<\/code> \u65b9\u6cd5\u5c06\u5d4c\u5957\u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u652f\u6301\u5d4c\u5957\u7ed3\u6784<\/strong>\uff1a\u53ef\u4ee5\u5904\u7406\u4efb\u610f\u590d\u6742\u7684\u5d4c\u5957\u6570\u7ec4\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027\u597d<\/strong>\uff1a\u751f\u6210\u7684 JSON \u5b57\u7b26\u4e32\u53ef\u4ee5\u5728\u4e0d\u540c\u7cfb\u7edf\u4e4b\u95f4\u4f20\u8f93\u3002<\/li>\n<\/ul>\n<p><p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u6216\u590d\u6742\u7ed3\u6784\u3002<\/li>\n<li>\u9700\u8981\u751f\u6210\u53ef\u8de8\u5e73\u53f0\u4f20\u8f93\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><h2>\u4e94\u3001\u4f7f\u7528 <code>numpy<\/code> \u5e93<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u5927\u6570\u636e\u5904\u7406\uff0c<code>numpy<\/code> \u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002<code>numpy.array_str()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06 <code>numpy<\/code> \u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e0e\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a <code>numpy<\/code> \u6570\u7ec4 <code>arr = np.array([1, 2, 3, 4, 5])<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>numpy.array_str()<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>str_arr = np.array_str(arr)<\/p>\n<p>print(str_arr)  # \u8f93\u51fa: &quot;[1 2 3 4 5]&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>numpy.array_str()<\/code> \u65b9\u6cd5\u5c06 <code>numpy<\/code> \u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1a<code>numpy<\/code> \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li>\u9700\u8981\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p><h2>\u516d\u3001\u4f7f\u7528 <code>pandas<\/code> \u5e93<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u5206\u6790\uff0c<code>pandas<\/code> \u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u5c06 <code>pandas<\/code> DataFrame \u6216 Series \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e0e\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a <code>pandas<\/code> DataFrame <code>df = pd.DataFrame({&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]})<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u7684 <code>to_string()<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]})<\/p>\n<p>str_df = df.to_string(index=False)<\/p>\n<p>print(str_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>pandas<\/code> \u7684 <code>to_string()<\/code> \u65b9\u6cd5\u5c06 DataFrame \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1a\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u3002<\/li>\n<li><strong>\u683c\u5f0f\u7075\u6d3b<\/strong>\uff1a\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u8fdb\u884c\u6570\u636e\u5206\u6790\u3002<\/li>\n<li>\u9700\u8981\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><h2>\u4e03\u3001\u4f7f\u7528 <code>ast.literal_eval()<\/code><\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u6570\u7ec4\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>ast.literal_eval()<\/code> \u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4e0e\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>str_arr = &quot;[1, 2, 3, 4, 5]&quot;<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>ast.literal_eval()<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>str_arr = &quot;[1, 2, 3, 4, 5]&quot;<\/p>\n<p>arr = ast.literal_eval(str_arr)<\/p>\n<p>print(arr)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>ast.literal_eval()<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u5168<\/strong>\uff1a\u76f8\u6bd4\u4e8e <code>eval()<\/code> \u65b9\u6cd5\uff0c<code>ast.literal_eval()<\/code> \u66f4\u52a0\u5b89\u5168\u3002<\/li>\n<li><strong>\u7b80\u5355<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<\/ul>\n<p><p><strong>\u9002\u7528\u573a\u666f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5bf9\u8c61\u3002<\/li>\n<li>\u9700\u8981\u5b89\u5168\u5730\u6267\u884c\u5b57\u7b26\u4e32\u89e3\u6790\u3002<\/li>\n<\/ul>\n<p><h2>\u516b\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5728 Python \u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5\u6700\u4e3a\u7075\u6d3b\u548c\u9ad8\u6548<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff1b<strong><code>str()<\/code> \u51fd\u6570\u7b80\u5355\u76f4\u63a5<\/strong>\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u8f6c\u6362\uff1b<strong>\u5217\u8868\u89e3\u6790<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u590d\u6742\u5904\u7406\u7684\u60c5\u51b5\uff1b<strong><code>json.dumps()<\/code> \u65b9\u6cd5\u9002\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\u548c\u590d\u6742\u7ed3\u6784<\/strong>\uff1b<strong><code>numpy<\/code> \u548c <code>pandas<\/code> \u5e93\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790<\/strong>\uff1b<strong><code>ast.literal_eval()<\/code> \u65b9\u6cd5\u9002\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u6570\u7ec4<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b8c\u6210\u6570\u7ec4\u5230\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u6570\u7ec4\uff08\u901a\u5e38\u662f\u5217\u8868\uff09\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\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)\nprint(result)  # \u8f93\u51fa: Hello World\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u5143\u7d20\uff0c\u975e\u5b57\u7b26\u4e32\u5143\u7d20\u9700\u8981\u5148\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5305\u542b\u975e\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u53ef\u4ee5\uff0c\u4f46\u9700\u8981\u5148\u5c06\u975e\u5b57\u7b26\u4e32\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>map()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, &#39;four&#39;]\nresult = &#39; &#39;.join(str(x) for x in my_list)\nprint(result)  # \u8f93\u51fa: 1 2 3 four\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u5143\u7d20\u90fd\u88ab\u6b63\u786e\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u8f6c\u6362\u6570\u7ec4\u4e3a\u5b57\u7b26\u4e32\u65f6\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u8f7b\u677e\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u7528\u9017\u53f7\u5206\u9694\u6570\u7ec4\u5143\u7d20\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]\nresult = &#39;, &#39;.join(my_list)\nprint(result)  # \u8f93\u51fa: apple, banana, cherry\n<\/code><\/pre>\n<p>\u53ea\u9700\u5c06\u5e0c\u671b\u4f7f\u7528\u7684\u5206\u9694\u7b26\u4f5c\u4e3a<code>join()<\/code>\u65b9\u6cd5\u7684\u8c03\u7528\u5bf9\u8c61\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06\u6570\u7ec4\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b8c\u6210\uff0c\u5305\u62ec\u4f7f\u7528join()\u65b9\u6cd5\u3001str()\u51fd\u6570\u3001\u5217\u8868\u89e3\u6790\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1130046,"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\/1130041"}],"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=1130041"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1130041\/revisions"}],"predecessor-version":[{"id":1130048,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1130041\/revisions\/1130048"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1130046"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1130041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1130041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1130041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}