{"id":1025878,"date":"2024-12-31T10:37:48","date_gmt":"2024-12-31T02:37:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1025878.html"},"modified":"2024-12-31T10:37:51","modified_gmt":"2024-12-31T02:37:51","slug":"python%e9%87%8c%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e5%ad%97%e5%87%ba%e7%8e%b0%e7%9a%84%e9%a2%91%e7%8e%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1025878.html","title":{"rendered":"python\u91cc\u5982\u4f55\u7edf\u8ba1\u5b57\u51fa\u73b0\u7684\u9891\u7387"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/94731767-8506-4079-8aa3-3ee23e0cecd4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u91cc\u5982\u4f55\u7edf\u8ba1\u5b57\u51fa\u73b0\u7684\u9891\u7387\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u7edf\u8ba1\u5b57\u51fa\u73b0\u7684\u9891\u7387\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3001Counter\u7c7b\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u3002<strong>\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528Counter\u7c7b\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u3002\u5176\u4e2d\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Counter\u7c7b\u3002Counter\u7c7b\u662fcollections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u7edf\u8ba1\u53ef\u54c8\u5e0c\u5bf9\u8c61\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u7edf\u8ba1\u5b57\u51fa\u73b0\u7684\u9891\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u662f\u6700\u57fa\u7840\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5c06\u5b57\u7b26\u4f5c\u4e3a\u952e\uff0c\u51fa\u73b0\u6b21\u6570\u4f5c\u4e3a\u503c\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_char_frequency(s):<\/p>\n<p>    freq = {}<\/p>\n<p>    for char in s:<\/p>\n<p>        if char in freq:<\/p>\n<p>            freq[char] += 1<\/p>\n<p>        else:<\/p>\n<p>            freq[char] = 1<\/p>\n<p>    return freq<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>frequency = count_char_frequency(text)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>count_char_frequency<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8f93\u5165\u3002\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<code>freq<\/code>\uff0c\u7136\u540e\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002\u5982\u679c\u5b57\u7b26\u5df2\u7ecf\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u6211\u4eec\u5c06\u5176\u503c\u589e\u52a01\uff1b\u5426\u5219\uff0c\u6211\u4eec\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u5e76\u5c06\u503c\u8bbe\u7f6e\u4e3a1\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u61c2\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002  <\/li>\n<li>\u6ca1\u6709\u4f9d\u8d56\u5916\u90e8\u6a21\u5757\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u76f8\u5bf9\u5197\u957f\u3002  <\/li>\n<li>\u5bf9\u4e8e\u5927\u5b57\u7b26\u4e32\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982Counter\u7c7b\u3002<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001\u4f7f\u7528Counter\u7c7b<\/p>\n<\/p>\n<p><p>Counter\u7c7b\u662fcollections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u7684\u9891\u7387\u3002\u4f7f\u7528Counter\u7c7b\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>frequency = Counter(text)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4ececollections\u6a21\u5757\u5bfc\u5165Counter\u7c7b\uff0c\u7136\u540e\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9Counter\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002Counter\u7c7b\u4f1a\u81ea\u52a8\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u9891\u7387\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01\u3002  <\/li>\n<li>\u6027\u80fd\u8f83\u597d\uff0c\u9002\u5408\u5904\u7406\u5927\u5b57\u7b26\u4e32\u3002  <\/li>\n<li>\u63d0\u4f9b\u4e86\u8bb8\u591a\u5b9e\u7528\u7684\u65b9\u6cd5\uff0c\u5982<code>most_common<\/code>\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u9891\u7387\u6700\u9ad8\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5bfc\u5165collections\u6a21\u5757\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5b57\u5178\u6765\u7edf\u8ba1\u5b57\u7b26\u9891\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_char_frequency(s):<\/p>\n<p>    return {char: s.count(char) for char in set(s)}<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>frequency = count_char_frequency(text)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>count_char_frequency<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8f93\u5165\u3002\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u503c\u662f\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01\u3002  <\/li>\n<li>\u4f7f\u7528\u6807\u51c6\u5e93\uff0c\u65e0\u9700\u5bfc\u5165\u989d\u5916\u6a21\u5757\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5927\u5b57\u7b26\u4e32\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982Counter\u7c7b\u548c\u76f4\u63a5\u4f7f\u7528\u5b57\u5178\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u7edf\u8ba1\u5b57\u7b26\u9891\u7387\uff0c\u5982\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001Pandas\u5e93\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u9002\u7528\u4e8e\u7279\u5b9a\u573a\u666f\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5b57\u5178\u548cCounter\u7c7b\u5df2\u7ecf\u8db3\u591f\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\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\u6765\u7edf\u8ba1\u5b57\u7b26\u9891\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import defaultdict<\/p>\n<p>def count_char_frequency(s):<\/p>\n<p>    freq = defaultdict(int)<\/p>\n<p>    for char in re.findall(r&#39;.&#39;, s):<\/p>\n<p>        freq[char] += 1<\/p>\n<p>    return freq<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>frequency = count_char_frequency(text)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>re.findall<\/code>\u51fd\u6570\u627e\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\uff0c\u7136\u540e\u4f7f\u7528<code>defaultdict<\/code>\u7edf\u8ba1\u9891\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u901a\u5e38\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5b57\u7b26\u9891\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>frequency = pd.Series(list(text)).value_counts()<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528Pandas\u7684<code>value_counts<\/code>\u51fd\u6570\u7edf\u8ba1\u9891\u7387\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7edf\u8ba1\u5b57\u51fa\u73b0\u7684\u9891\u7387\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4f7f\u7528\u5b57\u5178\u662f\u6700\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002\u4f7f\u7528Counter\u7c7b\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u6027\u80fd\u8f83\u597d\uff0c\u9002\u5408\u5904\u7406\u5927\u5b57\u7b26\u4e32\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u5e76\u7edf\u8ba1\u5b57\u7684\u9891\u7387\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff0c\u7136\u540e\u5229\u7528<code>collections.Counter<\/code>\u6765\u7edf\u8ba1\u5b57\u7684\u9891\u7387\u3002\u9996\u5148\uff0c\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9\uff0c\u63a5\u7740\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u5b57\u7b26\u5217\u8868\uff0c\u6700\u540e\u4f7f\u7528<code>Counter<\/code>\u7c7b\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nwith open(&#39;textfile.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    text = file.read()\n    frequency = Counter(text)\n    print(frequency)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u5f97\u5230\u6bcf\u4e2a\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u9891\u7387\u7684\u5b57\u5178\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5ffd\u7565\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7\u7edf\u8ba1\u5b57\u9891\u7387\uff1f<\/strong><br \/>\u8981\u5728\u7edf\u8ba1\u5b57\u9891\u7387\u65f6\u5ffd\u7565\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6\u6587\u672c\u540e\uff0c\u4f7f\u7528<code>str.isalpha()<\/code>\u65b9\u6cd5\u7b5b\u9009\u51fa\u5b57\u6bcd\u5b57\u7b26\u3002\u53ef\u4ee5\u5728\u7edf\u8ba1\u4e4b\u524d\u5148\u6e05\u6d17\u6587\u672c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import string\nfrom collections import Counter\n\nwith open(&#39;textfile.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    text = file.read()\n    cleaned_text = &#39;&#39;.join(filter(str.isalpha, text))  # \u53ea\u4fdd\u7559\u5b57\u6bcd\n    frequency = Counter(cleaned_text)\n    print(frequency)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u53ea\u7edf\u8ba1\u6709\u6548\u5b57\u6bcd\u7684\u9891\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u9891\u7387\u7ed3\u679c\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u4e3a\u4e86\u5c06\u5b57\u7684\u9891\u7387\u7ed3\u679c\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u3002\u901a\u8fc7\u7ed8\u5236\u67f1\u72b6\u56fe\u6216\u997c\u56fe\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u5c55\u793a\u5404\u4e2a\u5b57\u7684\u9891\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nfrom collections import Counter\n\ntext = &quot;\u793a\u4f8b\u6587\u672c\u5185\u5bb9&quot;\nfrequency = Counter(text)\n\nplt.bar(frequency.keys(), frequency.values())\nplt.xlabel(&#39;\u5b57\u7b26&#39;)\nplt.ylabel(&#39;\u9891\u7387&#39;)\nplt.title(&#39;\u5b57\u7b26\u9891\u7387\u7edf\u8ba1&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u80fd\u591f\u6e05\u6670\u5730\u770b\u5230\u5404\u4e2a\u5b57\u7b26\u7684\u9891\u7387\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7edf\u8ba1\u5b57\u51fa\u73b0\u7684\u9891\u7387\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3001Counter\u7c7b\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u3002\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528Counter [&hellip;]","protected":false},"author":3,"featured_media":1025886,"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\/1025878"}],"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=1025878"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025878\/revisions"}],"predecessor-version":[{"id":1025888,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025878\/revisions\/1025888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1025886"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1025878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1025878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1025878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}