{"id":1129812,"date":"2025-01-08T20:32:51","date_gmt":"2025-01-08T12:32:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1129812.html"},"modified":"2025-01-08T20:32:54","modified_gmt":"2025-01-08T12:32:54","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e5%9e%8b%e8%bd%ac%e6%8d%a2%e4%b8%balist%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1129812.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5b57\u7b26\u578b\u8f6c\u6362\u4e3alist\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100116\/06e82967-7d8b-4834-bbee-5e229e0006d2.webp\" alt=\"python\u5982\u4f55\u5c06\u5b57\u7b26\u578b\u8f6c\u6362\u4e3alist\u578b\" \/><\/p>\n<p><p> <strong>\u5c06\u5b57\u7b26\u578b\u8f6c\u6362\u4e3alist\u578b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528<code>eval()<\/code>\u51fd\u6570\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u6765\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u7684\u7ed3\u6784\u548c\u9700\u6c42\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u5206\u6790\u5b83\u4eec\u7684\u4f7f\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>list()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u4e4b\u4e00\u3002<code>list()<\/code>\u51fd\u6570\u4f1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u5355\u72ec\u7684\u5143\u7d20\uff0c\u653e\u5165\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>char_list = list(string)<\/p>\n<p>print(char_list)  # \u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u7528\u4e8e\u5c06\u6bcf\u4e2a\u5b57\u7b26\u72ec\u7acb\u5904\u7406\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u4e0d\u80fd\u6309\u7279\u5b9a\u5206\u9694\u7b26\u5206\u5272\uff0c\u53ea\u80fd\u9010\u5b57\u7b26\u5206\u5272\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>split()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e00\u4e2a\u5217\u8868\u3002\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u6309\u7a7a\u683c\u5206\u5272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple,banana,cherry&quot;<\/p>\n<p>fruit_list = string.split(&#39;,&#39;)<\/p>\n<p>print(fruit_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u53ef\u6309\u6307\u5b9a\u5206\u9694\u7b26\u5206\u5272\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6309\u7279\u5b9a\u6a21\u5f0f\u5206\u5272\u5b57\u7b26\u4e32\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u9700\u8981\u786e\u4fdd\u5b57\u7b26\u4e32\u4e2d\u5b58\u5728\u5206\u9694\u7b26\uff0c\u5426\u5219\u4f1a\u8fd4\u56de\u6574\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u5355\u4e2a\u5217\u8868\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6216\u6a21\u5f0f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>char_list = [char for char in string]<\/p>\n<p>print(char_list)  # \u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01\uff0c\u53ef\u8bfb\u6027\u5f3a\u3002<\/li>\n<li>\u53ef\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u8fdb\u884c\u66f4\u590d\u6742\u7684\u8f6c\u6362\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u8f6c\u6362\u573a\u666f\uff0c\u590d\u6742\u903b\u8f91\u53ef\u80fd\u9700\u8981\u66f4\u591a\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>eval()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>eval()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u8868\u8fbe\u5f0f\u5e76\u6267\u884c\u3002\u5982\u679c\u5b57\u7b26\u4e32\u672c\u8eab\u662f\u4e00\u4e2a\u8868\u793a\u5217\u8868\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u76f4\u63a5\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;[1, 2, 3, 4]&quot;<\/p>\n<p>list_from_string = eval(string)<\/p>\n<p>print(list_from_string)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u80fd\u591f\u76f4\u63a5\u5c06\u8868\u793a\u5217\u8868\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b9e\u9645\u5217\u8868\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u5b58\u5728\u5b89\u5168\u9690\u60a3\uff0c\u5bb9\u6613\u88ab\u6076\u610f\u4ee3\u7801\u5229\u7528\uff0c\u4e0d\u63a8\u8350\u5728\u4e0d\u53ef\u4fe1\u6570\u636e\u4e0a\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u5206\u5272<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5206\u5272\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;apple;banana,cherry orange&quot;<\/p>\n<p>pattern = r&#39;[;,\\s]+&#39;<\/p>\n<p>fruit_list = re.split(pattern, string)<\/p>\n<p>print(fruit_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u80fd\u591f\u5904\u7406\u590d\u6742\u7684\u5206\u5272\u6a21\u5f0f\uff0c\u9002\u7528\u4e8e\u590d\u6742\u5b57\u7b26\u4e32\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u590d\u6742\uff0c\u521d\u5b66\u8005\u53ef\u80fd\u96be\u4ee5\u638c\u63e1\u3002<\/li>\n<\/ul>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u578b\u8f6c\u6362\u4e3alist\u578b\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u5b57\u7b26\u4e32\u7684\u7ed3\u6784\u3002<strong>\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u9002\u7528\u4e8e\u9010\u5b57\u7b26\u5206\u5272\uff0c<code>split()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u6309\u5206\u9694\u7b26\u5206\u5272\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u9002\u7528\u4e8e\u7b80\u5355\u548c\u7075\u6d3b\u7684\u8f6c\u6362\uff0c<code>eval()<\/code>\u51fd\u6570\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u8868\u793a\u7684\u5217\u8868\u8f6c\u6362\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u590d\u6742\u5206\u5272\u3002<\/strong>\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u6709\u52a9\u4e8e\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u9009\u62e9\u6700\u4f73\u65b9\u6848\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\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\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>my_string.split(&#39;,&#39;)<\/code>\u53ef\u4ee5\u5c06\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u90fd\u8f6c\u6362\u4e3a\u5217\u8868\u5143\u7d20\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u90fd\u8f6c\u5316\u4e3a\u5217\u8868\u7684\u5143\u7d20\u3002\u53ea\u9700\u8c03\u7528<code>list(my_string)<\/code>\uff0c\u5c31\u4f1a\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u7a7a\u683c\u6216\u7279\u5b9a\u5b57\u7b26\uff1f<\/strong><br \/>\u5728\u8f6c\u6362\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u7a7a\u683c\u6216\u7279\u5b9a\u5b57\u7b26\u3002\u901a\u8fc7\u5728\u8c03\u7528<code>split()<\/code>\u6216<code>list()<\/code>\u4e4b\u524d\u4f7f\u7528<code>my_string.replace(&#39; &#39;, &#39;&#39;)<\/code>\uff0c\u53ef\u4ee5\u53bb\u9664\u7a7a\u683c\uff0c\u786e\u4fdd\u751f\u6210\u7684\u5217\u8868\u66f4\u7b26\u5408\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5217\u8868\u518d\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u3002\u901a\u8fc7<code>&#39;&#39;.join(my_list)<\/code>\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6ce8\u610f\u4e2d\u95f4\u7684\u5206\u9694\u7b26\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8bbe\u7f6e\uff0c\u6bd4\u5982\u7528\u7a7a\u683c\u6216\u9017\u53f7\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06\u5b57\u7b26\u578b\u8f6c\u6362\u4e3alist\u578b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528list()\u51fd\u6570\u3001\u4f7f\u7528split()\u65b9\u6cd5\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1129820,"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\/1129812"}],"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=1129812"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129812\/revisions"}],"predecessor-version":[{"id":1129824,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129812\/revisions\/1129824"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1129820"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1129812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1129812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1129812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}