{"id":1139076,"date":"2025-01-08T22:06:36","date_gmt":"2025-01-08T14:06:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139076.html"},"modified":"2025-01-08T22:06:39","modified_gmt":"2025-01-08T14:06:39","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e6%af%8f%e4%b8%aa%e5%ad%97%e7%ac%a6%e5%87%ba%e7%8e%b0%e7%9a%84%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1139076.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102509\/c1fb8f10-ad19-4e95-8dd6-a04e5c6949f4.webp\" alt=\"python\u4e2d\u5982\u4f55\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u4e2a\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528\u5b57\u5178\u3001collections.Counter\u6a21\u5757\u548cpandas\u5e93\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u5178\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570<\/strong>\uff1a\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5c06\u5176\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u5e76\u8bb0\u5f55\u5176\u51fa\u73b0\u7684\u6b21\u6570\u4f5c\u4e3a\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u5feb\u901f\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u7840\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u7684\u57fa\u7840\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528\u5b57\u5178\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_characters(s):<\/p>\n<p>    count_dict = {}<\/p>\n<p>    for char in s:<\/p>\n<p>        if char in count_dict:<\/p>\n<p>            count_dict[char] += 1<\/p>\n<p>        else:<\/p>\n<p>            count_dict[char] = 1<\/p>\n<p>    return count_dict<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;hello world&quot;<\/p>\n<p>print(count_characters(s))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5b57\u5178 <code>count_dict<\/code>\uff0c\u7136\u540e\u904d\u5386\u5b57\u7b26\u4e32 <code>s<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002\u5982\u679c\u5b57\u7b26\u5df2\u7ecf\u5728\u5b57\u5178\u4e2d\uff0c\u6211\u4eec\u5c31\u5c06\u5176\u5bf9\u5e94\u7684\u503c\u52a01\uff1b\u5426\u5219\uff0c\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u5e76\u8bbe\u7f6e\u521d\u59cb\u503c\u4e3a1\u3002\u6700\u7ec8\u8fd4\u56de\u5b57\u5178 <code>count_dict<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528 defaultdict<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>collections<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a <code>defaultdict<\/code> \u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u81ea\u52a8\u521b\u5efa\u952e\u5e76\u8d4b\u4e88\u9ed8\u8ba4\u503c\u3002\u4f7f\u7528 <code>defaultdict<\/code> \u53ef\u4ee5\u7b80\u5316\u6211\u4eec\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>def count_characters(s):<\/p>\n<p>    count_dict = defaultdict(int)<\/p>\n<p>    for char in s:<\/p>\n<p>        count_dict[char] += 1<\/p>\n<p>    return count_dict<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;hello world&quot;<\/p>\n<p>print(count_characters(s))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>defaultdict<\/code> \u6765\u4ee3\u66ff\u666e\u901a\u7684\u5b57\u5178\u3002<code>defaultdict<\/code> \u4f1a\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u81ea\u52a8\u521b\u5efa\u952e\u5e76\u8d4b\u4e88\u9ed8\u8ba4\u503c <code>0<\/code>\uff0c\u56e0\u6b64\u6211\u4eec\u4e0d\u518d\u9700\u8981\u68c0\u67e5\u5b57\u7b26\u662f\u5426\u5df2\u7ecf\u5728\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 collections.Counter<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u7840\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>collections<\/code> \u6a21\u5757\u4e2d\u7684 <code>Counter<\/code> \u7c7b\u662f\u4e13\u95e8\u7528\u6765\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u7684\u5de5\u5177\u3002\u5b83\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u4e14\u6548\u7387\u5f88\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_characters(s):<\/p>\n<p>    return Counter(s)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;hello world&quot;<\/p>\n<p>print(count_characters(s))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32 <code>s<\/code> \u4f20\u9012\u7ed9 <code>Counter<\/code> \u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c<code>Counter<\/code> \u4f1a\u81ea\u52a8\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a <code>Counter<\/code> \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u7c7b\u4f3c\u4e8e\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>2\u3001Counter \u7684\u5176\u4ed6\u529f\u80fd<\/h3>\n<\/p>\n<p><p><code>Counter<\/code> \u4e0d\u4ec5\u53ef\u4ee5\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>most_common<\/code> \u65b9\u6cd5\u6765\u83b7\u53d6\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u524d n \u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def most_common_characters(s, n):<\/p>\n<p>    return Counter(s).most_common(n)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;hello world&quot;<\/p>\n<p>print(most_common_characters(s, 2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>most_common<\/code> \u65b9\u6cd5\u83b7\u53d6\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u524d 2 \u4e2a\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528 pandas \u8fdb\u884c\u7edf\u8ba1<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u7840\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>pandas<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u867d\u7136 <code>pandas<\/code> \u4e3b\u8981\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def count_characters(s):<\/p>\n<p>    return pd.Series(list(s)).value_counts()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;hello world&quot;<\/p>\n<p>print(count_characters(s))<\/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 <code>s<\/code> \u8f6c\u6362\u4e3a\u5b57\u7b26\u5217\u8868\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9 <code>pd.Series<\/code> \u521b\u5efa\u4e00\u4e2a <code>Series<\/code> \u5bf9\u8c61\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528 <code>value_counts<\/code> \u65b9\u6cd5\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7edf\u8ba1\u7ed3\u679c\u7684\u8fdb\u4e00\u6b65\u5904\u7406<\/h3>\n<\/p>\n<p><p><code>pandas<\/code> \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7edf\u8ba1\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7edf\u8ba1\u7ed3\u679c\u8f6c\u6362\u4e3a\u5b57\u5178\u6216\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def count_characters(s):<\/p>\n<p>    count_series = pd.Series(list(s)).value_counts()<\/p>\n<p>    return count_series.to_dict()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;hello world&quot;<\/p>\n<p>print(count_characters(s))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u7edf\u8ba1\u7ed3\u679c\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><h3>1\u3001\u6027\u80fd\u6d4b\u8bd5\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u6d4b\u8bd5\u51fd\u6570\uff0c\u7edf\u8ba1\u6bcf\u79cd\u65b9\u6cd5\u5904\u7406\u5927\u5b57\u7b26\u4e32\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def performance_test(method, s):<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    method(s)<\/p>\n<p>    end_time = time.time()<\/p>\n<p>    return end_time - start_time<\/p>\n<h2><strong>\u793a\u4f8b\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>s = &quot;hello world&quot; * 10000<\/p>\n<h2><strong>\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd<\/strong><\/h2>\n<p>print(&quot;Dictionary method:&quot;, performance_test(count_characters, s))<\/p>\n<p>print(&quot;Defaultdict method:&quot;, performance_test(count_characters_defaultdict, s))<\/p>\n<p>print(&quot;Counter method:&quot;, performance_test(count_characters_counter, s))<\/p>\n<p>print(&quot;Pandas method:&quot;, performance_test(count_characters_pandas, s))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>performance_test<\/code> \u51fd\u6570\uff0c\u8ba1\u7b97\u6267\u884c\u6bcf\u79cd\u65b9\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6027\u80fd\u6d4b\u8bd5\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c<code>collections.Counter<\/code> \u65b9\u6cd5\u7684\u6027\u80fd\u6700\u4f73\uff0c\u5176\u6b21\u662f\u4f7f\u7528\u5b57\u5178\u548c <code>defaultdict<\/code> \u65b9\u6cd5\uff0c<code>pandas<\/code> \u65b9\u6cd5\u7684\u6027\u80fd\u8f83\u5dee\u3002\u5177\u4f53\u7ed3\u679c\u53ef\u80fd\u56e0\u5b57\u7b26\u4e32\u957f\u5ea6\u548c\u5b57\u7b26\u79cd\u7c7b\u800c\u5f02\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u3001<code>collections.Counter<\/code> \u548c <code>pandas<\/code>\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 <code>collections.Counter<\/code> \u662f\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u7b80\u5355\u9ad8\u6548\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u989d\u5916\u7684\u529f\u80fd\u3002\u5bf9\u4e8e\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u5206\u6790\u7ed3\u679c\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 <code>pandas<\/code>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\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\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>Counter<\/code>\u7c7b\u6765\u8f7b\u677e\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\ntext = &quot;hello world&quot;\nchar_count = Counter(text)\nprint(char_count)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u6bcf\u4e2a\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u65b9\u4fbf\u7528\u6237\u67e5\u770b\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4f7f\u7528\u5b57\u5178\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u5728\u5b57\u5178\u4e2d\u66f4\u65b0\u5176\u8ba1\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;hello world&quot;\nchar_count = {}\n\nfor char in text:\n    if char in char_count:\n        char_count[char] += 1\n    else:\n        char_count[char] = 1\n\nprint(char_count)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u5408\u60f3\u8981\u6df1\u5165\u4e86\u89e3\u6570\u636e\u7ed3\u6784\u7684\u7528\u6237\u3002<\/p>\n<p><strong>\u5728\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u65f6\uff0c\u5982\u4f55\u5ffd\u7565\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u7edf\u8ba1\u65f6\u5ffd\u7565\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u5b57\u7b26\u4e32\u65f6\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import string\n\ntext = &quot;hello, world!&quot;\nchar_count = {}\n\nfor char in text:\n    if char not in string.whitespace and char not in string.punctuation:\n        if char in char_count:\n            char_count[char] += 1\n        else:\n            char_count[char] = 1\n\nprint(char_count)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u5b57\u7b26\u7edf\u8ba1\u53ea\u5305\u62ec\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u5bf9\u4e8e\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u4e2a\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528\u5b57\u5178\u3001collections.Counter\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1139087,"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\/1139076"}],"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=1139076"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139076\/revisions"}],"predecessor-version":[{"id":1139089,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139076\/revisions\/1139089"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139087"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}