{"id":1116512,"date":"2025-01-08T18:17:10","date_gmt":"2025-01-08T10:17:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116512.html"},"modified":"2025-01-08T18:17:12","modified_gmt":"2025-01-08T10:17:12","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e6%89%be%e5%a4%a7%e5%b0%8f%e5%86%99%e5%ad%97%e6%af%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1116512.html","title":{"rendered":"python\u5982\u4f55\u67e5\u627e\u5927\u5c0f\u5199\u5b57\u6bcd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080822\/85156080-0bc8-429e-90be-1ac23398f6e5.webp\" alt=\"python\u5982\u4f55\u67e5\u627e\u5927\u5c0f\u5199\u5b57\u6bcd\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u67e5\u627e\u5927\u5c0f\u5199\u5b57\u6bcd\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u6700\u4e3a\u7b80\u5355\u548c\u76f4\u89c2\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5339\u914d\u548c\u63d0\u53d6\u80fd\u529b\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u5b9e\u4f8b\u5c55\u793a\u5b83\u4eec\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u5b57\u7b26\u4e32\u3002\u5728\u67e5\u627e\u5927\u5c0f\u5199\u5b57\u6bcd\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>isupper()<\/code> \u548c <code>islower()<\/code> \u65b9\u6cd5\u3002\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5206\u522b\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u5927\u5199\u5b57\u6bcd\u548c\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World&quot;<\/p>\n<p>uppercase_letters = [char for char in text if char.isupper()]<\/p>\n<p>lowercase_letters = [char for char in text if char.islower()]<\/p>\n<p>print(&quot;Uppercase letters:&quot;, uppercase_letters)<\/p>\n<p>print(&quot;Lowercase letters:&quot;, lowercase_letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>isupper()<\/code> \u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u662f\u5426\u4e3a\u5927\u5199\u5b57\u6bcd\uff0c\u800c <code>islower()<\/code> \u65b9\u6cd5\u5219\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u662f\u5426\u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002\u901a\u8fc7\u5217\u8868\u89e3\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u63d0\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5199\u548c\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u548c\u63d0\u53d6\u3002\u5728 Python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>re<\/code> \u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello World&quot;<\/p>\n<p>uppercase_pattern = r&#39;[A-Z]&#39;<\/p>\n<p>lowercase_pattern = r&#39;[a-z]&#39;<\/p>\n<p>uppercase_letters = re.findall(uppercase_pattern, text)<\/p>\n<p>lowercase_letters = re.findall(lowercase_pattern, text)<\/p>\n<p>print(&quot;Uppercase letters:&quot;, uppercase_letters)<\/p>\n<p>print(&quot;Lowercase letters:&quot;, lowercase_letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 <code>re.findall()<\/code> \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6839\u636e\u7ed9\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5339\u914d\u9879\u3002<code>[A-Z]<\/code> \u8868\u793a\u5339\u914d\u6240\u6709\u5927\u5199\u5b57\u6bcd\uff0c\u800c <code>[a-z]<\/code> \u8868\u793a\u5339\u914d\u6240\u6709\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\uff08List Comprehension\uff09\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u901a\u5e38\u6bd4\u4f7f\u7528\u5faa\u73af\u66f4\u52a0\u4f18\u96c5\u3002\u5728\u67e5\u627e\u5927\u5c0f\u5199\u5b57\u6bcd\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408 <code>isupper()<\/code> \u548c <code>islower()<\/code> \u65b9\u6cd5\u6765\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World&quot;<\/p>\n<p>uppercase_letters = [char for char in text if char.isupper()]<\/p>\n<p>lowercase_letters = [char for char in text if char.islower()]<\/p>\n<p>print(&quot;Uppercase letters:&quot;, uppercase_letters)<\/p>\n<p>print(&quot;Lowercase letters:&quot;, lowercase_letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u524d\u9762\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u7684\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u53ea\u662f\u6211\u4eec\u76f4\u63a5\u5728\u5217\u8868\u89e3\u6790\u4e2d\u4f7f\u7528\u4e86 <code>isupper()<\/code> \u548c <code>islower()<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7efc\u5408\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u67e5\u627e\u5927\u5c0f\u5199\u5b57\u6bcd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5904\u7406\u5305\u542b\u5927\u5c0f\u5199\u5b57\u6bcd\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u5206\u79bb\u6210\u4e24\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def separate_letters(text):<\/p>\n<p>    uppercase_pattern = r&#39;[A-Z]&#39;<\/p>\n<p>    lowercase_pattern = r&#39;[a-z]&#39;<\/p>\n<p>    uppercase_letters = &#39;&#39;.join(re.findall(uppercase_pattern, text))<\/p>\n<p>    lowercase_letters = &#39;&#39;.join(re.findall(lowercase_pattern, text))<\/p>\n<p>    return uppercase_letters, lowercase_letters<\/p>\n<p>text = &quot;Hello World&quot;<\/p>\n<p>uppercase_letters, lowercase_letters = separate_letters(text)<\/p>\n<p>print(&quot;Uppercase letters:&quot;, uppercase_letters)<\/p>\n<p>print(&quot;Lowercase letters:&quot;, lowercase_letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>separate_letters()<\/code> \u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5199\u548c\u5c0f\u5199\u5b57\u6bcd\uff0c\u5e76\u5c06\u5b83\u4eec\u5206\u522b\u7ec4\u5408\u6210\u4e24\u4e2a\u5b57\u7b26\u4e32\u3002\u6700\u7ec8\u8fd4\u56de\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u4f18\u5316\u6027\u80fd\uff0c\u4ee5\u63d0\u9ad8\u67e5\u627e\u5927\u5c0f\u5199\u5b57\u6bcd\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def separate_letters_optimized(text):<\/p>\n<p>    uppercase_letters = []<\/p>\n<p>    lowercase_letters = []<\/p>\n<p>    for char in text:<\/p>\n<p>        if &#39;A&#39; &lt;= char &lt;= &#39;Z&#39;:<\/p>\n<p>            uppercase_letters.append(char)<\/p>\n<p>        elif &#39;a&#39; &lt;= char &lt;= &#39;z&#39;:<\/p>\n<p>            lowercase_letters.append(char)<\/p>\n<p>    return &#39;&#39;.join(uppercase_letters), &#39;&#39;.join(lowercase_letters)<\/p>\n<p>text = &quot;Hello World&quot;<\/p>\n<p>uppercase_letters, lowercase_letters = separate_letters_optimized(text)<\/p>\n<p>print(&quot;Uppercase letters:&quot;, uppercase_letters)<\/p>\n<p>print(&quot;Lowercase letters:&quot;, lowercase_letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5b57\u7b26\u8303\u56f4\u68c0\u67e5\u6765\u66ff\u4ee3 <code>isupper()<\/code> \u548c <code>islower()<\/code> \u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u578b\u5b57\u7b26\u4e32\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728 Python \u4e2d\u67e5\u627e\u548c\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5c0f\u5199\u5b57\u6bcd\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u662f\u5217\u8868\u89e3\u6790\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u533a\u5206\u5927\u5c0f\u5199\u5b57\u6bcd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u5b57\u7b26\u7684\u5927\u5c0f\u5199\u3002\u53ef\u4ee5\u4f7f\u7528<code>isupper()<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u5927\u5199\u5b57\u6bcd\uff0c\u4f7f\u7528<code>islower()<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\u662f\u5426\u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u60a8\u53ef\u4ee5\u5faa\u73af\u904d\u5386\u5e76\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8bc6\u522b\u5927\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5199\u548c\u5c0f\u5199\u5b57\u6bcd\uff1f<\/strong><br \/>\u786e\u5b9e\u5b58\u5728\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>isupper()<\/code>\u548c<code>islower()<\/code>\u65b9\u6cd5\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5199\u5b57\u6bcd\u548c\u5c0f\u5199\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c<code>[char for char in your_string if char.isupper()]<\/code>\u53ef\u4ee5\u63d0\u53d6\u6240\u6709\u5927\u5199\u5b57\u6bcd\uff0c\u800c<code>[char for char in your_string if char.islower()]<\/code>\u53ef\u4ee5\u63d0\u53d6\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5927\u5c0f\u5199\u5b57\u6bcd\u7684\u8ba1\u6570\uff1f<\/strong><br \/>\u4e3a\u4e86\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u5927\u5199\u548c\u5c0f\u5199\u5b57\u6bcd\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3002\u6bd4\u5982\uff0c<code>sum(1 for char in your_string if char.isupper())<\/code>\u5c06\u8fd4\u56de\u5927\u5199\u5b57\u6bcd\u7684\u6570\u91cf\uff0c\u800c<code>sum(1 for char in your_string if char.islower())<\/code>\u5219\u8fd4\u56de\u5c0f\u5199\u5b57\u6bcd\u7684\u6570\u91cf\u3002\u8fd9\u79cd\u65b9\u5f0f\u7b80\u5355\u6709\u6548\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u5f97\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u67e5\u627e\u5927\u5c0f\u5199\u5b57\u6bcd\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u6700\u4e3a\u7b80\u5355\u548c [&hellip;]","protected":false},"author":3,"featured_media":1116519,"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\/1116512"}],"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=1116512"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116512\/revisions"}],"predecessor-version":[{"id":1116522,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116512\/revisions\/1116522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1116519"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1116512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1116512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1116512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}