{"id":1114468,"date":"2025-01-08T17:57:31","date_gmt":"2025-01-08T09:57:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1114468.html"},"modified":"2025-01-08T17:57:34","modified_gmt":"2025-01-08T09:57:34","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1114468.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075639\/238e01c5-19e0-49a8-aae7-b174453c9ef5.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5305\u62ec\u76f4\u63a5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong> \u4e3b\u8981\u65b9\u6cd5\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\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u548c<code>split()<\/code>\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u53ca\u5176\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>list()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u5217\u8868\u7684\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<code>list()<\/code>\u51fd\u6570\u4f1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u5355\u72ec\u7684\u5143\u7d20\u653e\u5165\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)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b57\u7b26\u4e32<code>&quot;hello&quot;<\/code>\u88ab\u4f20\u9012\u7ed9<code>list()<\/code>\u51fd\u6570\uff0c\u7ed3\u679c\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u5217\u8868\uff1a<code>[&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>split()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u5206\u9694\u7b26\u62c6\u5206\u4e3a\u5217\u8868\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4f1a\u4ee5\u7a7a\u683c\u8fdb\u884c\u62c6\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello world&quot;<\/p>\n<p>word_list = string.split()<\/p>\n<p>print(word_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b57\u7b26\u4e32<code>&quot;hello world&quot;<\/code>\u88ab<code>split()<\/code>\u65b9\u6cd5\u6309\u7a7a\u683c\u62c6\u5206\uff0c\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u5355\u8bcd\u7684\u5217\u8868\uff1a<code>[&#39;hello&#39;, &#39;world&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u6307\u5b9a\u5176\u4ed6\u5206\u9694\u7b26\u6765\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u4ee5\u9017\u53f7\u5206\u9694\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple,banana,orange&quot;<\/p>\n<p>fruit_list = string.split(&#39;,&#39;)<\/p>\n<p>print(fruit_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7ed3\u679c\u4e3a\uff1a<code>[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5176ASCII\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>ascii_list = [ord(char) for char in string]<\/p>\n<p>print(ascii_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b57\u7b26\u4e32<code>&quot;hello&quot;<\/code>\u88ab\u8f6c\u6362\u4e3a\u5176ASCII\u7801\u5217\u8868\uff1a<code>[104, 101, 108, 108, 111]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u5b57\u7b26\u4e32\u62c6\u5206\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u6309\u591a\u4e2a\u5206\u9694\u7b26\u62c6\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;apple;banana,orange&quot;<\/p>\n<p>fruit_list = re.split(r&#39;[;,]&#39;, string)<\/p>\n<p>print(fruit_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>r&#39;[;,]&#39;<\/code>\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u5206\u53f7\u6216\u9017\u53f7\uff0c\u7ed3\u679c\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u5355\u8bcd\u7684\u5217\u8868\uff1a<code>[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5176\u4ed6\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u89c1\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u7279\u5b9a\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;12345&quot;<\/p>\n<p>int_list = list(map(int, string))<\/p>\n<p>print(int_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b57\u7b26\u4e32<code>&quot;12345&quot;<\/code>\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570\u5217\u8868\uff1a<code>[1, 2, 3, 4, 5]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002<strong>\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u9002\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u5217\u8868\uff0c\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u6309\u6307\u5b9a\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u62c6\u5206\u9700\u6c42<\/strong>\u3002\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\u6570\u636e\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\u5206\u5272\u6210\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\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\u5c06\u5b57\u7b26\u4e32\u5206\u5272\uff0c\u4f8b\u5982\u7a7a\u683c\u3001\u9017\u53f7\u6216\u5176\u4ed6\u5b57\u7b26\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4ee5\u7a7a\u683c\u4e3a\u5206\u9694\u3002\u4f8b\u5982\uff0c<code>&quot;apple,banana,cherry&quot;.split(&quot;,&quot;)<\/code> \u5c06\u8fd4\u56de <code>[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u5217\u8868\u5143\u7d20\uff1f<\/strong><br \/>\u7edd\u5bf9\u53ef\u4ee5\uff01\u60a8\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u8fd9\u6837\u6bcf\u4e2a\u5b57\u7b26\u90fd\u4f1a\u6210\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>list(&quot;hello&quot;)<\/code>\u5c06\u8fd4\u56de <code>[&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u4e14\u6709\u6548\u3002<\/p>\n<p><strong>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u6709\u91cd\u590d\u5143\u7d20\uff0c\u5982\u4f55\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u5143\u7d20\u552f\u4e00\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u5143\u7d20\u552f\u4e00\u3002\u9996\u5148\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u6700\u540e\u518d\u8f6c\u6362\u56de\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>list(set(&quot;banana&quot;))<\/code>\u4f1a\u8fd4\u56de <code>[&#39;b&#39;, &#39;a&#39;, &#39;n&#39;]<\/code>\uff0c\u5176\u4e2d\u6240\u6709\u91cd\u590d\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u53bb\u9664\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u56e0\u6b64\u5143\u7d20\u7684\u987a\u5e8f\u53ef\u80fd\u4f1a\u6539\u53d8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5305\u62ec\u76f4\u63a5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002 \u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528l [&hellip;]","protected":false},"author":3,"featured_media":1114476,"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\/1114468"}],"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=1114468"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114468\/revisions"}],"predecessor-version":[{"id":1114477,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114468\/revisions\/1114477"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114476"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}