{"id":1059520,"date":"2024-12-31T15:25:50","date_gmt":"2024-12-31T07:25:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059520.html"},"modified":"2024-12-31T15:25:52","modified_gmt":"2024-12-31T07:25:52","slug":"python%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e8%87%b3list","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1059520.html","title":{"rendered":"python\u5b57\u7b26\u4e32\u5982\u4f55\u4fdd\u5b58\u81f3list"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/29cc52dc-212d-4250-8452-c103f694275c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5b57\u7b26\u4e32\u5982\u4f55\u4fdd\u5b58\u81f3list\" \/><\/p>\n<p><p> <strong>Python\u5b57\u7b26\u4e32\u5982\u4f55\u4fdd\u5b58\u81f3List<\/strong><\/p>\n<\/p>\n<p><p><strong>\u8981\u5c06Python\u5b57\u7b26\u4e32\u4fdd\u5b58\u81f3\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u62c6\u5206\u3001\u76f4\u63a5\u6dfb\u52a0\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5faa\u73af\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\uff0c\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528split()\u65b9\u6cd5\u3001\u4f7f\u7528append()\u65b9\u6cd5\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528split()\u65b9\u6cd5<\/strong>\uff1a<strong>\u5c06\u5b57\u7b26\u4e32\u6309\u7279\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f8b\u5982\u7531\u9017\u53f7\u3001\u7a7a\u683c\u3001\u6362\u884c\u7b26\u7b49\u5206\u9694\u7684\u6587\u672c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528split()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>split()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>split()<\/code>\u65b9\u6cd5\u4f1a\u4f7f\u7528\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u5176\u4ed6\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u7528\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>split(&#39;,&#39;)<\/code>\u6765\u5c06\u5176\u62c6\u5206\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5b57\u7b26\u4e32<\/p>\n<p>string = &quot;apple,banana,cherry,dates&quot;<\/p>\n<h2><strong>\u4f7f\u7528split()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u5e76\u4fdd\u5b58\u5230\u5217\u8868<\/strong><\/h2>\n<p>fruits_list = string.split(&#39;,&#39;)<\/p>\n<p>print(fruits_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;dates&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>string<\/code>\u53d8\u91cf\u5305\u542b\u4e00\u4e2a\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u8c03\u7528<code>string.split(&#39;,&#39;)<\/code>\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u56db\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5b83\u4eec\u4fdd\u5b58\u5230\u5217\u8868<code>fruits_list<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528append()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5e0c\u671b\u9010\u4e2a\u6dfb\u52a0\u5b57\u7b26\u4e32\u5230\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684<code>append()<\/code>\u65b9\u6cd5\u3002<code>append()<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\u3002\u4e0e<code>split()<\/code>\u65b9\u6cd5\u4e0d\u540c\uff0c<code>append()<\/code>\u65b9\u6cd5\u66f4\u7075\u6d3b\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5904\u7406\u52a8\u6001\u751f\u6210\u6216\u9010\u4e2a\u5904\u7406\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>fruits_list = []<\/p>\n<h2><strong>\u6dfb\u52a0\u5b57\u7b26\u4e32\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p>fruits_list.append(&quot;apple&quot;)<\/p>\n<p>fruits_list.append(&quot;banana&quot;)<\/p>\n<p>fruits_list.append(&quot;cherry&quot;)<\/p>\n<p>fruits_list.append(&quot;dates&quot;)<\/p>\n<p>print(fruits_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;dates&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u5217\u8868<code>fruits_list<\/code>\u3002\u7136\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>fruits_list.append()<\/code>\u65b9\u6cd5\uff0c\u5c06\u56db\u4e2a\u5b57\u7b26\u4e32\u4f9d\u6b21\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u751f\u6210\u5217\u8868\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e00\u4e2a\u73b0\u6709\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u4e2d\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u901a\u5e38\u6bd4\u5faa\u73af\u66f4\u7b80\u6d01\u3001\u66f4\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5b57\u7b26\u4e32<\/p>\n<p>string = &quot;apple,banana,cherry,dates&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u5e76\u4fdd\u5b58\u5230\u5217\u8868<\/strong><\/h2>\n<p>fruits_list = [fruit for fruit in string.split(&#39;,&#39;)]<\/p>\n<p>print(fruits_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;dates&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5b57\u7b26\u4e32<code>string<\/code>\u6309\u9017\u53f7\u62c6\u5206\uff0c\u901a\u8fc7<code>string.split(&#39;,&#39;)<\/code>\u751f\u6210\u4e00\u4e2a\u5217\u8868\u3002\u7136\u540e\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<code>[fruit for fruit in string.split(&#39;,&#39;)]<\/code>\u5c06\u62c6\u5206\u540e\u7684\u5b50\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u65b0\u5217\u8868<code>fruits_list<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5904\u7406\u683c\u5f0f\u4e0d\u89c4\u5219\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u529f\u80fd\uff0c\u901a\u8fc7<code>re.split()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6309\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u6a21\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u793a\u4f8b\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>string = &quot;apple1banana2cherry3dates&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u5e76\u4fdd\u5b58\u5230\u5217\u8868<\/strong><\/h2>\n<p>fruits_list = re.split(r&#39;\\d&#39;, string)<\/p>\n<p>print(fruits_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;dates&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>string<\/code>\u53d8\u91cf\u5305\u542b\u4e00\u4e2a\u7528\u6570\u5b57\u5206\u9694\u7684\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u901a\u8fc7\u8c03\u7528<code>re.split(r&#39;\\d&#39;, string)<\/code>\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<code>\\d<\/code>\uff08\u5339\u914d\u4efb\u4f55\u6570\u5b57\uff09\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u56db\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5b83\u4eec\u4fdd\u5b58\u5230\u5217\u8868<code>fruits_list<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528json\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u662fJSON\u683c\u5f0f\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>json<\/code>\u6a21\u5757\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<code>json.loads()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684Python\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u793a\u4f8bJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_string = &#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;dates&quot;]&#39;<\/p>\n<h2><strong>\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>fruits_list = json.loads(json_string)<\/p>\n<p>print(fruits_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;dates&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>json_string<\/code>\u53d8\u91cf\u5305\u542b\u4e00\u4e2aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u8c03\u7528<code>json.loads(json_string)<\/code>\uff0c\u6211\u4eec\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5217\u8868<code>fruits_list<\/code>\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528shlex\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u547d\u4ee4\u884c\u8f93\u5165\u6216\u590d\u6742\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>shlex<\/code>\u6a21\u5757\u3002<code>shlex.split()<\/code>\u65b9\u6cd5\u6309\u7c7b\u4f3c\u4e8eshell\u7684\u8bed\u6cd5\u62c6\u5206\u5b57\u7b26\u4e32\uff0c\u652f\u6301\u5904\u7406\u5e26\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u548c\u8f6c\u4e49\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import shlex<\/p>\n<h2><strong>\u793a\u4f8b\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>string = &#39;apple &quot;banana cherry&quot; dates&#39;<\/p>\n<h2><strong>\u4f7f\u7528shlex\u6a21\u5757\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u5e76\u4fdd\u5b58\u5230\u5217\u8868<\/strong><\/h2>\n<p>fruits_list = shlex.split(string)<\/p>\n<p>print(fruits_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana cherry&#39;, &#39;dates&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>string<\/code>\u53d8\u91cf\u5305\u542b\u4e00\u4e2a\u5e26\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u8c03\u7528<code>shlex.split(string)<\/code>\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u6309shell\u8bed\u6cd5\u62c6\u5206\u6210\u4e09\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5b83\u4eec\u4fdd\u5b58\u5230\u5217\u8868<code>fruits_list<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\uff0c\u518d\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u793a\u4f8b\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>string = &quot;apple 1, banana 2; cherry 3: dates&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206<\/strong><\/h2>\n<p>substrings = re.split(r&#39;[ ,;:]&#39;, string)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53bb\u9664\u7a7a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>fruits_list = [s for s in substrings if s]<\/p>\n<p>print(fruits_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;1&#39;, &#39;banana&#39;, &#39;2&#39;, &#39;cherry&#39;, &#39;3&#39;, &#39;dates&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>string<\/code>\u53d8\u91cf\u5305\u542b\u4e00\u4e2a\u7531\u591a\u79cd\u5206\u9694\u7b26\u5206\u9694\u7684\u5b57\u7b26\u4e32\u3002\u9996\u5148\uff0c\u901a\u8fc7\u8c03\u7528<code>re.split(r&#39;[ ,;:]&#39;, string)<\/code>\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u3001\u9017\u53f7\u3001\u5206\u53f7\u548c\u5192\u53f7\u62c6\u5206\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\u3002\u7136\u540e\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<code>[s for s in substrings if s]<\/code>\u53bb\u9664\u7a7a\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5269\u4f59\u7684\u5b50\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u5217\u8868<code>fruits_list<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8981\u5c06Python\u5b57\u7b26\u4e32\u4fdd\u5b58\u81f3\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<code>split()<\/code>\u65b9\u6cd5\u3001<code>append()<\/code>\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001<code>json<\/code>\u6a21\u5757\u3001<code>shlex<\/code>\u6a21\u5757\u4ee5\u53ca\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5c06\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u5217\u8868\u4e2d\uff0c\u5e76\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u5e0c\u671b\u901a\u8fc7\u4ee5\u4e0a\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u65b9\u62ec\u53f7\u521b\u5efa\u4e00\u4e2a\u5217\u8868\uff0c\u5e76\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u653e\u5728\u5176\u4e2d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u5217\u8868\u4e2d\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&quot;\u5b57\u7b26\u4e321&quot;, &quot;\u5b57\u7b26\u4e322&quot;, &quot;\u5b57\u7b26\u4e323&quot;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u64cd\u4f5c\u548c\u7ba1\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = []\nuser_input = input(&quot;\u8bf7\u8f93\u5165\u5b57\u7b26\u4e32\uff1a&quot;)\nmy_list.append(user_input)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u7528\u6237\u52a8\u6001\u8f93\u5165\u5b57\u7b26\u4e32\u5e76\u4fdd\u5b58\u5728\u5217\u8868\u4e2d\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u5e76\u4fdd\u5b58\u5230\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u8981\u4ece\u4e00\u4e2a\u8f83\u957f\u7684\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u5e76\u4fdd\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u4e00\u4e2a\u4ee5\u7a7a\u683c\u5206\u9694\u7684\u5b57\u7b26\u4e32\u5206\u5272\u5e76\u4fdd\u5b58\u5230\u5217\u8868\u4e2d\uff1a  <\/p>\n<pre><code class=\"language-python\">my_string = &quot;\u8fd9\u662f \u7b2c\u4e00\u4e2a \u5b57\u7b26\u4e32 \u7b2c\u4e8c\u4e2a \u5b57\u7b26\u4e32&quot;\nmy_list = my_string.split(&quot; &quot;)\n<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u7a7a\u683c\u5206\u9694\u5e76\u4fdd\u5b58\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u5230\u5217\u8868\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b57\u7b26\u4e32\u5982\u4f55\u4fdd\u5b58\u81f3List \u8981\u5c06Python\u5b57\u7b26\u4e32\u4fdd\u5b58\u81f3\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u62c6\u5206\u3001\u76f4 [&hellip;]","protected":false},"author":3,"featured_media":1059525,"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\/1059520"}],"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=1059520"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059520\/revisions"}],"predecessor-version":[{"id":1059527,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059520\/revisions\/1059527"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1059525"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1059520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1059520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1059520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}