{"id":1142735,"date":"2025-01-08T22:43:20","date_gmt":"2025-01-08T14:43:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142735.html"},"modified":"2025-01-08T22:43:22","modified_gmt":"2025-01-08T14:43:22","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%80%e8%a1%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8f%98%e6%88%90%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1142735.html","title":{"rendered":"python\u5982\u4f55\u5c06\u4e00\u884c\u5b57\u7b26\u4e32\u53d8\u6210\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180717\/f996ef10-5b0b-462f-8582-c7562be79bd1.webp\" alt=\"python\u5982\u4f55\u5c06\u4e00\u884c\u5b57\u7b26\u4e32\u53d8\u6210\u5217\u8868\" \/><\/p>\n<p><p> <strong>Python\u5c06\u4e00\u884c\u5b57\u7b26\u4e32\u53d8\u6210\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca\u5176\u4ed6\u5de5\u5177\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1asplit()\u3001list()\u3001re.split()\u3002\u5177\u4f53\u65b9\u6cd5\u4f1a\u56e0\u5b57\u7b26\u4e32\u7684\u7ed3\u6784\u548c\u9700\u6c42\u800c\u5f02\u3002\u4ee5\u4e0b\u662f\u5bf9split()\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>split()\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u548c\u7b80\u5355\u7684\u65b9\u5f0f<\/strong>\u3002\u5b83\u901a\u8fc7\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e00\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u884c\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5355\u8bcd\u4e4b\u95f4\u7528\u7a7a\u683c\u9694\u5f00\uff0c\u53ef\u4ee5\u4f7f\u7528split()\u65b9\u6cd5\u5c06\u5176\u5206\u5272\u6210\u5355\u8bcd\u5217\u8868\u3002split()\u65b9\u6cd5\u6ca1\u6709\u53c2\u6570\u65f6\uff0c\u9ed8\u8ba4\u6309\u7a7a\u683c\u5206\u5272\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u63cf\u8ff0\u548c\u5176\u4ed6\u65b9\u6cd5\u7684\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528split()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>split()\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u80fd\u591f\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\u5217\u8868\u3002\u5176\u7528\u6cd5\u7b80\u5355\u4e14\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f8b\u5b50<\/p>\n<p>string = &quot;Python is an amazing programming language&quot;<\/p>\n<p>words_list = string.split()<\/p>\n<p>print(words_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>string.split()<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u5206\u5272\uff0c\u751f\u6210\u4e00\u4e2a\u5355\u8bcd\u5217\u8868\uff1a<code>[&#39;Python&#39;, &#39;is&#39;, &#39;an&#39;, &#39;amazing&#39;, &#39;programming&#39;, &#39;language&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u6307\u5b9a\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><p>split()\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5206\u9694\u7b26\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u7684\u5355\u8bcd\u662f\u7528\u9017\u53f7\u5206\u9694\u7684\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python,is,an,amazing,programming,language&quot;<\/p>\n<p>words_list = string.split(&#39;,&#39;)<\/p>\n<p>print(words_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>string.split(&#39;,&#39;)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u6309\u9017\u53f7\u5206\u5272\uff0c\u751f\u6210\u4e00\u4e2a\u5355\u8bcd\u5217\u8868\uff1a<code>[&#39;Python&#39;, &#39;is&#39;, &#39;an&#39;, &#39;amazing&#39;, &#39;programming&#39;, &#39;language&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528list()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>list()\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u5316\u4e3a\u4e00\u4e2a\u5217\u8868\u5143\u7d20\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u6309\u5b57\u7b26\u5206\u5272\u800c\u4e0d\u662f\u6309\u5355\u8bcd\u5206\u5272\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>char_list = list(string)<\/p>\n<p>print(char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>list(string)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u6309\u5b57\u7b26\u5206\u5272\uff0c\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u5217\u8868\uff1a<code>[&#39;P&#39;, &#39;y&#39;, &#39;t&#39;, &#39;h&#39;, &#39;o&#39;, &#39;n&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0fre.split()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u5206\u5272\u89c4\u5219\u3002re.split()\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u590d\u6742\u7684\u6a21\u5f0f\u8fdb\u884c\u5206\u5272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Python, is an; amazing: programming language&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\uff0c\u591a\u4e2a\u5206\u9694\u7b26<\/strong><\/h2>\n<p>words_list = re.split(r&#39;[;,\\s:]+&#39;, string)<\/p>\n<p>print(words_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>re.split(r&#39;[;,\\s:]+&#39;, string)<\/code>\u4f1a\u6839\u636e\u9017\u53f7\u3001\u7a7a\u683c\u3001\u5206\u53f7\u548c\u5192\u53f7\u7b49\u591a\u4e2a\u5206\u9694\u7b26\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u751f\u6210\u4e00\u4e2a\u5355\u8bcd\u5217\u8868\uff1a<code>[&#39;Python&#39;, &#39;is&#39;, &#39;an&#39;, &#39;amazing&#39;, &#39;programming&#39;, &#39;language&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548cjoin()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u4e00\u4e9b\u9884\u5904\u7406\u518d\u8fdb\u884c\u5206\u5272\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548cjoin()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python is an amazing programming language&quot;<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u6bcd\u8f6c\u4e3a\u5927\u5199\u540e\u518d\u5206\u5272<\/strong><\/h2>\n<p>words_list = string.upper().split()<\/p>\n<p>print(words_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>string.upper().split()<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5927\u5199\u540e\u6309\u7a7a\u683c\u5206\u5272\uff0c\u751f\u6210\u4e00\u4e2a\u5927\u5199\u5355\u8bcd\u5217\u8868\uff1a<code>[&#39;PYTHON&#39;, &#39;IS&#39;, &#39;AN&#39;, &#39;AMAZING&#39;, &#39;PROGRAMMING&#39;, &#39;LANGUAGE&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5b57\u7b26\u4e32\u4e2d\u53ef\u80fd\u5305\u542b\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u6216\u683c\u5f0f\uff0c\u9700\u8981\u5148\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53bb\u6389\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u6216\u8005\u53bb\u9664\u7279\u5b9a\u7684\u524d\u7f00\u6216\u540e\u7f00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;   Python is an amazing programming language   &quot;<\/p>\n<h2><strong>\u53bb\u9664\u4e24\u7aef\u7a7a\u767d\u5b57\u7b26\u540e\u5206\u5272<\/strong><\/h2>\n<p>words_list = string.strip().split()<\/p>\n<p>print(words_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>string.strip().split()<\/code>\u4f1a\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u540e\u6309\u7a7a\u683c\u5206\u5272\uff0c\u751f\u6210\u4e00\u4e2a\u5355\u8bcd\u5217\u8868\uff1a<code>[&#39;Python&#39;, &#39;is&#39;, &#39;an&#39;, &#39;amazing&#39;, &#39;programming&#39;, &#39;language&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7efc\u5408\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5206\u5272\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5904\u7406\u5305\u542b\u4e0d\u540c\u5206\u9694\u7b26\u548c\u524d\u540e\u7f00\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;   Python, is an; amazing: programming language   &quot;<\/p>\n<h2><strong>\u53bb\u9664\u4e24\u7aef\u7a7a\u767d\u5b57\u7b26\u540e\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272<\/strong><\/h2>\n<p>words_list = re.split(r&#39;[;,\\s:]+&#39;, string.strip())<\/p>\n<p>print(words_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>string.strip()<\/code>\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u7136\u540e<code>re.split(r&#39;[;,\\s:]+&#39;, string.strip())<\/code>\u4f1a\u6839\u636e\u591a\u4e2a\u5206\u9694\u7b26\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u751f\u6210\u4e00\u4e2a\u5355\u8bcd\u5217\u8868\uff1a<code>[&#39;Python&#39;, &#39;is&#39;, &#39;an&#39;, &#39;amazing&#39;, &#39;programming&#39;, &#39;language&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u51e0\u4e4e\u53ef\u4ee5\u5904\u7406\u4efb\u4f55\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\u5206\u5272\u9700\u6c42\u3002\u6bcf\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u65f6\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u6765\u786e\u5b9a\u6700\u4f73\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u901a\u8fc7\u6307\u5b9a\u7684\u5206\u9694\u7b26\u6765\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u9ed8\u8ba4\u4f7f\u7528\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c<code>&quot;a b c&quot;.split()<\/code>\u5c06\u8fd4\u56de<code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\u3002\u5982\u679c\u4f60\u7684\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u5176\u4ed6\u5b57\u7b26\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u6bd4\u5982\u9017\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528<code>&quot;a,b,c&quot;.split(&#39;,&#39;)<\/code>\u5f97\u5230<code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5e26\u6709\u591a\u4e2a\u5206\u9694\u7b26\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5f53\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u591a\u4e2a\u5206\u9694\u7b26\uff0c\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>split()<\/code>\u51fd\u6570\u975e\u5e38\u6709\u6548\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u4f60\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9a\u4e49\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c<code>import re<\/code>\u540e\uff0c\u4f7f\u7528<code>re.split(r&#39;[ ,;]&#39;, &#39;a,b; c d&#39;)<\/code>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210<code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/code>\uff0c\u540c\u65f6\u5904\u7406\u9017\u53f7\u3001\u5206\u53f7\u548c\u7a7a\u683c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5305\u542b\u6574\u6570\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff0c\u53ef\u4ee5\u7ed3\u5408<code>split()<\/code>\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u5148\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u8f6c\u6362\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>numbers = &quot;1 2 3&quot;.split()<\/code>\u53ef\u4ee5\u5f97\u5230<code>[&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]<\/code>\uff0c\u63a5\u7740\u4f7f\u7528<code>[int(num) for num in numbers]<\/code>\u5f97\u5230\u6574\u6570\u5217\u8868<code>[1, 2, 3]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u4ee5\u7a7a\u683c\u6216\u7279\u5b9a\u5206\u9694\u7b26\u5206\u9694\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u4e00\u884c\u5b57\u7b26\u4e32\u53d8\u6210\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca\u5176\u4ed6\u5de5\u5177\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709 [&hellip;]","protected":false},"author":3,"featured_media":1142745,"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\/1142735"}],"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=1142735"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142735\/revisions"}],"predecessor-version":[{"id":1142746,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142735\/revisions\/1142746"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1142745"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1142735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1142735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1142735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}