{"id":1144482,"date":"2025-01-08T23:00:08","date_gmt":"2025-01-08T15:00:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144482.html"},"modified":"2025-01-08T23:00:12","modified_gmt":"2025-01-08T15:00:12","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e6%af%8f%e4%b8%aa%e5%ad%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1144482.html","title":{"rendered":"python\u5982\u4f55\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181541\/6b9f96df-0f8c-4b7d-9fa9-3a14ddfb5e35.webp\" alt=\"python\u5982\u4f55\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528for\u5faa\u73af\u3001list()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u5207\u7247\u3001enumerate()\u51fd\u6570<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528for\u5faa\u73af<\/strong>\u662f\u6700\u4e3a\u666e\u904d\u4e14\u76f4\u89c2\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u901a\u8fc7for\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u9010\u5b57\u7b26\u5730\u8bbf\u95ee\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\u9010\u4e2a\u5b57\u7b26\u5730\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u5904\u7406\u6216\u5b58\u50a8\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>for char in string:<\/p>\n<p>    print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>char<\/code>\u53d8\u91cf\u4f1a\u4f9d\u6b21\u83b7\u53d6\u5b57\u7b26\u4e32<code>string<\/code>\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u5b58\u50a8\u5230\u5217\u8868\u4e2d\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>for\u5faa\u73af\u662f\u4e00\u79cd\u57fa\u7840\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u9010\u5b57\u7b26\u5730\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\u3002\u5b83\u4e0d\u4ec5\u7b80\u5355\u6613\u61c2\uff0c\u800c\u4e14\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f20\u9012\u7ed9for\u5faa\u73af\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>for char in string:<\/p>\n<p>    print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>char<\/code>\u53d8\u91cf\u4f1a\u4f9d\u6b21\u83b7\u53d6\u5b57\u7b26\u4e32<code>string<\/code>\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u9010\u4e2a\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5b58\u50a8\u5b57\u7b26\u5230\u5217\u8868\u4e2d<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5b58\u50a8\u5230\u5217\u8868\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&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<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5b57\u7b26\u6dfb\u52a0\u5230\u5217\u8868<code>char_list<\/code>\u4e2d\uff0c\u6700\u7ec8\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5b57\u7b26\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e8c\u3001\u4f7f\u7528list()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>list()\u51fd\u6570\u662f\u4e00\u79cd\u975e\u5e38\u76f4\u89c2\u4e14\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5b57\u7b26\u5217\u8868\uff0c\u6bcf\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528list()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/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>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>list(string)<\/code>\u5c06\u5b57\u7b26\u4e32<code>string<\/code>\u8f6c\u5316\u4e3a\u5b57\u7b26\u5217\u8868<code>char_list<\/code>\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u9002\u5408\u9700\u8981\u5feb\u901f\u83b7\u5f97\u5b57\u7b26\u5217\u8868\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u5bf9\u5b57\u7b26\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7list()\u51fd\u6570\u83b7\u53d6\u5b57\u7b26\u5217\u8868\u540e\uff0c\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\uff0c\u4f8b\u5982\u6392\u5e8f\u3001\u53bb\u91cd\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;banana&quot;<\/p>\n<p>char_list = list(string)<\/p>\n<p>unique_chars = list(set(char_list))<\/p>\n<p>sorted_chars = sorted(unique_chars)<\/p>\n<p>print(sorted_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5b57\u7b26\u5217\u8868\uff0c\u7136\u540e\u901a\u8fc7<code>set()<\/code>\u51fd\u6570\u53bb\u91cd\uff0c\u6700\u540e\u901a\u8fc7<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5b57\u7b26\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230\u4e00\u4e2a\u53bb\u91cd\u4e14\u6392\u5e8f\u540e\u7684\u5b57\u7b26\u5217\u8868\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u4efb\u610f\u90e8\u5206\uff0c\u5305\u62ec\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u8981\u901a\u8fc7\u5207\u7247\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u5355\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>char = string[2]<\/p>\n<p>print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>string[2]<\/code>\u83b7\u53d6\u5b57\u7b26\u4e32<code>string<\/code>\u4e2d\u7d22\u5f15\u4e3a2\u7684\u5b57\u7b26\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u8bbf\u95ee\u7279\u5b9a\u4f4d\u7f6e\u5b57\u7b26\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u904d\u5386\u5e76\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u5207\u7247\u64cd\u4f5c\u901a\u5e38\u7528\u4e8e\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7ed3\u5408for\u5faa\u73af\u6765\u904d\u5386\u5e76\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>for i in range(len(string)):<\/p>\n<p>    char = string[i]<\/p>\n<p>    print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>range(len(string))<\/code>\u751f\u6210\u7d22\u5f15\u8303\u56f4\uff0c\u5e76\u7ed3\u5408for\u5faa\u73af\u9010\u4e2a\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u56db\u3001\u4f7f\u7528enumerate()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>enumerate()\u51fd\u6570\u662f\u4e00\u79cd\u9ad8\u7ea7\u904d\u5386\u65b9\u5f0f\uff0c\u9664\u4e86\u63d0\u4f9b\u5b57\u7b26\u672c\u8eab\uff0c\u8fd8\u53ef\u4ee5\u83b7\u53d6\u5b57\u7b26\u7684\u7d22\u5f15\uff0c\u9002\u5408\u9700\u8981\u7d22\u5f15\u4fe1\u606f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528enumerate()\u51fd\u6570\u904d\u5386\u5b57\u7b26\u4e32\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>for index, char in enumerate(string):<\/p>\n<p>    print(index, char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>enumerate(string)<\/code>\u751f\u6210\u7d22\u5f15\u548c\u5b57\u7b26\u7684\u5bf9\uff0c<code>index<\/code>\u53d8\u91cf\u83b7\u53d6\u5b57\u7b26\u7684\u7d22\u5f15\uff0c<code>char<\/code>\u53d8\u91cf\u83b7\u53d6\u5b57\u7b26\u672c\u8eab\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u5b58\u50a8\u7d22\u5f15\u548c\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5c06\u7d22\u5f15\u548c\u5b57\u7b26\u5b58\u50a8\u5230\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>indexed_chars = []<\/p>\n<p>for index, char in enumerate(string):<\/p>\n<p>    indexed_chars.append((index, char))<\/p>\n<p>print(indexed_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u7d22\u5f15\u548c\u5b57\u7b26\u7684\u5bf9\u6dfb\u52a0\u5230\u5217\u8868<code>indexed_chars<\/code>\u4e2d\uff0c\u6700\u7ec8\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u548c\u5b57\u7b26\u5bf9\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u5feb\u901f\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>char_list = [char for char in string]<\/p>\n<p>print(char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f<code>[char for char in string]<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u5217\u8868\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u9002\u5408\u9700\u8981\u5feb\u901f\u751f\u6210\u5b57\u7b26\u5217\u8868\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u7ed3\u5408\u6761\u4ef6\u8fc7\u6ee4<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u8fd8\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u8fc7\u6ee4\uff0c\u53ea\u83b7\u53d6\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python123&quot;<\/p>\n<p>char_list = [char for char in string if char.isalpha()]<\/p>\n<p>print(char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f<code>[char for char in string if char.isalpha()]<\/code>\u751f\u6210\u4e00\u4e2a\u53ea\u5305\u542b\u5b57\u6bcd\u5b57\u7b26\u7684\u5217\u8868\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u516d\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u548c\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u6216\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Python123&quot;<\/p>\n<p>char_list = re.findall(r&#39;.&#39;, string)<\/p>\n<p>print(char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>re.findall(r&#39;.&#39;, string)<\/code>\u5339\u914d\u5b57\u7b26\u4e32<code>string<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u5217\u8868<code>char_list<\/code>\uff0c\u6700\u7ec8\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u63d0\u53d6\u7279\u5b9a\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u6216\u6a21\u5f0f\uff0c\u4f8b\u5982\u53ea\u63d0\u53d6\u5b57\u6bcd\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Python123&quot;<\/p>\n<p>char_list = re.findall(r&#39;[a-zA-Z]&#39;, string)<\/p>\n<p>print(char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>re.findall(r&#39;[a-zA-Z]&#39;, string)<\/code>\u5339\u914d\u5e76\u63d0\u53d6\u5b57\u7b26\u4e32<code>string<\/code>\u4e2d\u7684\u5b57\u6bcd\u5b57\u7b26\uff0c\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u5217\u8868<code>char_list<\/code>\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e03\u3001\u4f7f\u7528map()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>map()\u51fd\u6570\u662f\u4e00\u79cd\u9ad8\u9636\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u9002\u5408\u9700\u8981\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u7279\u5b9a\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528map()\u51fd\u6570\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>char_list = list(map(lambda char: char, string))<\/p>\n<p>print(char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>map(lambda char: char, string)<\/code>\u5c06\u533f\u540d\u51fd\u6570\u5e94\u7528\u4e8e\u5b57\u7b26\u4e32<code>string<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u5217\u8868<code>char_list<\/code>\uff0c\u6700\u7ec8\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h4>7.2 \u5bf9\u5b57\u7b26\u8fdb\u884c\u7279\u5b9a\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7map()\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u7279\u5b9a\u64cd\u4f5c\uff0c\u4f8b\u5982\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>upper_char_list = list(map(lambda char: char.upper(), string))<\/p>\n<p>print(upper_char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>map(lambda char: char.upper(), string)<\/code>\u5c06\u533f\u540d\u51fd\u6570\u5e94\u7528\u4e8e\u5b57\u7b26\u4e32<code>string<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\uff0c\u751f\u6210\u4e00\u4e2a\u5927\u5199\u5b57\u7b26\u5217\u8868<code>upper_char_list<\/code>\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u8bbf\u95ee\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u4f8b\u5982join()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>8.1 \u4f7f\u7528join()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u867d\u7136join()\u65b9\u6cd5\u901a\u5e38\u7528\u6765\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5206\u9694\u5f00\u6765\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>char_list = list(&quot;&quot;.join(string))<\/p>\n<p>print(char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>&quot;&quot;.join(string)<\/code>\u5c06\u5b57\u7b26\u4e32<code>string<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8fde\u63a5\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u5316\u4e3a\u5b57\u7b26\u5217\u8868<code>char_list<\/code>\uff0c\u6700\u7ec8\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h4>8.2 \u4f7f\u7528split()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>split()\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u6765\u5c06\u5b57\u7b26\u4e32\u6309\u7279\u5b9a\u5206\u9694\u7b26\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u867d\u7136\u901a\u5e38\u7528\u4e8e\u5355\u8bcd\u5206\u5272\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u5206\u5272\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Python&quot;<\/p>\n<p>char_list = string.split(&quot;&quot;)<\/p>\n<p>print(char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>string.split(&quot;&quot;)<\/code>\u5c06\u5b57\u7b26\u4e32<code>string<\/code>\u6309\u7a7a\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5b57\u7b26\u5217\u8868<code>char_list<\/code>\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u9012\u5f52<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u6280\u5de7\uff0c\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u8bb8\u591a\u590d\u6742\u95ee\u9898\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>9.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528\u9012\u5f52\u904d\u5386\u5b57\u7b26\u4e32\u5e76\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_chars(string, index=0):<\/p>\n<p>    if index == len(string):<\/p>\n<p>        return []<\/p>\n<p>    return [string[index]] + get_chars(string, index + 1)<\/p>\n<p>string = &quot;Python&quot;<\/p>\n<p>char_list = get_chars(string)<\/p>\n<p>print(char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u9012\u5f52\u51fd\u6570<code>get_chars<\/code>\u904d\u5386\u5b57\u7b26\u4e32<code>string<\/code>\uff0c\u5e76\u9010\u5b57\u7b26\u5730\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u5217\u8868<code>char_list<\/code>\uff0c\u6700\u7ec8\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><h4>9.2 \u7ed3\u5408\u6761\u4ef6\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u9012\u5f52\u8fd8\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u5904\u7406\uff0c\u53ea\u83b7\u53d6\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_alpha_chars(string, index=0):<\/p>\n<p>    if index == len(string):<\/p>\n<p>        return []<\/p>\n<p>    char = string[index]<\/p>\n<p>    return ([char] if char.isalpha() else []) + get_alpha_chars(string, index + 1)<\/p>\n<p>string = &quot;Python123&quot;<\/p>\n<p>alpha_char_list = get_alpha_chars(string)<\/p>\n<p>print(alpha_char_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u9012\u5f52\u51fd\u6570<code>get_alpha_chars<\/code>\u904d\u5386\u5b57\u7b26\u4e32<code>string<\/code>\uff0c\u5e76\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\uff0c\u53ea\u83b7\u53d6\u5b57\u6bcd\u5b57\u7b26\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u5b57\u6bcd\u5b57\u7b26\u5217\u8868<code>alpha_char_list<\/code>\uff0c\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u603b\u7ed3\uff1aPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5305\u62ecfor\u5faa\u73af\u3001list()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u5207\u7247\u3001enumerate()\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001map()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u548c\u9012\u5f52\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\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\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff1f<\/strong><br \/>\u8981\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684for\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">string = &quot;Hello&quot;\nfor char in string:\n    print(char)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u4f9d\u6b21\u8f93\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u53d6\u51fa\u5b57\u7b26\u4e32\u5b57\u7b26\u7684\u5176\u4ed6\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528for\u5faa\u73af\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">string = &quot;Hello&quot;\ncharacters = [char for char in string]\nprint(characters)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u5b57\u7b26\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n<p><strong>\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u53ef\u4ee5\u8fdb\u884c\u54ea\u4e9b\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u8fdb\u884c\u591a\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u62fc\u63a5\u3001\u5207\u7247\u3001\u67e5\u627e\u3001\u66ff\u6362\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u4f8b\u5982<code>string.find()<\/code>\u3001<code>string.replace()<\/code>\u7b49\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528for\u5faa\u73af\u3001list()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u5207\u7247\u3001enumera [&hellip;]","protected":false},"author":3,"featured_media":1144491,"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\/1144482"}],"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=1144482"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144482\/revisions"}],"predecessor-version":[{"id":1144492,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144482\/revisions\/1144492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144491"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}