{"id":1084644,"date":"2025-01-08T13:09:09","date_gmt":"2025-01-08T05:09:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084644.html"},"modified":"2025-01-08T13:09:11","modified_gmt":"2025-01-08T05:09:11","slug":"python%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e5%90%84%e8%af%8d%e6%b1%87%e5%87%ba%e7%8e%b0%e9%a2%91%e6%ac%a1-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1084644.html","title":{"rendered":"python\u5982\u4f55\u7edf\u8ba1\u5404\u8bcd\u6c47\u51fa\u73b0\u9891\u6b21"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194916\/58ed74f7-b6e4-4385-84c8-2225c3398eda.webp\" alt=\"python\u5982\u4f55\u7edf\u8ba1\u5404\u8bcd\u6c47\u51fa\u73b0\u9891\u6b21\" \/><\/p>\n<p><p> <strong>Python\u7edf\u8ba1\u8bcd\u6c47\u51fa\u73b0\u9891\u6b21\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528collections.Counter\u3001\u4f7f\u7528pandas\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528collections.Counter\u662f\u6700\u7b80\u4fbf\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u8bcd\u6c47\u7684\u51fa\u73b0\u9891\u6b21\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6\u6587\u672c\u6570\u636e<\/strong>\uff1a\u53ef\u4ee5\u4ece\u6587\u4ef6\u3001\u5b57\u7b26\u4e32\u7b49\u591a\u79cd\u6765\u6e90\u8bfb\u53d6\u6570\u636e\u3002<\/li>\n<li><strong>\u5206\u8bcd<\/strong>\uff1a\u5c06\u6587\u672c\u6570\u636e\u5206\u5272\u6210\u5355\u72ec\u7684\u8bcd\u6c47\u3002<\/li>\n<li><strong>\u7edf\u8ba1\u9891\u6b21<\/strong>\uff1a\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u6bcf\u4e2a\u8bcd\u6c47\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/li>\n<\/ol>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def count_word_frequency(text):<\/p>\n<p>    # 1. \u8bfb\u53d6\u6587\u672c\u6570\u636e<\/p>\n<p>    words = text.split()<\/p>\n<p>    # 2. \u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>    frequency = {}<\/p>\n<p>    # 3. \u7edf\u8ba1\u8bcd\u6c47\u51fa\u73b0\u9891\u6b21<\/p>\n<p>    for word in words:<\/p>\n<p>        if word in frequency:<\/p>\n<p>            frequency[word] += 1<\/p>\n<p>        else:<\/p>\n<p>            frequency[word] = 1<\/p>\n<p>    return frequency<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;this is a test text with some words this is a test&quot;<\/p>\n<p>frequency = count_word_frequency(text)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528collections.Counter<\/h3>\n<\/p>\n<p><p><code>collections.Counter<\/code> \u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u7c7b\uff0c\u5b83\u7528\u4e8e\u8ba1\u6570\u53ef\u54c8\u5e0c\u5bf9\u8c61\u3002\u4f7f\u7528<code>Counter<\/code>\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7edf\u8ba1\u8bcd\u6c47\u7684\u51fa\u73b0\u9891\u6b21\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_word_frequency(text):<\/p>\n<p>    # 1. \u8bfb\u53d6\u6587\u672c\u6570\u636e<\/p>\n<p>    words = text.split()<\/p>\n<p>    # 2. \u4f7f\u7528Counter\u7edf\u8ba1\u8bcd\u6c47\u51fa\u73b0\u9891\u6b21<\/p>\n<p>    frequency = Counter(words)<\/p>\n<p>    return frequency<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;this is a test text with some words this is a test&quot;<\/p>\n<p>frequency = count_word_frequency(text)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas<\/h3>\n<\/p>\n<p><p><code>pandas<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7edf\u8ba1\u8bcd\u6c47\u7684\u51fa\u73b0\u9891\u6b21\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6\u6587\u672c\u6570\u636e<\/strong>\u3002<\/li>\n<li><strong>\u521b\u5efaDataFrame<\/strong>\uff1a\u5c06\u8bcd\u6c47\u653e\u5165DataFrame\u4e2d\u3002<\/li>\n<li><strong>\u4f7f\u7528value_counts()\u65b9\u6cd5<\/strong>\uff1a\u7edf\u8ba1\u8bcd\u6c47\u7684\u51fa\u73b0\u9891\u6b21\u3002<\/li>\n<\/ol>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def count_word_frequency(text):<\/p>\n<p>    # 1. \u8bfb\u53d6\u6587\u672c\u6570\u636e<\/p>\n<p>    words = text.split()<\/p>\n<p>    # 2. \u521b\u5efaDataFrame<\/p>\n<p>    df = pd.DataFrame(words, columns=[&#39;word&#39;])<\/p>\n<p>    # 3. \u4f7f\u7528value_counts()\u65b9\u6cd5\u7edf\u8ba1\u8bcd\u6c47\u51fa\u73b0\u9891\u6b21<\/p>\n<p>    frequency = df[&#39;word&#39;].value_counts()<\/p>\n<p>    return frequency<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;this is a test text with some words this is a test&quot;<\/p>\n<p>frequency = count_word_frequency(text)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6570\u636e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u7edf\u8ba1\u8bcd\u6c47\u51fa\u73b0\u9891\u6b21\u4e4b\u524d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4e00\u4e9b\u9884\u5904\u7406\uff0c\u4f8b\u5982\u53bb\u9664\u6807\u70b9\u7b26\u53f7\u3001\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\u7b49\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u7edf\u8ba1\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<p>def preprocess_text(text):<\/p>\n<p>    # 1. \u53bb\u9664\u6807\u70b9\u7b26\u53f7<\/p>\n<p>    text = re.sub(r&#39;[^\\w\\s]&#39;, &#39;&#39;, text)<\/p>\n<p>    # 2. \u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd<\/p>\n<p>    text = text.lower()<\/p>\n<p>    return text<\/p>\n<p>def count_word_frequency(text):<\/p>\n<p>    # 1. \u9884\u5904\u7406\u6587\u672c\u6570\u636e<\/p>\n<p>    text = preprocess_text(text)<\/p>\n<p>    # 2. \u8bfb\u53d6\u6587\u672c\u6570\u636e<\/p>\n<p>    words = text.split()<\/p>\n<p>    # 3. \u4f7f\u7528Counter\u7edf\u8ba1\u8bcd\u6c47\u51fa\u73b0\u9891\u6b21<\/p>\n<p>    frequency = Counter(words)<\/p>\n<p>    return frequency<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;This is a test, text with some words! This is a test.&quot;<\/p>\n<p>frequency = count_word_frequency(text)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u6587\u672c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5904\u7406\u5927\u6587\u672c\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\uff08generator\uff09\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<p>def preprocess_text(text):<\/p>\n<p>    # 1. \u53bb\u9664\u6807\u70b9\u7b26\u53f7<\/p>\n<p>    text = re.sub(r&#39;[^\\w\\s]&#39;, &#39;&#39;, text)<\/p>\n<p>    # 2. \u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd<\/p>\n<p>    text = text.lower()<\/p>\n<p>    return text<\/p>\n<p>def count_word_frequency(file_path):<\/p>\n<p>    frequency = Counter()<\/p>\n<p>    # 1. \u9010\u884c\u8bfb\u53d6\u6587\u4ef6<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            # 2. \u9884\u5904\u7406\u6587\u672c\u6570\u636e<\/p>\n<p>            line = preprocess_text(line)<\/p>\n<p>            # 3. \u5206\u8bcd<\/p>\n<p>            words = line.split()<\/p>\n<p>            # 4. \u66f4\u65b0\u8bcd\u6c47\u9891\u6b21<\/p>\n<p>            frequency.update(words)<\/p>\n<p>    return frequency<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = &#39;large_text_file.txt&#39;<\/p>\n<p>frequency = count_word_frequency(file_path)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u7edf\u8ba1\u8bcd\u6c47\u7684\u51fa\u73b0\u9891\u6b21\u3002\u4e0d\u540c\u65b9\u6cd5\u6709\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u65b9\u6cd5\u53ef\u4ee5\u66f4\u597d\u5730\u5b8c\u6210\u4efb\u52a1\u3002\u5bf9\u4e8e\u5c0f\u6587\u672c\u6570\u636e\uff0c\u4f7f\u7528\u5b57\u5178\u6216<code>collections.Counter<\/code>\u90fd\u975e\u5e38\u65b9\u4fbf\uff1b\u800c\u5bf9\u4e8e\u5927\u6587\u672c\u6570\u636e\uff0c\u4f7f\u7528\u751f\u6210\u5668\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7edf\u8ba1\u6587\u672c\u4e2d\u5404\u4e2a\u8bcd\u6c47\u7684\u9891\u6b21\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.Counter<\/code>\u7c7b\u6765\u7edf\u8ba1\u8bcd\u6c47\u7684\u51fa\u73b0\u9891\u6b21\u3002\u9996\u5148\uff0c\u8bfb\u53d6\u6587\u672c\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u6587\u672c\u5206\u5272\u6210\u5355\u8bcd\uff0c\u63a5\u7740\u7528<code>Counter<\/code>\u6765\u8ba1\u7b97\u6bcf\u4e2a\u8bcd\u7684\u9891\u6b21\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;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\uff0c\u6587\u672c\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u590d\u7684\u8bcd\u6c47\u3002&quot;\nwords = text.split()  # \u5206\u5272\u6587\u672c\nword_counts = Counter(words)  # \u7edf\u8ba1\u8bcd\u6c47\u9891\u6b21\nprint(word_counts)\n<\/code><\/pre>\n<p><strong>\u5728\u7edf\u8ba1\u8bcd\u6c47\u9891\u6b21\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6807\u70b9\u7b26\u53f7\u548c\u5927\u5c0f\u5199\u95ee\u9898\uff1f<\/strong><br \/>\u5904\u7406\u6807\u70b9\u7b26\u53f7\u548c\u5927\u5c0f\u5199\u662f\u786e\u4fdd\u7edf\u8ba1\u7ed3\u679c\u51c6\u786e\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u5728\u5206\u5272\u6587\u672c\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5220\u9664\u6807\u70b9\u7b26\u53f7\uff0c\u5e76\u5c06\u6240\u6709\u5355\u8bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u540c\u4e00\u8bcd\u6c47\u56e0\u5927\u5c0f\u5199\u4e0d\u540c\u800c\u88ab\u7edf\u8ba1\u4e3a\u4e0d\u540c\u8bcd\u6c47\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\nfrom collections import Counter\n\ntext = &quot;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\uff0c\u6587\u672c\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u590d\u7684\u8bcd\u6c47\u3002&quot;\ntext = re.sub(r&#39;[^\\w\\s]&#39;, &#39;&#39;, text)  # \u53bb\u9664\u6807\u70b9\u7b26\u53f7\nwords = text.lower().split()  # \u8f6c\u4e3a\u5c0f\u5199\u5e76\u5206\u5272\nword_counts = Counter(words)\nprint(word_counts)\n<\/code><\/pre>\n<p><strong>\u6709\u4ec0\u4e48Python\u5e93\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u8bcd\u9891\u7edf\u8ba1\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>collections.Counter<\/code>\uff0c\u8fd8\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7b80\u5316\u8bcd\u9891\u7edf\u8ba1\u7684\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c<code>nltk<\/code>\uff08\u81ea\u7136\u8bed\u8a00\u5de5\u5177\u5305\uff09\u548c<code>pandas<\/code>\u5e93\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u529f\u80fd\u3002\u4f7f\u7528<code>nltk<\/code>\u65f6\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u5206\u8bcd\u3001\u53bb\u505c\u7528\u8bcd\u7b49\u5904\u7406\uff0c\u8fdb\u800c\u8fdb\u884c\u8bcd\u9891\u7edf\u8ba1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>nltk<\/code>\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import nltk\nfrom nltk.tokenize import word_tokenize\nfrom collections import Counter\n\nnltk.download(&#39;punkt&#39;)  # \u4e0b\u8f7dpunkt\u6a21\u5757\ntext = &quot;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\uff0c\u6587\u672c\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u590d\u7684\u8bcd\u6c47\u3002&quot;\nwords = word_tokenize(text)  # \u4f7f\u7528nltk\u8fdb\u884c\u5206\u8bcd\nword_counts = Counter(words)\nprint(word_counts)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u7edf\u8ba1\u8bcd\u6c47\u51fa\u73b0\u9891\u6b21\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528collections.Counter\u3001\u4f7f\u7528pandas [&hellip;]","protected":false},"author":3,"featured_media":1084649,"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\/1084644"}],"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=1084644"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084644\/revisions"}],"predecessor-version":[{"id":1084653,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084644\/revisions\/1084653"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1084649"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1084644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1084644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1084644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}