{"id":1186149,"date":"2025-01-15T19:49:19","date_gmt":"2025-01-15T11:49:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186149.html"},"modified":"2025-01-15T19:49:22","modified_gmt":"2025-01-15T11:49:22","slug":"python%e5%a6%82%e4%bd%95%e6%8c%89%e5%9b%ba%e5%ae%9a%e7%ac%a6%e5%8f%b7%e5%88%86%e5%89%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1186149.html","title":{"rendered":"Python\u5982\u4f55\u6309\u56fa\u5b9a\u7b26\u53f7\u5206\u5272"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135032\/3658602d-bb1c-4abd-a903-84d4132c96f4.webp\" alt=\"Python\u5982\u4f55\u6309\u56fa\u5b9a\u7b26\u53f7\u5206\u5272\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>str.split()<\/code>\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684<code>re.split()<\/code>\u65b9\u6cd5\u3001\u5206\u5272\u5b57\u7b26\u4e32\u6a21\u5757<code>str.partition()<\/code>\u7b49\u65b9\u5f0f\u6309\u56fa\u5b9a\u7b26\u53f7\u8fdb\u884c\u5206\u5272<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>str.split()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.split()<\/code>\u662fPython\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e4b\u4e00\uff0c\u53ef\u4ee5\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u4f1a\u4ee5\u7a7a\u683c\u4e3a\u5206\u9694\u7b26\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u4f20\u5165\u5176\u4ed6\u4efb\u610f\u7b26\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u9017\u53f7\u5206\u5272<\/p>\n<p>text = &quot;apple,banana,cherry&quot;<\/p>\n<p>result = text.split(&#39;,&#39;)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u6309\u7a7a\u683c\u5206\u5272<\/strong><\/h2>\n<p>text = &quot;apple banana cherry&quot;<\/p>\n<p>result = text.split(&#39; &#39;)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>str.split()<\/code>\u65b9\u6cd5\u4f1a\u6839\u636e\u4f20\u5165\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e00\u4e2a\u5217\u8868\u3002\u5982\u679c\u6ca1\u6709\u4f20\u5165\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4f7f\u7528\u7a7a\u683c\u3002\u8be5\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u53ef\u9009\u53c2\u6570<code>maxsplit<\/code>\uff0c\u7528\u4e8e\u6307\u5b9a\u6700\u5927\u5206\u5272\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u9017\u53f7\u5206\u5272\uff0c\u5e76\u6307\u5b9a\u6700\u5927\u5206\u5272\u6b21\u6570<\/p>\n<p>text = &quot;apple,banana,cherry,orange&quot;<\/p>\n<p>result = text.split(&#39;,&#39;, 2)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry,orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<code>re.split()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5206\u5272\u9700\u6c42\uff0c<code>re.split()<\/code>\u65b9\u6cd5\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u4ece\u800c\u5b9e\u73b0\u7075\u6d3b\u7684\u5206\u5272\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u6309\u9017\u53f7\u6216\u5206\u53f7\u5206\u5272<\/strong><\/h2>\n<p>text = &quot;apple,banana;cherry&quot;<\/p>\n<p>result = re.split(r&#39;[;,]&#39;, text)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u6309\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u683c\u5206\u5272<\/strong><\/h2>\n<p>text = &quot;apple   banana cherry&quot;<\/p>\n<p>result = re.split(r&#39;\\s+&#39;, text)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>re.split()<\/code>\u65b9\u6cd5\u5141\u8bb8\u4f60\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u590d\u6742\u7684\u5206\u5272\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u96c6<code>[]<\/code>\u6765\u6307\u5b9a\u591a\u4e2a\u5206\u9694\u7b26\uff0c\u4f7f\u7528<code>\\s+<\/code>\u6765\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u683c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u591a\u4e2a\u7b26\u53f7\u5206\u5272<\/p>\n<p>text = &quot;apple, banana; cherry|orange&quot;<\/p>\n<p>result = re.split(r&#39;[;,\\| ]+&#39;, text)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>str.partition()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.partition()<\/code>\u65b9\u6cd5\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e09\u4e2a\u90e8\u5206\uff1a\u5206\u9694\u7b26\u524d\u7684\u90e8\u5206\u3001\u5206\u9694\u7b26\u672c\u8eab\u3001\u5206\u9694\u7b26\u540e\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u7b2c\u4e00\u4e2a\u9017\u53f7\u5206\u5272<\/p>\n<p>text = &quot;apple,banana,cherry&quot;<\/p>\n<p>result = text.partition(&#39;,&#39;)<\/p>\n<p>print(result)  # \u8f93\u51fa: (&#39;apple&#39;, &#39;,&#39;, &#39;banana,cherry&#39;)<\/p>\n<h2><strong>\u6309\u7b2c\u4e00\u4e2a\u7a7a\u683c\u5206\u5272<\/strong><\/h2>\n<p>text = &quot;apple banana cherry&quot;<\/p>\n<p>result = text.partition(&#39; &#39;)<\/p>\n<p>print(result)  # \u8f93\u51fa: (&#39;apple&#39;, &#39; &#39;, &#39;banana cherry&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>str.partition()<\/code>\u65b9\u6cd5\u53ea\u4f1a\u8fdb\u884c\u4e00\u6b21\u5206\u5272\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5bf9\u4e8e\u9700\u8981\u4fdd\u7559\u5206\u9694\u7b26\u5e76\u8fdb\u884c\u4e00\u6b21\u5206\u5272\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>str.rpartition()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.rpartition()<\/code>\u65b9\u6cd5\u4e0e<code>str.partition()<\/code>\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4ece\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u5f00\u59cb\u67e5\u627e\u5206\u9694\u7b26\uff0c\u5e76\u8fd4\u56de\u4e09\u4e2a\u90e8\u5206\uff1a\u5206\u9694\u7b26\u524d\u7684\u90e8\u5206\u3001\u5206\u9694\u7b26\u672c\u8eab\u3001\u5206\u9694\u7b26\u540e\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u6700\u540e\u4e00\u4e2a\u9017\u53f7\u5206\u5272<\/p>\n<p>text = &quot;apple,banana,cherry&quot;<\/p>\n<p>result = text.rpartition(&#39;,&#39;)<\/p>\n<p>print(result)  # \u8f93\u51fa: (&#39;apple,banana&#39;, &#39;,&#39;, &#39;cherry&#39;)<\/p>\n<h2><strong>\u6309\u6700\u540e\u4e00\u4e2a\u7a7a\u683c\u5206\u5272<\/strong><\/h2>\n<p>text = &quot;apple banana cherry&quot;<\/p>\n<p>result = text.rpartition(&#39; &#39;)<\/p>\n<p>print(result)  # \u8f93\u51fa: (&#39;apple banana&#39;, &#39; &#39;, &#39;cherry&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>str.rpartition()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u4ece\u53f3\u4fa7\u5f00\u59cb\u67e5\u627e\u5206\u9694\u7b26\u5e76\u8fdb\u884c\u5206\u5272\u7684\u573a\u666f\u3002\u5b83\u4e0e<code>str.partition()<\/code>\u7684\u533a\u522b\u5728\u4e8e\u5206\u5272\u7684\u65b9\u5411\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>str.splitlines()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.splitlines()<\/code>\u65b9\u6cd5\u7528\u4e8e\u6309\u7167\u884c\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u3002\u5b83\u53ef\u4ee5\u8bc6\u522b\u591a\u79cd\u884c\u5206\u9694\u7b26\uff0c\u5305\u62ec<code>\\n<\/code>\u3001<code>\\r<\/code>\u548c<code>\\r\\n<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u884c\u5206\u9694\u7b26\u5206\u5272<\/p>\n<p>text = &quot;apple\\nbanana\\rcherry\\r\\norange&quot;<\/p>\n<p>result = text.splitlines()<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<h2><strong>\u4fdd\u7559\u884c\u5206\u9694\u7b26<\/strong><\/h2>\n<p>result = text.splitlines(keepends=True)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;apple\\n&#39;, &#39;banana\\r&#39;, &#39;cherry\\r\\n&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>str.splitlines()<\/code>\u65b9\u6cd5\u5bf9\u4e8e\u6309\u884c\u5206\u5272\u6587\u672c\u975e\u5e38\u6709\u7528\u3002\u53ef\u4ee5\u901a\u8fc7\u4f20\u5165<code>keepends=True<\/code>\u53c2\u6570\u6765\u4fdd\u7559\u884c\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u6761\u4ef6\u8bed\u53e5\u5206\u5272\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u9017\u53f7\u548c\u7a7a\u683c\u5206\u5272\uff0c\u5e76\u53bb\u6389\u7a7a\u5b57\u7b26\u4e32<\/p>\n<p>text = &quot;apple, banana, ,cherry&quot;<\/p>\n<p>result = [s.strip() for s in text.split(&#39;,&#39;) if s.strip()]<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u5728\u5206\u5272\u5b57\u7b26\u4e32\u7684\u540c\u65f6\u8fdb\u884c\u6570\u636e\u6e05\u6d17\uff0c\u4f8b\u5982\u53bb\u9664\u7a7a\u5b57\u7b26\u4e32\u6216\u591a\u4f59\u7684\u7a7a\u683c\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5206\u5272\u7ed3\u679c\u8fdb\u884c\u989d\u5916\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528<code>str.split()<\/code>\u7ed3\u5408<code>map()<\/code>\u51fd\u6570\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5206\u5272\u5b57\u7b26\u4e32\u4e4b\u540e\uff0c\u53ef\u4ee5\u7ed3\u5408<code>map()<\/code>\u51fd\u6570\u5bf9\u5206\u5272\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u4f8b\u5982\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u9017\u53f7\u5206\u5272\uff0c\u5e76\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>text = &quot;1, 2, 3, 4, 5&quot;<\/p>\n<p>result = list(map(int, text.split(&#39;,&#39;)))<\/p>\n<p>print(result)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u6309\u7a7a\u683c\u5206\u5272\uff0c\u5e76\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>text = &quot;1.1 2.2 3.3 4.4 5.5&quot;<\/p>\n<p>result = list(map(float, text.split()))<\/p>\n<p>print(result)  # \u8f93\u51fa: [1.1, 2.2, 3.3, 4.4, 5.5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408<code>map()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u5206\u5272\u5b57\u7b26\u4e32\u7684\u540c\u65f6\u5bf9\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5206\u5272\u7ed3\u679c\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u6216\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5206\u5272\u7ed3\u679c\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528<code>str.split()<\/code>\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u8fdb\u884c\u5927\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6570\u636e\u5904\u7406\u573a\u666f\uff0c\u53ef\u4ee5\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u5185\u5b58\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u5206\u5272\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u9017\u53f7\u5206\u5272\uff0c\u5e76\u9010\u4e2a\u5904\u7406<\/p>\n<p>text = &quot;apple, banana, cherry, orange&quot;<\/p>\n<p>result = (s.strip() for s in text.split(&#39;,&#39;))<\/p>\n<p>for item in result:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5728\u5206\u5272\u5b57\u7b26\u4e32\u7684\u540c\u65f6\u5b9e\u73b0\u60f0\u6027\u6c42\u503c\uff0c\u4ece\u800c\u63d0\u9ad8\u5185\u5b58\u5229\u7528\u7387\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5927\u6570\u636e\u5904\u7406\u573a\u666f\uff0c\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u5927\u91cf\u6570\u636e\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528<code>str.split()<\/code>\u7ed3\u5408<code>itertools<\/code>\u6a21\u5757\u8fdb\u884c\u590d\u6742\u5206\u5272<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u5206\u5272\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408<code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u5de5\u5177\u8fdb\u884c\u9ad8\u7ea7\u5206\u5272\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u6309\u9017\u53f7\u548c\u7a7a\u683c\u5206\u5272\uff0c\u5e76\u6309\u957f\u5ea6\u5206\u7ec4<\/strong><\/h2>\n<p>text = &quot;apple, banana, cherry, orange&quot;<\/p>\n<p>result = [list(group) for key, group in itertools.groupby(text.split(&#39;, &#39;), key=len)]<\/p>\n<p>print(result)  # \u8f93\u51fa: [[&#39;apple&#39;], [&#39;banana&#39;, &#39;cherry&#39;], [&#39;orange&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408<code>itertools<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u5206\u5272\u548c\u5206\u7ec4\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6309\u7167\u5206\u5272\u7ed3\u679c\u7684\u957f\u5ea6\u8fdb\u884c\u5206\u7ec4\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u9ad8\u7ea7\u7684\u6570\u636e\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5206\u5272\u7ed3\u679c\u8fdb\u884c\u590d\u6742\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6309\u56fa\u5b9a\u7b26\u53f7\u5206\u5272\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u5206\u5272\u548c\u5904\u7406<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h4>\u4e3b\u8981\u65b9\u6cd5\u603b\u7ed3\uff1a<\/h4>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>str.split()<\/code>\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5206\u5272\u9700\u6c42\uff0c\u652f\u6301\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u548c\u6700\u5927\u5206\u5272\u6b21\u6570\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<code>re.split()<\/code>\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u590d\u6742\u7684\u5206\u5272\u9700\u6c42\uff0c\u652f\u6301\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\u5206\u9694\u7b26\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>str.partition()<\/code>\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u7559\u5206\u9694\u7b26\u5e76\u8fdb\u884c\u4e00\u6b21\u5206\u5272\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>str.rpartition()<\/code>\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u4ece\u53f3\u4fa7\u5f00\u59cb\u67e5\u627e\u5206\u9694\u7b26\u5e76\u8fdb\u884c\u5206\u5272\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>str.splitlines()<\/code>\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u6309\u884c\u5206\u5272\u6587\u672c\u3002<\/li>\n<li><strong>\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u6761\u4ef6\u8bed\u53e5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5206\u5272\u7ed3\u679c\u8fdb\u884c\u989d\u5916\u5904\u7406\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u7ed3\u5408<code>map()<\/code>\u51fd\u6570\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5206\u5272\u7ed3\u679c\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u8fdb\u884c\u5927\u6570\u636e\u5904\u7406<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u6570\u636e\u5904\u7406\u573a\u666f\u3002<\/li>\n<li><strong>\u7ed3\u5408<code>itertools<\/code>\u6a21\u5757\u8fdb\u884c\u590d\u6742\u5206\u5272<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5206\u5272\u7ed3\u679c\u8fdb\u884c\u590d\u6742\u5904\u7406\u7684\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u89e3\u51b3\u5404\u79cd\u5b57\u7b26\u4e32\u5206\u5272\u95ee\u9898\uff0c\u4ece\u800c\u6ee1\u8db3\u4e0d\u540c\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u5206\u5272\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u5206\u5272\u65b9\u6cd5\u662f\u4f7f\u7528<code>split()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>string.split(&#39;,&#39;)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u6309\u9017\u53f7\u5206\u5272\u3002\u5982\u679c\u4e0d\u4f20\u9012\u53c2\u6570\uff0c\u9ed8\u8ba4\u4f1a\u6309\u7a7a\u683c\u5206\u5272\u3002\u6b64\u5916\uff0c<code>re.split()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5206\u9694\u60c5\u51b5\uff0c\u6bd4\u5982\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5206\u5272\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32\u5217\u8868\uff1f<\/strong><br \/>\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32\u4f1a\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684\u7d22\u5f15\u8bbf\u95ee\u7279\u5b9a\u7684\u5143\u7d20\uff0c\u6216\u8005\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6574\u4e2a\u5217\u8868\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for item in string_list:<\/code>\u6765\u9010\u4e2a\u5904\u7406\u6bcf\u4e2a\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32\u3002\u9664\u4e86\u57fa\u672c\u7684\u7d22\u5f15\u64cd\u4f5c\uff0c\u5217\u8868\u8fd8\u652f\u6301\u591a\u79cd\u65b9\u6cd5\uff0c\u6bd4\u5982<code>append()<\/code>\u3001<code>remove()<\/code>\u548c<code>sort()<\/code>\u7b49\uff0c\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5206\u5272\u65f6\u51fa\u73b0\u7684\u7a7a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u5206\u9694\u7b26\u8fde\u7eed\u51fa\u73b0\uff0c\u4f1a\u5bfc\u81f4\u751f\u6210\u7a7a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>&#39;a,,b&#39;.split(&#39;,&#39;)<\/code>\u4f1a\u8fd4\u56de<code>[&#39;a&#39;, &#39;&#39;, &#39;b&#39;]<\/code>\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u5206\u5272\u65f6\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u6765\u53bb\u9664\u7a7a\u5b57\u7b26\u4e32\uff0c\u6216\u8005\u5728\u8c03\u7528<code>split()<\/code>\u65f6\u4f7f\u7528<code>split(&#39;,&#39;, 1)<\/code>\u6765\u9650\u5236\u5206\u5272\u6b21\u6570\uff0c\u4ece\u800c\u907f\u514d\u591a\u4f59\u7684\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528str.split()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684re.split()\u65b9\u6cd5\u3001\u5206\u5272\u5b57\u7b26\u4e32\u6a21\u5757st [&hellip;]","protected":false},"author":3,"featured_media":1186154,"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\/1186149"}],"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=1186149"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186149\/revisions"}],"predecessor-version":[{"id":1186158,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186149\/revisions\/1186158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1186154"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1186149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1186149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1186149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}