{"id":1107902,"date":"2025-01-08T16:54:30","date_gmt":"2025-01-08T08:54:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1107902.html"},"modified":"2025-01-08T16:54:32","modified_gmt":"2025-01-08T08:54:32","slug":"python%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e5%ad%97%e6%af%8d%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1107902.html","title":{"rendered":"python\u5982\u4f55\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071747\/5cdf9f0b-7044-40bf-b6e5-99538b238b5b.webp\" alt=\"python\u5982\u4f55\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>Python\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u96c6\u5408\u3001\u5b57\u5178\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u5b57\u6bcd\u53ca\u5176\u8ba1\u6570\u3001\u4f7f\u7528collections.Counter\u7c7b\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u4ee3\u7801\u793a\u4f8b\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u6765\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u7edf\u8ba1\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32<\/strong>\u662f\u6700\u57fa\u672c\u7684\u7edf\u8ba1\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u5224\u65ad\u8be5\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\uff0c\u5e76\u8fdb\u884c\u8ba1\u6570\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u521d\u5b66\u8005\u548c\u5c0f\u89c4\u6a21\u5b57\u7b26\u4e32\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u662f\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u4e2a\u6570\u7684\u6700\u57fa\u672c\u65b9\u6cd5\u3002\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u5224\u65ad\u8be5\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\uff0c\u5e76\u8fdb\u884c\u8ba1\u6570\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u521d\u5b66\u8005\u548c\u5c0f\u89c4\u6a21\u5b57\u7b26\u4e32\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_letters(string):<\/p>\n<p>    count = 0<\/p>\n<p>    for char in string:<\/p>\n<p>        if char.isalpha():<\/p>\n<p>            count += 1<\/p>\n<p>    return count<\/p>\n<p>sample_string = &quot;Hello, World! 123&quot;<\/p>\n<p>print(count_letters(sample_string))  # Output: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>isalpha()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\u3002\u5982\u679c\u662f\u5b57\u6bcd\uff0c\u5219\u8ba1\u6570\u52a01\u3002\u6700\u7ec8\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u6bcd\u7684\u603b\u6570\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u5b57\u6bcd\u53ca\u5176\u8ba1\u6570<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u9002\u5408\u5b58\u50a8\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\u3002\u901a\u8fc7\u5b57\u5178\uff0c\u53ef\u4ee5\u8bb0\u5f55\u6bcf\u4e2a\u5b57\u6bcd\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u4fbf\u4e8e\u540e\u7eed\u7edf\u8ba1\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_letters(string):<\/p>\n<p>    letter_count = {}<\/p>\n<p>    for char in string:<\/p>\n<p>        if char.isalpha():<\/p>\n<p>            char = char.lower()<\/p>\n<p>            if char in letter_count:<\/p>\n<p>                letter_count[char] += 1<\/p>\n<p>            else:<\/p>\n<p>                letter_count[char] = 1<\/p>\n<p>    return letter_count<\/p>\n<p>sample_string = &quot;Hello, World!&quot;<\/p>\n<p>print(count_letters(sample_string))  # Output: {&#39;h&#39;: 1, &#39;e&#39;: 1, &#39;l&#39;: 3, &#39;o&#39;: 2, &#39;w&#39;: 1, &#39;r&#39;: 1, &#39;d&#39;: 1}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u5178<code>letter_count<\/code>\u7528\u4e8e\u5b58\u50a8\u6bcf\u4e2a\u5b57\u6bcd\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\u3002\u904d\u5386\u5b57\u7b26\u4e32\u65f6\uff0c\u5982\u679c\u5b57\u7b26\u662f\u5b57\u6bcd\u4e14\u5df2\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u8ba1\u6570\u52a01\uff1b\u5426\u5219\uff0c\u521d\u59cb\u5316\u4e3a1\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e09\u3001\u4f7f\u7528collections.Counter\u7c7b<\/p>\n<\/p>\n<p><p><code>collections.Counter<\/code>\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u8ba1\u6570\u5668\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002\u4f7f\u7528<code>Counter<\/code>\u7c7b\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_letters(string):<\/p>\n<p>    filtered_string = [char.lower() for char in string if char.isalpha()]<\/p>\n<p>    return Counter(filtered_string)<\/p>\n<p>sample_string = &quot;Hello, World!&quot;<\/p>\n<p>print(count_letters(sample_string))  # Output: Counter({&#39;l&#39;: 3, &#39;o&#39;: 2, &#39;h&#39;: 1, &#39;e&#39;: 1, &#39;w&#39;: 1, &#39;r&#39;: 1, &#39;d&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd\uff0c\u5e76\u8f6c\u6362\u4e3a\u5c0f\u5199\u3002\u7136\u540e\u4f7f\u7528<code>Counter<\/code>\u7c7b\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u6bcd\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u56db\u3001\u4f7f\u7528\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\u5904\u7406\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u63d0\u53d6\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd\uff0c\u5e76\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<p>def count_letters(string):<\/p>\n<p>    letters = re.findall(r&#39;[a-zA-Z]&#39;, string)<\/p>\n<p>    return Counter(letters)<\/p>\n<p>sample_string = &quot;Hello, World! 123&quot;<\/p>\n<p>print(count_letters(sample_string))  # Output: Counter({&#39;l&#39;: 3, &#39;o&#39;: 2, &#39;H&#39;: 1, &#39;e&#39;: 1, &#39;W&#39;: 1, &#39;r&#39;: 1, &#39;d&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>re.findall()<\/code>\u65b9\u6cd5\u7528\u4e8e\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u6bcd\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u7136\u540e\u4f7f\u7528<code>Counter<\/code>\u7c7b\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u6bcd\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e94\u3001\u6027\u80fd\u5bf9\u6bd4\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u7edf\u8ba1\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u7684\u89c4\u6a21\u548c\u590d\u6742\u5ea6\u3002\u5bf9\u4e8e\u5c0f\u89c4\u6a21\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6216\u5b57\u5178\u5b58\u50a8\u5373\u53ef\u6ee1\u8db3\u9700\u6c42\uff1b\u5bf9\u4e8e\u5927\u89c4\u6a21\u5b57\u7b26\u4e32\u6216\u9700\u8981\u9891\u7e41\u7edf\u8ba1\u7684\u573a\u666f\uff0c\u5efa\u8bae\u4f7f\u7528<code>collections.Counter<\/code>\u7c7b\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32<\/strong>\uff1a\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u5b57\u7b26\u4e32\u7edf\u8ba1\uff0c\u4ee3\u7801\u7b80\u5355\u76f4\u89c2\u3002<\/li>\n<li><strong>\u5b57\u5178\u5b58\u50a8\u5b57\u6bcd\u53ca\u5176\u8ba1\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u8bb0\u5f55\u6bcf\u4e2a\u5b57\u6bcd\u51fa\u73b0\u6b21\u6570\u7684\u573a\u666f\uff0c\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\u3002<\/li>\n<li><strong>collections.Counter\u7c7b<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u5b57\u7b26\u4e32\u7edf\u8ba1\uff0c\u4ee3\u7801\u7b80\u6d01\u9ad8\u6548\u3002<\/li>\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u9002\u7528\u4e8e\u590d\u6742\u5b57\u7b26\u4e32\u5339\u914d\u548c\u63d0\u53d6\uff0c\u7075\u6d3b\u6027\u9ad8\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7edf\u8ba1\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5728\u6587\u672c\u5206\u6790\u3001\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u9886\u57df\uff0c\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u4e2a\u6570\u662f\u57fa\u7840\u64cd\u4f5c\u4e4b\u4e00\uff0c\u5408\u7406\u9009\u62e9\u7edf\u8ba1\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u516d\u3001\u4f18\u5316\u548c\u6269\u5c55<\/p>\n<\/p>\n<p><p>\u5728\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u4e2a\u6570\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u4f18\u5316\u548c\u6269\u5c55\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5ffd\u7565\u5927\u5c0f\u5199\u3001\u6392\u9664\u7279\u5b9a\u5b57\u7b26\u3001\u7edf\u8ba1\u5b57\u6bcd\u4ee5\u5916\u7684\u5176\u4ed6\u5b57\u7b26\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5ffd\u7565\u5927\u5c0f\u5199<\/strong>\uff1a\u5c06\u6240\u6709\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\uff0c\u7edf\u4e00\u7edf\u8ba1\u3002<\/li>\n<li><strong>\u6392\u9664\u7279\u5b9a\u5b57\u7b26<\/strong>\uff1a\u5728\u7edf\u8ba1\u524d\uff0c\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u7684\u5b57\u7b26\u3002<\/li>\n<li><strong>\u7edf\u8ba1\u5b57\u6bcd\u4ee5\u5916\u7684\u5176\u4ed6\u5b57\u7b26<\/strong>\uff1a\u6269\u5c55\u7edf\u8ba1\u8303\u56f4\uff0c\u5305\u62ec\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u7b49\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u7edf\u8ba1\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u4f18\u5316\u548c\u6269\u5c55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<p>def count_letters(string, ignore_case=True, exclude_chars=None):<\/p>\n<p>    if ignore_case:<\/p>\n<p>        string = string.lower()<\/p>\n<p>    if exclude_chars:<\/p>\n<p>        string = &#39;&#39;.join(char for char in string if char not in exclude_chars)<\/p>\n<p>    letters = re.findall(r&#39;[a-z]&#39;, string)<\/p>\n<p>    return Counter(letters)<\/p>\n<p>sample_string = &quot;Hello, World! 123&quot;<\/p>\n<p>exclude_chars = [&#39;l&#39;, &#39;o&#39;]<\/p>\n<p>print(count_letters(sample_string, ignore_case=True, exclude_chars=exclude_chars))  # Output: Counter({&#39;h&#39;: 1, &#39;e&#39;: 1, &#39;w&#39;: 1, &#39;r&#39;: 1, &#39;d&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u53c2\u6570<code>ignore_case<\/code>\u548c<code>exclude_chars<\/code>\uff0c\u53ef\u4ee5\u7075\u6d3b\u63a7\u5236\u7edf\u8ba1\u8fc7\u7a0b\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u591a\u79cd\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u4e2a\u6570\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u5b58\u50a8\u5b57\u6bcd\u53ca\u5176\u8ba1\u6570\u3001collections.Counter\u7c7b\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u8fdb\u884c\u4f18\u5316\u548c\u6269\u5c55\uff0c\u4ee5\u63d0\u9ad8\u7edf\u8ba1\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u6709\u6548\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u7684\u4e2a\u6570\uff0c\u8fd8\u80fd\u5728\u6587\u672c\u5206\u6790\u3001\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u9886\u57df\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u63d0\u5347\u6570\u636e\u5904\u7406\u80fd\u529b\u548c\u9879\u76ee\u5f00\u53d1\u6c34\u5e73\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u7684\u5b57\u7b26\u4e32\u5904\u7406\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd\u6570\u91cf\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u9ad8\u6548\u5730\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd\u6570\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528<code>isalpha()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6bcf\u4e2a\u5b57\u7b26\u662f\u5426\u662f\u5b57\u6bcd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">def count_letters(s):\n    return sum(1 for char in s if char.isalpha())\n\n# \u793a\u4f8b\ninput_string = &quot;Hello, World! 123&quot;\nletter_count = count_letters(input_string)\nprint(f&quot;\u5b57\u6bcd\u6570\u91cf: {letter_count}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u4f1a\u8f93\u51fa\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u6bcd\u7684\u6570\u91cf\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5ffd\u7565\u5927\u5c0f\u5199\u7edf\u8ba1\u5b57\u6bcd\u6570\u91cf\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u5e0c\u671b\u5728\u7edf\u8ba1\u65f6\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>isalpha()<\/code>\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u672c\u8eab\u4f1a\u8fd4\u56deTrue\u6216False\uff0c\u9002\u7528\u4e8e\u6240\u6709\u5b57\u6bcd\u3002\u793a\u4f8b\u4ee3\u7801\u4e0e\u4e4b\u524d\u76f8\u540c\uff0c\u56e0\u4e3a<code>isalpha()<\/code>\u65b9\u6cd5\u5e76\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u4e0d\u540c\u5b57\u6bcd\u7684\u9891\u7387\uff1f<\/strong><\/p>\n<p>\u8981\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u51fa\u73b0\u7684\u9891\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>Counter<\/code>\u7c7b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\ndef letter_frequency(s):\n    return Counter(char.lower() for char in s if char.isalpha())\n\n# \u793a\u4f8b\ninput_string = &quot;Hello, World!&quot;\nfrequency = letter_frequency(input_string)\nprint(f&quot;\u5b57\u6bcd\u9891\u7387: {frequency}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u4f1a\u8f93\u51fa\u4e00\u4e2a\u5b57\u5178\uff0c\u663e\u793a\u6bcf\u4e2a\u5b57\u6bcd\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u96c6\u5408\u3001\u5b57\u5178\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32 [&hellip;]","protected":false},"author":3,"featured_media":1107906,"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\/1107902"}],"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=1107902"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107902\/revisions"}],"predecessor-version":[{"id":1107907,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107902\/revisions\/1107907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1107906"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1107902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1107902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1107902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}