{"id":1124191,"date":"2025-01-08T19:39:13","date_gmt":"2025-01-08T11:39:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1124191.html"},"modified":"2025-01-08T19:39:15","modified_gmt":"2025-01-08T11:39:15","slug":"python%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8f%98%e4%b8%ba%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e6%8a%8a%e5%bc%95%e5%8f%b7%e5%8e%bb%e6%8e%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1124191.html","title":{"rendered":"python\u5b57\u7b26\u4e32\u53d8\u4e3a\u5217\u8868\u5982\u4f55\u628a\u5f15\u53f7\u53bb\u6389"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085351\/1317a898-6699-40c9-acd9-f85771a313e3.webp\" alt=\"python\u5b57\u7b26\u4e32\u53d8\u4e3a\u5217\u8868\u5982\u4f55\u628a\u5f15\u53f7\u53bb\u6389\" \/><\/p>\n<p><p> <strong>\u8981\u5c06 Python \u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5217\u8868\u5e76\u53bb\u6389\u5f15\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>split<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\u5e76\u901a\u8fc7\u904d\u5386\u5217\u8868\u53bb\u6389\u5f15\u53f7\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca\u5185\u7f6e\u7684 <code>ast<\/code> \u6a21\u5757\u3002<\/strong> \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u56db\u79cd\u65b9\u6cd5\uff0c\u5e76\u4ee5\u4e00\u79cd\u65b9\u6cd5\u4e3a\u4f8b\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u4f7f\u7528 <code>split<\/code> \u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u4f7f\u7528 <code>split<\/code> \u65b9\u6cd5\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5217\u8868\u5e76\u53bb\u6389\u5f15\u53f7\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u6838\u5fc3\u5728\u4e8e\u5229\u7528\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\uff0c\u7136\u540e\u518d\u5904\u7406\u6bcf\u4e00\u4e2a\u5206\u5272\u540e\u7684\u5143\u7d20\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>&quot;a, b, c&quot;<\/code>\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868 <code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code> \u5e76\u53bb\u6389\u5f15\u53f7\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>split<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u9017\u53f7\u5206\u5272\u3002<\/li>\n<li>\u4f7f\u7528 <code>strip<\/code> \u65b9\u6cd5\u53bb\u6389\u6bcf\u4e2a\u5143\u7d20\u4e24\u7aef\u7684\u7a7a\u683c\u548c\u5f15\u53f7\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">input_string = &#39;&quot;a&quot;, &quot;b&quot;, &quot;c&quot;&#39;<\/p>\n<p>elements = input_string.split(&#39;,&#39;)<\/p>\n<p>cleaned_elements = [element.strip().replace(&#39;&quot;&#39;, &#39;&#39;) for element in elements]<\/p>\n<p>print(cleaned_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5b57\u7b26\u4e32\u6309\u9017\u53f7\u5206\u5272\u6210\u5217\u8868 <code>[&#39;&quot;a&quot;&#39;, &#39;&quot;b&quot;&#39;, &#39;&quot;c&quot;&#39;]<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u89e3\u6790\u9010\u4e2a\u53bb\u6389\u6bcf\u4e2a\u5143\u7d20\u7684\u5f15\u53f7\uff0c\u6700\u7ec8\u5f97\u5230\u7ed3\u679c <code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u5230\u5217\u8868\u7684\u8f6c\u6362\u5e76\u53bb\u6389\u5f15\u53f7\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>replace<\/code> \u65b9\u6cd5\u53bb\u6389\u6bcf\u4e2a\u5143\u7d20\u7684\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">input_string = &#39;&quot;a&quot;, &quot;b&quot;, &quot;c&quot;&#39;<\/p>\n<p>cleaned_elements = [element.replace(&#39;&quot;&#39;, &#39;&#39;).strip() for element in input_string.split(&#39;,&#39;)]<\/p>\n<p>print(cleaned_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u4e0a\u9762\u7684 <code>split<\/code> \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u662f\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>re<\/code> \u6a21\u5757\u4e2d\u7684 <code>re.findall<\/code> \u65b9\u6cd5\u6765\u63d0\u53d6\u6240\u6709\u4e0d\u5e26\u5f15\u53f7\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>input_string = &#39;&quot;a&quot;, &quot;b&quot;, &quot;c&quot;&#39;<\/p>\n<p>cleaned_elements = re.findall(r&#39;\\b\\w+\\b&#39;, input_string)<\/p>\n<p>print(cleaned_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>\\b\\w+\\b<\/code> \u627e\u5230\u6240\u6709\u5355\u8bcd\u8fb9\u754c\u4e2d\u7684\u975e\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u53bb\u6389\u5f15\u53f7\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u56db\u3001\u4f7f\u7528 <code>ast<\/code> \u6a21\u5757<\/strong><\/h2>\n<p><p>Python \u7684 <code>ast<\/code> \u6a21\u5757\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u8868\u8fbe\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u529f\u80fd\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u5217\u8868\uff0c\u7136\u540e\u53bb\u6389\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>input_string = &#39;[&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]&#39;<\/p>\n<p>cleaned_elements = ast.literal_eval(input_string)<\/p>\n<p>print(cleaned_elements)<\/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\u5217\u8868\uff0c\u5f97\u5230 <code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u56db\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u5c06 Python \u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5217\u8868\u5e76\u53bb\u6389\u5f15\u53f7\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\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\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5e76\u79fb\u9664\u5f15\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>strip<\/code> \u65b9\u6cd5\u6765\u53bb\u9664\u5f15\u53f7\uff0c\u7136\u540e\u4f7f\u7528 <code>split<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5305\u542b\u5f15\u53f7\u7684\u5b57\u7b26\u4e32 <code>&quot;[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]&quot;<\/code>\uff0c\u53ef\u4ee5\u5148\u53bb\u6389\u5f15\u53f7\uff0c\u518d\u5c06\u5b83\u5206\u5272\u6210\u5217\u8868\u5143\u7d20\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5217\u8868\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ast.literal_eval()<\/code> \u51fd\u6570\u5c06\u683c\u5f0f\u6b63\u786e\u7684\u5b57\u7b26\u4e32\u76f4\u63a5\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4f7f\u7528 <code>eval()<\/code> \u7684\u5b89\u5168\u9690\u60a3\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import ast\nstring = &quot;[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]&quot;\nlist_result = ast.literal_eval(string)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c06\u5f97\u5230\u4e00\u4e2a\u4e0d\u5305\u542b\u5f15\u53f7\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u65f6\uff0c\u786e\u4fdd\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u6b63\u786e\u975e\u5e38\u91cd\u8981\u3002\u5982\u679c\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u7b26\u5408 Python \u7684\u5217\u8868\u6807\u51c6\uff0c\u4f8b\u5982\u7f3a\u5c11\u5f15\u53f7\u6216\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u5206\u9694\u7b26\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u89e3\u6790\u9519\u8bef\u3002\u6b64\u5916\uff0c\u4f7f\u7528 <code>eval()<\/code> \u53ef\u80fd\u4f1a\u5f15\u5165\u5b89\u5168\u98ce\u9669\uff0c\u56e0\u6b64\u5efa\u8bae\u4f18\u5148\u4f7f\u7528 <code>ast.literal_eval()<\/code> \u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06 Python \u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5217\u8868\u5e76\u53bb\u6389\u5f15\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528 split \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\u5e76\u901a\u8fc7\u904d\u5386\u5217\u8868\u53bb\u6389 [&hellip;]","protected":false},"author":3,"featured_media":1124200,"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\/1124191"}],"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=1124191"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124191\/revisions"}],"predecessor-version":[{"id":1124203,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124191\/revisions\/1124203"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1124200"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1124191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1124191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1124191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}