{"id":1141111,"date":"2025-01-08T22:28:02","date_gmt":"2025-01-08T14:28:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141111.html"},"modified":"2025-01-08T22:28:05","modified_gmt":"2025-01-08T14:28:05","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%8c%89%e4%bd%8d%e6%94%be%e5%85%a5%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141111.html","title":{"rendered":"python\u5982\u4f55\u628a\u5b57\u7b26\u4e32\u6309\u4f4d\u653e\u5165\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103922\/e5aed5a0-da04-489c-9bd7-6e0c20e0f733.webp\" alt=\"python\u5982\u4f55\u628a\u5b57\u7b26\u4e32\u6309\u4f4d\u653e\u5165\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u4f4d\u653e\u5165\u5217\u8868\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4f7f\u7528list()\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001for\u5faa\u73af\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528list()\u51fd\u6570\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528list()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u7684<code>list()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5b57\u7b26\u4e32\u6309\u4f4d\u62c6\u5206\u6210\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u5355\u72ec\u7684\u5143\u7d20\u52a0\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<h2><strong>\u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u6700\u4e3a\u7b80\u6d01\u548c\u76f4\u89c2\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u62c6\u5206\u4efb\u52a1\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u590d\u6742\u7684\u64cd\u4f5c\u3002\u5b83\u9002\u5408\u5728\u62c6\u5206\u5b57\u7b26\u4e32\u7684\u540c\u65f6\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u67d0\u4e9b\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>char_list = [char for char in string]<\/p>\n<p>print(char_list)<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u62c6\u5206\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u5faa\u73af\u4f53\u5185\u5bf9\u5b57\u7b26\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>char_list = []<\/p>\n<p>for char in string:<\/p>\n<p>    char_list.append(char)<\/p>\n<p>print(char_list)<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528map()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u6309\u4f4d\u62c6\u5206\u6210\u5217\u8868\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u76f8\u540c\u7684\u8f6c\u6362\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>char_list = list(map(str, string))<\/p>\n<p>print(char_list)<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u6548\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u573a\u666f\uff0c<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u52a0\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>string = &quot;hello&quot;<\/p>\n<p>char_array = np.array(list(string))<\/p>\n<p>print(char_array)<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;h&#39; &#39;e&#39; &#39;l&#39; &#39;l&#39; &#39;o&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5728\u9700\u8981\u8fdb\u4e00\u6b65\u5bf9\u5b57\u7b26\u6570\u7ec4\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u7b49\u590d\u6742\u64cd\u4f5c\u65f6\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528re.split()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6309\u7279\u5b9a\u89c4\u5219\u62c6\u5206\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93<code>re<\/code>\u4e2d\u7684<code>split()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;hello&quot;<\/p>\n<p>char_list = re.split(&#39;&#39;, string)[1:-1]<\/p>\n<p>print(char_list)<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u6309\u7279\u6b8a\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u6709\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528itertools\u5e93<\/h3>\n<\/p>\n<p><p><code>itertools<\/code>\u5e93\u4e2d\u7684<code>ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.from_iterable<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u6309\u4f4d\u62c6\u5206\u6210\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>string = &quot;hello&quot;<\/p>\n<p>char_list = list(itertools.chain.from_iterable(string))<\/p>\n<p>print(char_list)<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u6309\u4f4d\u62c6\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def split_string_to_list(string):<\/p>\n<p>    return [char for char in string]<\/p>\n<p>string = &quot;hello&quot;<\/p>\n<p>char_list = split_string_to_list(string)<\/p>\n<p>print(char_list)<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u7a7a\u5b57\u7b26\u4e32\u548c\u7279\u6b8a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u7a7a\u5b57\u7b26\u4e32\u548c\u7279\u6b8a\u5b57\u7b26\u3002\u53ef\u4ee5\u5728\u62c6\u5206\u524d\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u9884\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;h e l l o&quot;<\/p>\n<p>string = string.replace(&#39; &#39;, &#39;&#39;)  # \u53bb\u9664\u7a7a\u683c<\/p>\n<p>char_list = list(string)<\/p>\n<p>print(char_list)<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0c\u4f7f\u7528<code>numpy<\/code>\u5e93\u6216<code>itertools<\/code>\u5e93\u53ef\u80fd\u66f4\u4e3a\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>string = &quot;hello&quot; * 1000<\/p>\n<h2><strong>\u4f7f\u7528list()\u51fd\u6570<\/strong><\/h2>\n<p>time_list = timeit.timeit(lambda: list(string), number=1000)<\/p>\n<p>print(f&quot;list()\u51fd\u6570\u8017\u65f6: {time_list}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>time_comprehension = timeit.timeit(lambda: [char for char in string], number=1000)<\/p>\n<p>print(f&quot;\u5217\u8868\u63a8\u5bfc\u5f0f\u8017\u65f6: {time_comprehension}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u5e93<\/strong><\/h2>\n<p>time_numpy = timeit.timeit(lambda: np.array(list(string)), number=1000)<\/p>\n<p>print(f&quot;numpy\u5e93\u8017\u65f6: {time_numpy}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u6309\u4f4d\u653e\u5165\u5217\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u9700\u6c42\u3002<\/strong> \u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u6700\u4e3a\u7b80\u6d01\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u62c6\u5206\u4efb\u52a1\uff1b\u5217\u8868\u63a8\u5bfc\u5f0f\u548cfor\u5faa\u73af\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff1b<code>numpy<\/code>\u5e93\u548c<code>itertools<\/code>\u5e93\u5219\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u66f4\u4e3a\u9ad8\u6548\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\u7684\u6bcf\u4e2a\u5b57\u7b26\u653e\u5165\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5185\u7f6e\u51fd\u6570<code>list()<\/code>\u5c06\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u653e\u5165\u4e00\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<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\u662f\u4e00\u79cd\u7b80\u5355\u800c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5904\u7406\u4efb\u4f55\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u653e\u5165\u5217\u8868\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5982\u679c\u53ea\u60f3\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u7279\u5b9a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5143\u97f3\u5b57\u6bcd\u653e\u5165\u5217\u8868\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>[char for char in &quot;hello&quot; if char in &#39;aeiou&#39;]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>[&#39;e&#39;, &#39;o&#39;]<\/code>\u3002<\/p>\n<p><strong>\u5728\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u540e\uff0c\u5982\u4f55\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u6216\u53bb\u91cd\uff1f<\/strong><br \/>\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u53bb\u91cd\uff0c\u7136\u540e\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u9996\u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u540e\uff0c\u4f7f\u7528<code>unique_chars = set(list(&quot;hello&quot;))<\/code>\u5f97\u5230\u552f\u4e00\u5b57\u7b26\uff0c\u518d\u4f7f\u7528<code>sorted_chars = sorted(unique_chars)<\/code>\u5bf9\u5176\u6392\u5e8f\u3002\u8fd9\u6837\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u6ca1\u6709\u91cd\u590d\u4e14\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\u5b57\u7b26\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u4f4d\u653e\u5165\u5217\u8868\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4f7f\u7528list()\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001for\u5faa [&hellip;]","protected":false},"author":3,"featured_media":1141118,"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\/1141111"}],"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=1141111"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141111\/revisions"}],"predecessor-version":[{"id":1141121,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141111\/revisions\/1141121"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141118"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}