{"id":1111197,"date":"2025-01-08T17:26:48","date_gmt":"2025-01-08T09:26:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111197.html"},"modified":"2025-01-08T17:26:50","modified_gmt":"2025-01-08T09:26:50","slug":"%e5%9c%a8python%e9%87%8c%e9%9d%a2%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e9%a6%96%e5%ad%97%e6%af%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1111197.html","title":{"rendered":"\u5728Python\u91cc\u9762\u5982\u4f55\u8868\u793a\u9996\u5b57\u6bcd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073741\/6d9eee8c-d73b-4316-97c3-d9efa5e78c48.webp\" alt=\"\u5728Python\u91cc\u9762\u5982\u4f55\u8868\u793a\u9996\u5b57\u6bcd\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8868\u793a\u9996\u5b57\u6bcd\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u7d22\u5f15\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u7d22\u5f15\u662f\u6700\u76f4\u63a5\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5c06\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u63d0\u53d6\u51fa\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u8be5\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b57\u7b26\u4e32\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u7d22\u5f15\u662f\u83b7\u53d6\u5b57\u7b26\u4e32\u9996\u5b57\u6bcd\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5b57\u7b26\u4e32\u5728Python\u4e2d\u662f\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u7279\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f150\u6765\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello&quot;<\/p>\n<p>first_letter = string[0]<\/p>\n<p>print(first_letter)  # \u8f93\u51fa: H<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u5408\u5927\u591a\u6570\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u4e5f\u662f\u4e00\u79cd\u83b7\u53d6\u5b57\u7b26\u4e32\u9996\u5b57\u6bcd\u7684\u65b9\u6cd5\u3002\u5207\u7247\u5141\u8bb8\u6211\u4eec\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u90e8\u5206\uff0c\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u6765\u83b7\u53d6\u5b50\u5b57\u7b26\u4e32\u3002\u8981\u83b7\u53d6\u9996\u5b57\u6bcd\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5207\u7247\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello&quot;<\/p>\n<p>first_letter = string[:1]<\/p>\n<p>print(first_letter)  # \u8f93\u51fa: H<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u7684\u7279\u5b9a\u90e8\u5206\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Hello&quot;<\/p>\n<p>match = re.match(r&quot;^[A-Za-z]&quot;, string)<\/p>\n<p>if match:<\/p>\n<p>    first_letter = match.group(0)<\/p>\n<p>    print(first_letter)  # \u8f93\u51fa: H<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u5b57\u7b26\u4e32\u5339\u914d\u548c\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>capitalize<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\uff0c\u7136\u540e\u83b7\u53d6\u8be5\u9996\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>capitalized_string = string.capitalize()<\/p>\n<p>first_letter = capitalized_string[0]<\/p>\n<p>print(first_letter)  # \u8f93\u51fa: H<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u591a\u79cd\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7ec4\u5408\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ec4\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7ed3\u5408\u7d22\u5f15\u548c\u6761\u4ef6\u5224\u65ad\u7684\u793a\u4f8b\uff0c\u6765\u83b7\u53d6\u9996\u5b57\u6bcd\u5e76\u5904\u7406\u7279\u6b8a\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_first_letter(string):<\/p>\n<p>    if not string:<\/p>\n<p>        return None<\/p>\n<p>    first_letter = string[0]<\/p>\n<p>    if first_letter.isalpha():<\/p>\n<p>        return first_letter<\/p>\n<p>    else:<\/p>\n<p>        # \u5904\u7406\u7279\u6b8a\u60c5\u51b5\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u9996\u5b57\u7b26\u4e3a\u6570\u5b57\u6216\u7b26\u53f7<\/p>\n<p>        for char in string:<\/p>\n<p>            if char.isalpha():<\/p>\n<p>                return char<\/p>\n<p>        return None<\/p>\n<p>string = &quot;123Hello&quot;<\/p>\n<p>print(get_first_letter(string))  # \u8f93\u51fa: H<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u591a\u79cd\u8f93\u5165\u60c5\u51b5\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li><strong>\u7528\u6237\u8f93\u5165\u5904\u7406<\/strong>\uff1a\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c\u5728\u7528\u6237\u6ce8\u518c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u9996\u5b57\u6bcd\u8fdb\u884c\u6635\u79f0\u6216\u7528\u6237\u540d\u7684\u751f\u6210\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def generate_username(name):<\/p>\n<p>    first_letter = name[0].upper()<\/p>\n<p>    return f&quot;user_{first_letter}{len(name)}&quot;<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>username = generate_username(name)<\/p>\n<p>print(username)  # \u8f93\u51fa: user_A5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6587\u4ef6\u540d\u5904\u7406<\/strong>\uff1a\u5728\u5904\u7406\u6587\u4ef6\u540d\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u6587\u4ef6\u540d\u7684\u9996\u5b57\u6bcd\u8fdb\u884c\u5206\u7c7b\u6216\u6392\u5e8f\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def categorize_files(file_list):<\/p>\n<p>    categorized_files = {}<\/p>\n<p>    for file in file_list:<\/p>\n<p>        first_letter = file[0].upper()<\/p>\n<p>        if first_letter not in categorized_files:<\/p>\n<p>            categorized_files[first_letter] = []<\/p>\n<p>        categorized_files[first_letter].append(file)<\/p>\n<p>    return categorized_files<\/p>\n<p>file_list = [&quot;apple.txt&quot;, &quot;banana.doc&quot;, &quot;cherry.pdf&quot;, &quot;avocado.jpg&quot;]<\/p>\n<p>categorized_files = categorize_files(file_list)<\/p>\n<p>print(categorized_files)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;A&#39;: [&#39;apple.txt&#39;, &#39;avocado.jpg&#39;], &#39;B&#39;: [&#39;banana.doc&#39;], &#39;C&#39;: [&#39;cherry.pdf&#39;]}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u8fdb\u884c\u5206\u7ec4\u6216\u7edf\u8ba1\u3002\u4f8b\u5982\uff0c\u5728\u5206\u6790\u5ba2\u6237\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5ba2\u6237\u540d\u5b57\u7684\u9996\u5b57\u6bcd\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def group_customers_by_initial(customers):<\/p>\n<p>    grouped_customers = {}<\/p>\n<p>    for customer in customers:<\/p>\n<p>        initial = customer[0].upper()<\/p>\n<p>        if initial not in grouped_customers:<\/p>\n<p>            grouped_customers[initial] = []<\/p>\n<p>        grouped_customers[initial].append(customer)<\/p>\n<p>    return grouped_customers<\/p>\n<p>customers = [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;, &quot;Amanda&quot;, &quot;Brian&quot;]<\/p>\n<p>grouped_customers = group_customers_by_initial(customers)<\/p>\n<p>print(grouped_customers)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;A&#39;: [&#39;Alice&#39;, &#39;Amanda&#39;], &#39;B&#39;: [&#39;Bob&#39;, &#39;Brian&#39;], &#39;C&#39;: [&#39;Charlie&#39;]}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u5b57\u7b26\u4e32\u7d22\u5f15\u3001\u5b57\u7b26\u4e32\u5207\u7247\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u7b49<\/strong>\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7d22\u5f15\u6765\u83b7\u53d6\u9996\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>word<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>word[0]<\/code> \u6765\u63d0\u53d6\u9996\u5b57\u6bcd\u3002\u786e\u4fdd\u5b57\u7b26\u4e32\u4e0d\u4e3a\u7a7a\uff0c\u4ee5\u907f\u514d\u7d22\u5f15\u9519\u8bef\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5982\u4f55\u5c06\u9996\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>capitalize()<\/code> \u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c<code>word.capitalize()<\/code> \u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u9996\u5b57\u6bcd\u662f\u5927\u5199\uff0c\u5176\u4f59\u5b57\u6bcd\u4fdd\u6301\u4e0d\u53d8\u3002\u6b64\u5916\uff0c\u4f7f\u7528 <code>str.upper()<\/code> \u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5355\u72ec\u8f6c\u6362\u9996\u5b57\u6bcd\uff0c\u5982 <code>word[0].upper() + word[1:]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u662f\u5426\u4e3a\u7279\u5b9a\u5b57\u6bcd\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u4e0e\u76ee\u6807\u5b57\u6bcd\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>if word[0] == &#39;A&#39;:<\/code> \u6765\u5224\u65ad\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u662f\u5426\u4e3a &#39;A&#39;\u3002\u786e\u4fdd\u5728\u6bd4\u8f83\u4e4b\u524d\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8868\u793a\u9996\u5b57\u6bcd\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4f7f\u7528\u5b57\u7b26\u4e32\u7d22\u5f15\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1111204,"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\/1111197"}],"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=1111197"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111197\/revisions"}],"predecessor-version":[{"id":1111205,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111197\/revisions\/1111205"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111204"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}