{"id":1096975,"date":"2025-01-08T15:07:06","date_gmt":"2025-01-08T07:07:06","guid":{"rendered":""},"modified":"2025-01-08T15:07:08","modified_gmt":"2025-01-08T07:07:08","slug":"python%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e6%9f%90%e4%b8%80%e7%b1%bb%e5%ad%97%e7%ac%a6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1096975.html","title":{"rendered":"python\u5982\u4f55\u7edf\u8ba1\u67d0\u4e00\u7c7b\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211937\/e283894f-ab19-4888-ba9b-1bbaa746d5e0.webp\" alt=\"python\u5982\u4f55\u7edf\u8ba1\u67d0\u4e00\u7c7b\u5b57\u7b26\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7edf\u8ba1\u67d0\u4e00\u7c7b\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<\/strong> \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u67d0\u4e00\u7c7b\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5b83\u4eec\u7684\u7528\u6cd5\u3002\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u65b9\u6cd5\u3001\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u3001\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5b57\u7b26\u7edf\u8ba1\u3002\u4f8b\u5982\uff0c<code>str.count()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u7edf\u8ba1\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>str.count()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>str.count()<\/code> \u662f\u4e00\u4e2a\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7edf\u8ba1\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u5b83\u7684\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! Hello Python!&quot;<\/p>\n<p>count = text.count(&#39;o&#39;)<\/p>\n<p>print(f&quot;The character &#39;o&#39; appears {count} times in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>text<\/code>\uff0c\u5e76\u4f7f\u7528 <code>text.count(&#39;o&#39;)<\/code> \u65b9\u6cd5\u7edf\u8ba1\u5b57\u7b26 <code>o<\/code> \u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u7ed3\u679c\u662f 3 \u6b21\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>str.isalpha()<\/code> \u65b9\u6cd5\u7edf\u8ba1\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u60f3\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u6bcd\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>str.isalpha()<\/code> \u65b9\u6cd5\u6765\u5224\u65ad\u6bcf\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! 123&quot;<\/p>\n<p>count = sum(1 for char in text if char.isalpha())<\/p>\n<p>print(f&quot;There are {count} letters in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7ed3\u5408 <code>sum()<\/code> \u51fd\u6570\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u6bcd\u7684\u6570\u91cf\u3002<code>char.isalpha()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u7edf\u8ba1\u5b57\u7b26\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\u5b57\u7b26\u662f\u5426\u5c5e\u4e8e\u6211\u4eec\u8981\u7edf\u8ba1\u7684\u7c7b\u522b\u3002<\/p>\n<\/p>\n<p><h4>1. \u7edf\u8ba1\u5b57\u6bcd\u548c\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd\u548c\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! 123&quot;<\/p>\n<p>letter_count = 0<\/p>\n<p>digit_count = 0<\/p>\n<p>for char in text:<\/p>\n<p>    if char.isalpha():<\/p>\n<p>        letter_count += 1<\/p>\n<p>    elif char.isdigit():<\/p>\n<p>        digit_count += 1<\/p>\n<p>print(f&quot;There are {letter_count} letters and {digit_count} digits in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528 <code>if<\/code> \u6761\u4ef6\u8bed\u53e5\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\u6216\u6570\u5b57\u3002\u5206\u522b\u7edf\u8ba1\u5b57\u6bcd\u548c\u6570\u5b57\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2. \u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u6570\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! Hello Python!&quot;<\/p>\n<p>char_to_count = &#39;H&#39;<\/p>\n<p>count = 0<\/p>\n<p>for char in text:<\/p>\n<p>    if char == char_to_count:<\/p>\n<p>        count += 1<\/p>\n<p>print(f&quot;The character &#39;{char_to_count}&#39; appears {count} times in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8981\u7edf\u8ba1\u5b57\u7b26 <code>H<\/code> \u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u6211\u4eec\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528 <code>if<\/code> \u6761\u4ef6\u8bed\u53e5\u5224\u65ad\u5f53\u524d\u5b57\u7b26\u662f\u5426\u4e3a <code>H<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\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\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u548c\u5b57\u7b26\u7edf\u8ba1\u3002Python \u7684 <code>re<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u7edf\u8ba1\u6240\u6709\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u6bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello World! 123&quot;<\/p>\n<p>pattern = r&#39;[A-Za-z]&#39;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>count = len(matches)<\/p>\n<p>print(f&quot;There are {count} letters in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>[A-Za-z]<\/code> \u6765\u5339\u914d\u6240\u6709\u5b57\u6bcd\uff0c\u5e76\u4f7f\u7528 <code>re.findall()<\/code> \u65b9\u6cd5\u67e5\u627e\u6240\u6709\u5339\u914d\u9879\u3002<code>len(matches)<\/code> \u8fd4\u56de\u5339\u914d\u9879\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2. \u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u6570\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello World! Hello Python!&quot;<\/p>\n<p>char_to_count = &#39;o&#39;<\/p>\n<p>pattern = re.escape(char_to_count)<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>count = len(matches)<\/p>\n<p>print(f&quot;The character &#39;{char_to_count}&#39; appears {count} times in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>re.escape()<\/code> \u51fd\u6570\u5bf9\u7279\u5b9a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\uff0c\u4ee5\u4fbf\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u5b89\u5168\u4f7f\u7528\u3002\u7136\u540e\u4f7f\u7528 <code>re.findall()<\/code> \u65b9\u6cd5\u67e5\u627e\u6240\u6709\u5339\u914d\u9879\uff0c\u5e76\u8ba1\u7b97\u5339\u914d\u9879\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Counter\u7c7b<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>collections<\/code> \u6a21\u5757\u4e2d\u7684 <code>Counter<\/code> \u7c7b\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7edf\u8ba1\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u7edf\u8ba1\u6240\u6709\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>Counter<\/code> \u7c7b\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>text = &quot;Hello World! Hello Python!&quot;<\/p>\n<p>counter = Counter(text)<\/p>\n<p>print(counter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Counter(text)<\/code> \u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u7edf\u8ba1\u4e3a\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u5176\u4e2d\u952e\u662f\u5b57\u7b26\uff0c\u503c\u662f\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>Counter<\/code> \u7c7b\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u6570\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>text = &quot;Hello World! Hello Python!&quot;<\/p>\n<p>char_to_count = &#39;o&#39;<\/p>\n<p>counter = Counter(text)<\/p>\n<p>count = counter[char_to_count]<\/p>\n<p>print(f&quot;The character &#39;{char_to_count}&#39; appears {count} times in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Counter(text)<\/code> \u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u952e\u662f\u5b57\u7b26\uff0c\u503c\u662f\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7279\u5b9a\u5b57\u7b26\u7684\u8ba1\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7edf\u8ba1\u5927\u5199\u5b57\u6bcd\u548c\u5c0f\u5199\u5b57\u6bcd<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5206\u522b\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5199\u5b57\u6bcd\u548c\u5c0f\u5199\u5b57\u6bcd\u3002\u53ef\u4ee5\u7ed3\u5408\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1. \u7edf\u8ba1\u5927\u5199\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>str.isupper()<\/code> \u65b9\u6cd5\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5199\u5b57\u6bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! HELLO PYTHON!&quot;<\/p>\n<p>count = sum(1 for char in text if char.isupper())<\/p>\n<p>print(f&quot;There are {count} uppercase letters in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7ed3\u5408 <code>sum()<\/code> \u51fd\u6570\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5199\u5b57\u6bcd\u6570\u91cf\u3002<code>char.isupper()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u5927\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><h4>2. \u7edf\u8ba1\u5c0f\u5199\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>str.islower()<\/code> \u65b9\u6cd5\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5c0f\u5199\u5b57\u6bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! hello python!&quot;<\/p>\n<p>count = sum(1 for char in text if char.islower())<\/p>\n<p>print(f&quot;There are {count} lowercase letters in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7ed3\u5408 <code>sum()<\/code> \u51fd\u6570\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5c0f\u5199\u5b57\u6bcd\u6570\u91cf\u3002<code>char.islower()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7edf\u8ba1\u5143\u97f3\u5b57\u6bcd\u548c\u8f85\u97f3\u5b57\u6bcd<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u97f3\u5b57\u6bcd\u548c\u8f85\u97f3\u5b57\u6bcd\u3002\u5143\u97f3\u5b57\u6bcd\u5305\u62ec <code>a, e, i, o, u<\/code>\uff0c\u8f85\u97f3\u5b57\u6bcd\u662f\u9664\u4e86\u5143\u97f3\u5b57\u6bcd\u4e4b\u5916\u7684\u6240\u6709\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><h4>1. \u7edf\u8ba1\u5143\u97f3\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u97f3\u5b57\u6bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! Hello Python!&quot;<\/p>\n<p>vowels = set(&#39;aeiouAEIOU&#39;)<\/p>\n<p>count = sum(1 for char in text if char in vowels)<\/p>\n<p>print(f&quot;There are {count} vowels in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5143\u97f3\u5b57\u6bcd\u7684\u96c6\u5408 <code>vowels<\/code>\uff0c\u5e76\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7ed3\u5408 <code>sum()<\/code> \u51fd\u6570\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u97f3\u5b57\u6bcd\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2. \u7edf\u8ba1\u8f85\u97f3\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u8f85\u97f3\u5b57\u6bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! Hello Python!&quot;<\/p>\n<p>vowels = set(&#39;aeiouAEIOU&#39;)<\/p>\n<p>count = sum(1 for char in text if char.isalpha() and char not in vowels)<\/p>\n<p>print(f&quot;There are {count} consonants in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7ed3\u5408 <code>sum()<\/code> \u51fd\u6570\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u8f85\u97f3\u5b57\u6bcd\u6570\u91cf\u3002\u6211\u4eec\u4f7f\u7528 <code>char.isalpha()<\/code> \u65b9\u6cd5\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\uff0c\u5e76\u4f7f\u7528 <code>char not in vowels<\/code> \u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u8f85\u97f3\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7edf\u8ba1\u6807\u70b9\u7b26\u53f7<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u7edf\u8ba1\u6807\u70b9\u7b26\u53f7<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>string.punctuation<\/code> \u5e38\u91cf\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import string<\/p>\n<p>text = &quot;Hello, World! How are you?&quot;<\/p>\n<p>count = sum(1 for char in text if char in string.punctuation)<\/p>\n<p>print(f&quot;There are {count} punctuation marks in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>string.punctuation<\/code> \u5e38\u91cf\uff0c\u5b83\u5305\u542b\u6240\u6709\u5e38\u89c1\u7684\u6807\u70b9\u7b26\u53f7\uff0c\u5e76\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7ed3\u5408 <code>sum()<\/code> \u51fd\u6570\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7edf\u8ba1\u6807\u70b9\u7b26\u53f7<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, World! How are you?&quot;<\/p>\n<p>pattern = r&#39;[^\\w\\s]&#39;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>count = len(matches)<\/p>\n<p>print(f&quot;There are {count} punctuation marks in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f <code>[^\\w\\s]<\/code> \u7528\u4e8e\u5339\u914d\u6240\u6709\u975e\u5355\u8bcd\u5b57\u7b26\u548c\u975e\u7a7a\u767d\u5b57\u7b26\uff0c\u5373\u6807\u70b9\u7b26\u53f7\u3002\u6211\u4eec\u4f7f\u7528 <code>re.findall()<\/code> \u65b9\u6cd5\u67e5\u627e\u6240\u6709\u5339\u914d\u9879\uff0c\u5e76\u8ba1\u7b97\u5339\u914d\u9879\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7edf\u8ba1\u7a7a\u683c\u548c\u5176\u4ed6\u7a7a\u767d\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u7a7a\u767d\u5b57\u7b26\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u548c\u6362\u884c\u7b26\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u7edf\u8ba1\u7a7a\u767d\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>str.isspace()<\/code> \u65b9\u6cd5\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! \\nHello\\tPython!&quot;<\/p>\n<p>count = sum(1 for char in text if char.isspace())<\/p>\n<p>print(f&quot;There are {count} whitespace characters in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7ed3\u5408 <code>sum()<\/code> \u51fd\u6570\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\u6570\u91cf\u3002<code>char.isspace()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7edf\u8ba1\u7a7a\u767d\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello World! \\nHello\\tPython!&quot;<\/p>\n<p>pattern = r&#39;\\s&#39;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>count = len(matches)<\/p>\n<p>print(f&quot;There are {count} whitespace characters in the text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f <code>\\s<\/code> \u7528\u4e8e\u5339\u914d\u6240\u6709\u7a7a\u767d\u5b57\u7b26\u3002\u6211\u4eec\u4f7f\u7528 <code>re.findall()<\/code> \u65b9\u6cd5\u67e5\u627e\u6240\u6709\u5339\u914d\u9879\uff0c\u5e76\u8ba1\u7b97\u5339\u914d\u9879\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 Python \u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e00\u7c7b\u5b57\u7b26\u3002\u6211\u4eec\u63a2\u8ba8\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001<code>Counter<\/code> \u7c7b\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u8bfb\u8005\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7edf\u8ba1\u5b57\u7b26\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u8981\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>count<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26&#39;a&#39;\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>your_string.count(&#39;a&#39;)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6709\u6548\uff0c\u9002\u5408\u5904\u7406\u8f83\u77ed\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u6bcd\u7684\u9891\u7387\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>Counter<\/code>\u7c7b\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u6bcd\u7684\u9891\u7387\u3002\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\nyour_string = &quot;example string&quot;\nfrequency = Counter(your_string)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u975e\u5e38\u9002\u5408\u8fdb\u884c\u5b57\u7b26\u9891\u7387\u5206\u6790\u3002<\/p>\n<p><strong>\u5bf9\u4e8e\u5927\u578b\u6587\u672c\u6587\u4ef6\uff0c\u5982\u4f55\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u6570\u91cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u6765\u8bfb\u53d6\u6587\u4ef6\u5e76\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u6570\u91cf\u3002\u901a\u8fc7\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u4f7f\u7528<code>count<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u578b\u6587\u672c\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;your_file.txt&#39;, &#39;r&#39;) as file:\n    text = file.read()\n    character_count = text.count(&#39;a&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u660e\u4e86\uff0c\u8fd8\u80fd\u6709\u6548\u5904\u7406\u5927\u6587\u4ef6\uff0c\u907f\u514d\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7edf\u8ba1\u67d0\u4e00\u7c7b\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002 \u5728\u8fd9\u7bc7\u6587 [&hellip;]","protected":false},"author":3,"featured_media":1096979,"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\/1096975"}],"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=1096975"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096975\/revisions"}],"predecessor-version":[{"id":1096982,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096975\/revisions\/1096982"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096979"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}