{"id":1042822,"date":"2024-12-31T13:00:35","date_gmt":"2024-12-31T05:00:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1042822.html"},"modified":"2024-12-31T13:00:38","modified_gmt":"2024-12-31T05:00:38","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8e%bb%e6%8e%89%e8%be%93%e5%87%ba%e6%9c%ab%e5%b0%be%e7%9a%84%e9%80%97%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1042822.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u53bb\u6389\u8f93\u51fa\u672b\u5c3e\u7684\u9017\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/db5f73f7-e8b1-481e-9702-287f98f1a6e3.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u53bb\u6389\u8f93\u51fa\u672b\u5c3e\u7684\u9017\u53f7\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u53bb\u6389\u8f93\u51fa\u672b\u5c3e\u7684\u9017\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u4e8c\u3001\u4f7f\u7528join\u65b9\u6cd5\u3001\u4e09\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u6709\u6548\u5730\u53bb\u9664\u5b57\u7b26\u4e32\u6216\u5217\u8868\u4e2d\u7684\u672b\u5c3e\u9017\u53f7\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u2014\u2014\u4f7f\u7528join\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528join\u65b9\u6cd5<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u4fbf\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4f7f\u7528\u67d0\u4e2a\u5206\u9694\u7b26\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u5730\u9009\u62e9\u662f\u5426\u5728\u672b\u5c3e\u6dfb\u52a0\u4efb\u4f55\u7b26\u53f7\u3002\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = [1, 2, 3, 4, 5]<\/p>\n<p>output = &#39;, &#39;.join(map(str, items))<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>map(str, items)<\/code>\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c<code>join<\/code>\u65b9\u6cd5\u4f7f\u7528<code>, <\/code>\u4f5c\u4e3a\u5206\u9694\u7b26\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u6700\u7ec8\u751f\u6210\u4e86\u4e00\u4e2a\u6ca1\u6709\u672b\u5c3e\u9017\u53f7\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u66f4\u591a\u7684\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u53bb\u9664\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u6307\u5b9a\u5b57\u7b26\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">output = &quot;1, 2, 3, 4, 5,&quot;<\/p>\n<p>if output.endswith(&#39;,&#39;):<\/p>\n<p>    output = output[:-1]<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>output.endswith(&#39;,&#39;)<\/code>\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u9017\u53f7\u7ed3\u5c3e\uff0c\u5982\u679c\u662f\uff0c\u5219\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u7b26<code>[:-1]<\/code>\u53bb\u9664\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5904\u7406\u56fa\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = [1, 2, 3, 4, 5]<\/p>\n<p>output = &#39;, &#39;.join(map(str, items))<\/p>\n<p>formatted_output = f&quot;{output.rstrip(&#39;,&#39;)}&quot;<\/p>\n<p>print(formatted_output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>f&quot;{output.rstrip(&#39;,&#39;)}&quot;<\/code>\u4f7f\u7528<code>f-string<\/code>\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u5e76\u901a\u8fc7<code>rstrip(&#39;,&#39;)<\/code>\u53bb\u9664\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u9017\u53f7\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u5b57\u7b26\u4e32\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\u548c\u66ff\u6362\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>output = &quot;1, 2, 3, 4, 5,&quot;<\/p>\n<p>output = re.sub(r&#39;,$&#39;, &#39;&#39;, output)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>re.sub(r&#39;,$&#39;, &#39;&#39;, output)<\/code>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u9017\u53f7\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u5b57\u7b26\u4e32\u5339\u914d\u548c\u66ff\u6362\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u751f\u6210\u5217\u8868\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u5217\u8868\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = [1, 2, 3, 4, 5]<\/p>\n<p>output = &#39;, &#39;.join(str(item) for item in items)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u751f\u6210\u5f0f<code>str(item) for item in items<\/code>\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u5e76\u901a\u8fc7<code>join<\/code>\u65b9\u6cd5\u4f7f\u7528<code>, <\/code>\u4f5c\u4e3a\u5206\u9694\u7b26\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u6700\u7ec8\u751f\u6210\u4e86\u4e00\u4e2a\u6ca1\u6709\u672b\u5c3e\u9017\u53f7\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u5217\u8868\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u7b97\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u8bb8\u591a\u590d\u6742\u7684\u95ee\u9898\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ling_comma(output):<\/p>\n<p>    if output.endswith(&#39;,&#39;):<\/p>\n<p>        return remove_trailing_comma(output[:-1])<\/p>\n<p>    return output<\/p>\n<p>output = &quot;1, 2, 3, 4, 5,&quot;<\/p>\n<p>output = remove_trailing_comma(output)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9012\u5f52\u65b9\u6cd5<code>remove_trailing_comma<\/code>\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u9017\u53f7\u7ed3\u5c3e\uff0c\u5982\u679c\u662f\uff0c\u5219\u9012\u5f52\u8c03\u7528\u81ea\u8eab\u53bb\u9664\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u76f4\u5230\u5b57\u7b26\u4e32\u4e0d\u518d\u4ee5\u9017\u53f7\u7ed3\u5c3e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u5b57\u7b26\u4e32\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u751f\u6210\u5e8f\u5217\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u5e8f\u5217\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_items():<\/p>\n<p>    items = [1, 2, 3, 4, 5]<\/p>\n<p>    for item in items:<\/p>\n<p>        yield str(item)<\/p>\n<p>output = &#39;, &#39;.join(generate_items())<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u5668<code>generate_items<\/code>\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e8f\u5217\uff0c\u5e76\u901a\u8fc7<code>join<\/code>\u65b9\u6cd5\u4f7f\u7528<code>, <\/code>\u4f5c\u4e3a\u5206\u9694\u7b26\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u6700\u7ec8\u751f\u6210\u4e86\u4e00\u4e2a\u6ca1\u6709\u672b\u5c3e\u9017\u53f7\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u5e8f\u5217\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528itertools\u5e93<\/h3>\n<\/p>\n<p><p>itertools\u5e93\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7528\u4e8e\u9ad8\u6548\u5904\u7406\u8fed\u4ee3\u5668\u7684\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>items = [1, 2, 3, 4, 5]<\/p>\n<p>output = &#39;, &#39;.join(map(str, itertools.chain(items)))<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>itertools.chain(items)<\/code>\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5e76\u901a\u8fc7<code>map(str, ...)<\/code>\u5c06\u8fed\u4ee3\u5668\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6700\u7ec8\u901a\u8fc7<code>join<\/code>\u65b9\u6cd5\u4f7f\u7528<code>, <\/code>\u4f5c\u4e3a\u5206\u9694\u7b26\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u751f\u6210\u4e86\u4e00\u4e2a\u6ca1\u6709\u672b\u5c3e\u9017\u53f7\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u8fed\u4ee3\u5668\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u5206\u9694\u7b26<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u6765\u63a7\u5236\u5b57\u7b26\u4e32\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = [1, 2, 3, 4, 5]<\/p>\n<p>separator = &#39;, &#39;<\/p>\n<p>output = separator.join(map(str, items))<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u81ea\u5b9a\u4e49\u5206\u9694\u7b26<code>separator<\/code>\u7528\u4e8e\u8fde\u63a5\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u6700\u7ec8\u751f\u6210\u4e86\u4e00\u4e2a\u6ca1\u6709\u672b\u5c3e\u9017\u53f7\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u5b57\u7b26\u4e32replace\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u7684replace\u65b9\u6cd5\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u9ad8\u6548\u5730\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">output = &quot;1, 2, 3, 4, 5,&quot;<\/p>\n<p>output = output.replace(&#39;, &#39;, &#39; &#39;, output.count(&#39;,&#39;) - 1)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>output.replace(&#39;, &#39;, &#39; &#39;, output.count(&#39;,&#39;) - 1)<\/code>\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u9017\u53f7\u66ff\u6362\u4e3a\u7a7a\u683c\uff0c\u6700\u7ec8\u751f\u6210\u4e86\u4e00\u4e2a\u6ca1\u6709\u672b\u5c3e\u9017\u53f7\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u5b57\u7b26\u4e32\u66ff\u6362\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u5b57\u7b26\u4e32strip\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u7684strip\u65b9\u6cd5\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u9ad8\u6548\u5730\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u6307\u5b9a\u5b57\u7b26\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">output = &quot;1, 2, 3, 4, 5,&quot;<\/p>\n<p>output = output.rstrip(&#39;,&#39;)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>output.rstrip(&#39;,&#39;)<\/code>\u7528\u4e8e\u53bb\u9664\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u9017\u53f7\uff0c\u6700\u7ec8\u751f\u6210\u4e86\u4e00\u4e2a\u6ca1\u6709\u672b\u5c3e\u9017\u53f7\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u7b80\u5355\u5b57\u7b26\u4e32\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32partition\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u7684partition\u65b9\u6cd5\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u9ad8\u6548\u5730\u5206\u5272\u5b57\u7b26\u4e32\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">output = &quot;1, 2, 3, 4, 5,&quot;<\/p>\n<p>before, sep, after = output.rpartition(&#39;,&#39;)<\/p>\n<p>output = before if after == &#39;&#39; else output<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>output.rpartition(&#39;,&#39;)<\/code>\u7528\u4e8e\u5206\u5272\u5b57\u7b26\u4e32\uff0c<code>before<\/code>\u4e3a\u5206\u5272\u7b26\u4e4b\u524d\u7684\u90e8\u5206\uff0c<code>sep<\/code>\u4e3a\u5206\u5272\u7b26\uff0c<code>after<\/code>\u4e3a\u5206\u5272\u7b26\u4e4b\u540e\u7684\u90e8\u5206\u3002\u5982\u679c<code>after<\/code>\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u5b57\u7b26\u4e32\u672b\u5c3e\u6709\u9017\u53f7\uff0c\u4f7f\u7528<code>before<\/code>\u4f5c\u4e3a\u6700\u7ec8\u8f93\u51fa\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u5b57\u7b26\u4e32\u5206\u5272\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32split\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u7684split\u65b9\u6cd5\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u9ad8\u6548\u5730\u5206\u5272\u5b57\u7b26\u4e32\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">output = &quot;1, 2, 3, 4, 5,&quot;<\/p>\n<p>output = &#39;, &#39;.join(output.split(&#39;, &#39;)[:-1]) if output.endswith(&#39;,&#39;) else output<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>output.split(&#39;, &#39;)[:-1]<\/code>\u7528\u4e8e\u5206\u5272\u5b57\u7b26\u4e32\u5e76\u53bb\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u6700\u7ec8\u901a\u8fc7<code>join<\/code>\u65b9\u6cd5\u751f\u6210\u4e86\u4e00\u4e2a\u6ca1\u6709\u672b\u5c3e\u9017\u53f7\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u5b57\u7b26\u4e32\u5206\u5272\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u53bb\u6389\u8f93\u51fa\u672b\u5c3e\u7684\u9017\u53f7\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u53bb\u6389\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u9017\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>rstrip()<\/code>\u65b9\u6cd5\u6765\u53bb\u6389\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u9017\u53f7\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;Hello, World,&quot;<\/code>\u53ef\u4ee5\u901a\u8fc7<code>my_string.rstrip(&#39;,&#39;)<\/code>\u6765\u53bb\u6389\u672b\u5c3e\u7684\u9017\u53f7\uff0c\u7ed3\u679c\u5c06\u662f<code>&quot;Hello, World&quot;<\/code>\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u53bb\u6389\u8f93\u51fa\u672b\u5c3e\u7684\u9017\u53f7\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>rstrip()<\/code>\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u5207\u7247\u6765\u53bb\u6389\u672b\u5c3e\u7684\u9017\u53f7\u3002\u4f8b\u5982\uff0c\u5982\u679c\u77e5\u9053\u5b57\u7b26\u4e32\u4ee5\u9017\u53f7\u7ed3\u675f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_string[:-1]<\/code>\uff0c\u4f46\u9700\u786e\u4fdd\u672b\u5c3e\u786e\u5b9e\u662f\u9017\u53f7\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u610f\u5916\u5220\u9664\u5176\u4ed6\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5728\u6253\u5370\u8f93\u51fa\u65f6\u5982\u4f55\u907f\u514d\u672b\u5c3e\u51fa\u73b0\u9017\u53f7\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u591a\u4e2a\u503c\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e<code>sep<\/code>\u53c2\u6570\u6765\u6539\u53d8\u8f93\u51fa\u7684\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>print(value1, value2, sep=&#39; &#39;)<\/code>\u53ef\u4ee5\u7528\u7a7a\u683c\u66ff\u4ee3\u9ed8\u8ba4\u7684\u9017\u53f7\uff0c\u8fd9\u6837\u5728\u8f93\u51fa\u65f6\u5c31\u4e0d\u4f1a\u6709\u9017\u53f7\u4e86\u3002\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u683c\u5f0f\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6216f-string\u8fdb\u884c\u66f4\u7075\u6d3b\u7684\u8f93\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53bb\u6389\u8f93\u51fa\u672b\u5c3e\u7684\u9017\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u4e8c\u3001\u4f7f\u7528join\u65b9\u6cd5\u3001\u4e09\u3001\u4f7f\u7528\u683c [&hellip;]","protected":false},"author":3,"featured_media":1042834,"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\/1042822"}],"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=1042822"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042822\/revisions"}],"predecessor-version":[{"id":1042836,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042822\/revisions\/1042836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1042834"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1042822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1042822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1042822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}