{"id":1007797,"date":"2024-12-27T10:56:58","date_gmt":"2024-12-27T02:56:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007797.html"},"modified":"2024-12-27T10:57:01","modified_gmt":"2024-12-27T02:57:01","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e8%af%8d%e6%b1%87%e7%bb%9f%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1007797.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u8bcd\u6c47\u7edf\u8ba1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083346\/c6f674d4-6588-4a77-b5e4-c3158c10c45b.webp\" alt=\"python\u5982\u4f55\u5bf9\u8bcd\u6c47\u7edf\u8ba1\" \/><\/p>\n<p><p> <strong>Python\u5bf9\u8bcd\u6c47\u7edf\u8ba1\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528collections\u6a21\u5757\u7684Counter\u7c7b\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u8bcd\u6c47\u63d0\u53d6\u3001\u4f7f\u7528pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u5176\u4e2dCounter\u7c7b\u975e\u5e38\u9002\u5408\u5feb\u901f\u7edf\u8ba1\u8bcd\u9891\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5219\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u683c\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>Counter\u7c7b\u7684\u4f7f\u7528<\/strong><br \/>Counter\u662fPython collections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u8ba1\u6570\u3002\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u7edf\u8ba1\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u8bcd\u6c47\u7edf\u8ba1\u3002\u4f7f\u7528Counter\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u7684\u4e00\u822c\u6b65\u9aa4\u5305\u62ec\u8bfb\u53d6\u6587\u672c\u6570\u636e\u3001\u5206\u8bcd\u3001\u4f7f\u7528Counter\u7edf\u8ba1\u8bcd\u9891\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6216\u534a\u7ed3\u6784\u5316\u7684\u6587\u672c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528COLLECTIONS\u6a21\u5757\u7684COUNTER\u7c7b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Counter\u7c7b\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u662fPython\u4e2d\u7684\u4e00\u79cd\u5feb\u6377\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u5229\u7528Python\u5185\u7f6e\u7684collections\u6a21\u5757\uff0c\u80fd\u591f\u5feb\u901f\u7edf\u8ba1\u51fa\u6bcf\u4e2a\u8bcd\u6c47\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u9002\u5408\u7528\u4e8e\u7b80\u5355\u7684\u6587\u672c\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1.1 COUNTER\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>Counter\u7c7b\u662fPython collections\u6a21\u5757\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8ba1\u6570\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u5143\u7d20\u4f5c\u4e3a\u952e\uff0c\u8ba1\u6570\u4f5c\u4e3a\u503c\u3002\u4f7f\u7528Counter\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u7684\u8fc7\u7a0b\u901a\u5e38\u5305\u62ec\u8bfb\u53d6\u6587\u672c\u6570\u636e\u3001\u5206\u8bcd\u3001\u7136\u540e\u4f7f\u7528Counter\u7c7b\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>text = &quot;Python is great and Python is easy to learn&quot;<\/p>\n<p>words = text.split()<\/p>\n<p>word_count = Counter(words)<\/p>\n<p>print(word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Counter<\/code>\u7edf\u8ba1\u4e86\u6bcf\u4e2a\u5355\u8bcd\u5728\u7ed9\u5b9a\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5904\u7406\u5927\u89c4\u6a21\u6587\u672c\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6587\u672c\u6570\u636e\uff0cCounter\u7c7b\u540c\u6837\u6709\u6548\u3002\u4e0d\u8fc7\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u6ce8\u610f\u5185\u5b58\u5360\u7528\u548c\u6027\u80fd\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u7684\u65b9\u5f0f\u9010\u884c\u5904\u7406\u6587\u672c\uff0c\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_words_in_file(filename):<\/p>\n<p>    with open(filename, &#39;r&#39;) as file:<\/p>\n<p>        word_count = Counter(file.read().split())<\/p>\n<p>    return word_count<\/p>\n<p>print(count_words_in_file(&#39;large_text_file.txt&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u8f83\u5927\u7684\u6587\u672c\u6587\u4ef6\uff0c\u907f\u514d\u5c06\u6574\u4e2a\u6587\u4ef6\u4e00\u6b21\u6027\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u8bcd\u6c47\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u80fd\u591f\u7075\u6d3b\u5730\u63d0\u53d6\u3001\u5339\u914d\u548c\u64cd\u4f5c\u6587\u672c\u6570\u636e\u3002\u5728\u8bcd\u6c47\u7edf\u8ba1\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u683c\u5f0f\uff0c\u63d0\u53d6\u51fa\u6211\u4eec\u9700\u8981\u7684\u8bcd\u6c47\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u57fa\u672c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7re\u6a21\u5757\uff0cPython\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u5355\u8bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<p>text = &quot;Python is great; Python is easy to learn! And Python is powerful.&quot;<\/p>\n<p>words = re.findall(r&#39;\\b\\w+\\b&#39;, text.lower())<\/p>\n<p>word_count = Counter(words)<\/p>\n<p>print(word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>\\b\\w+\\b<\/code>\u4f5c\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u6765\u5339\u914d\u5355\u8bcd\uff0c\u5e76\u5c06\u6240\u6709\u5355\u8bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u4ee5\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u590d\u6742\u6587\u672c\u7684\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5904\u7406\u590d\u6742\u6587\u672c\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u5904\u7406\u5e26\u6709\u6807\u70b9\u7b26\u53f7\u6216HTML\u6807\u7b7e\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<p>html_text = &quot;&lt;p&gt;Python is great.&lt;\/p&gt;&lt;div&gt;Python is easy to learn!&lt;\/div&gt;&quot;<\/p>\n<p>clean_text = re.sub(r&#39;&lt;[^&gt;]+&gt;&#39;, &#39;&#39;, html_text)  # \u53bb\u9664HTML\u6807\u7b7e<\/p>\n<p>words = re.findall(r&#39;\\b\\w+\\b&#39;, clean_text.lower())<\/p>\n<p>word_count = Counter(words)<\/p>\n<p>print(word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664HTML\u6807\u7b7e\uff0c\u7136\u540e\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PANDAS\u8fdb\u884c\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\u3002\u5b83\u7279\u522b\u9002\u5408\u7528\u4e8e\u7ed3\u6784\u5316\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u57fa\u672c\u7684PANDAS\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7Pandas\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6587\u672c\u6570\u636e\u8fdb\u884c\u5206\u6790\u548c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Pandas\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u7684\u7b80\u5355\u4f8b\u5b50\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>text = &quot;Python is great and Python is easy to learn&quot;<\/p>\n<p>words = pd.Series(text.split())<\/p>\n<p>word_count = words.value_counts()<\/p>\n<p>print(word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u7684<code>value_counts()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u7edf\u8ba1\u6bcf\u4e2a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u7b80\u5355\u7684\u8bcd\u6c47\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u5904\u7406\u5927\u578b\u6570\u636e\u96c6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0cPandas\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6CSV\u6587\u4ef6\u7684\u65b9\u5f0f\uff0c\u5c06\u6587\u672c\u6570\u636e\u52a0\u8f7d\u5230DataFrame\u4e2d\uff0c\u7136\u540e\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&#39;large_text_file.csv&#39;, usecols=[&#39;text_column&#39;])<\/p>\n<p>word_series = df[&#39;text_column&#39;].str.split().explode()<\/p>\n<p>word_count = word_series.value_counts()<\/p>\n<p>print(word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>str.split().explode()<\/code>\u65b9\u6cd5\u5c06\u6587\u672c\u5217\u4e2d\u7684\u8bcd\u8bed\u5206\u5272\u5f00\u6765\uff0c\u7136\u540e\u4f7f\u7528<code>value_counts()<\/code>\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408NLTK\u8fdb\u884c\u81ea\u7136\u8bed\u8a00\u5904\u7406<\/h3>\n<\/p>\n<p><p>NLTK\uff08Natural Language Toolkit\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u7528\u4e8e\u6587\u672c\u5904\u7406\u548c\u5206\u6790\u3002\u5b83\u53ef\u4ee5\u4e0e\u5176\u4ed6\u65b9\u6cd5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u548c\u7cbe\u786e\u7684\u8bcd\u6c47\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528NLTK\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1<\/h4>\n<\/p>\n<p><p>NLTK\u63d0\u4f9b\u4e86\u5206\u8bcd\u3001\u8bcd\u6027\u6807\u6ce8\u3001\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\u7b49\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u6587\u672c\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528NLTK\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u7684\u7b80\u5355\u4f8b\u5b50\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>from nltk.tokenize import word_tokenize<\/p>\n<p>from collections import Counter<\/p>\n<p>nltk.download(&#39;punkt&#39;)<\/p>\n<p>text = &quot;Python is great and Python is easy to learn&quot;<\/p>\n<p>words = word_tokenize(text.lower())<\/p>\n<p>word_count = Counter(words)<\/p>\n<p>print(word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NLTK\u7684<code>word_tokenize()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bc6\u522b\u5355\u8bcd\u5e76\u5ffd\u7565\u6807\u70b9\u7b26\u53f7\uff0c\u4f7f\u5f97\u8bcd\u6c47\u7edf\u8ba1\u66f4\u4e3a\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u9ad8\u7ea7\u6587\u672c\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NLTK\u53ef\u4ee5\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u6587\u672c\u5206\u6790\uff0c\u4f8b\u5982\u8fc7\u6ee4\u6389\u5e38\u89c1\u7684\u505c\u7528\u8bcd\uff08\u5982\u201cis\u201d\u3001\u201cand\u201d\u7b49\uff09\uff0c\u4ee5\u83b7\u5f97\u66f4\u6709\u610f\u4e49\u7684\u8bcd\u6c47\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from nltk.corpus import stopwords<\/p>\n<p>nltk.download(&#39;stopwords&#39;)<\/p>\n<p>stop_words = set(stopwords.words(&#39;english&#39;))<\/p>\n<p>filtered_words = [word for word in words if word not in stop_words]<\/p>\n<p>filtered_word_count = Counter(filtered_words)<\/p>\n<p>print(filtered_word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u53bb\u9664\u5e38\u89c1\u7684\u505c\u7528\u8bcd\uff0c\u53ef\u4ee5\u5f97\u5230\u66f4\u7cbe\u786e\u7684\u8bcd\u6c47\u7edf\u8ba1\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u5b9e\u7528\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u53ef\u4ee5\u91c7\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u7279\u70b9\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u65b9\u6cd5\u6bd4\u8f83<\/h4>\n<\/p>\n<ul>\n<li><strong>Counter\u7c7b<\/strong>\uff1a\u7b80\u5355\u3001\u5feb\u901f\uff0c\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u6216\u7ed3\u6784\u5316\u7684\u6587\u672c\u6570\u636e\u3002<\/li>\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u7075\u6d3b\u3001\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u683c\u5f0f\u7684\u6587\u672c\u3002<\/li>\n<li><strong>Pandas<\/strong>\uff1a\u9ad8\u6548\u3001\u5168\u9762\uff0c\u9002\u5408\u7528\u4e8e\u5927\u578b\u7ed3\u6784\u5316\u6570\u636e\u96c6\u3002<\/li>\n<li><strong>NLTK<\/strong>\uff1a\u529f\u80fd\u4e30\u5bcc\uff0c\u9002\u5408\u81ea\u7136\u8bed\u8a00\u5904\u7406\u548c\u9ad8\u7ea7\u6587\u672c\u5206\u6790\u3002<\/li>\n<\/ul>\n<p><h4>5.2 \u5b9e\u7528\u5efa\u8bae<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u65f6\uff0c\u5efa\u8bae\u9996\u5148\u8bc4\u4f30\u6570\u636e\u7684\u7279\u70b9\u548c\u5206\u6790\u9700\u6c42\uff0c\u7136\u540e\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u683c\u5f0f\u6216\u8fdb\u884c\u9ad8\u7ea7\u6587\u672c\u5206\u6790\uff0c\u5efa\u8bae\u7ed3\u5408\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548cNLTK\u3002\u5982\u679c\u5904\u7406\u7684\u662f\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0cPandas\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u6b64\u5916\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u8981\u6ce8\u610f\u5185\u5b58\u4f7f\u7528\u548c\u6027\u80fd\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u53ef\u4ee5\u5982\u4f55\u5e2e\u52a9\u6211\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u548c\u65b9\u6cd5\u6765\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>collections.Counter<\/code>\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u6587\u672c\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u7684\u51fa\u73b0\u9891\u7387\u3002\u6b64\u5916\uff0c<code>nltk<\/code>\u548c<code>spaCy<\/code>\u7b49\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u6587\u672c\u6570\u636e\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u6587\u672c\u4e2d\u7684\u8bcd\u6c47\u7edf\u8ba1\u4fe1\u606f\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u54ea\u4e9bPython\u5e93\u6765\u5b9e\u73b0\u8bcd\u6c47\u7edf\u8ba1\uff1f<\/strong><br \/>\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u65f6\uff0c\u5e38\u7528\u7684Python\u5e93\u5305\u62ec<code>collections<\/code>\u3001<code>nltk<\/code>\u3001<code>pandas<\/code>\u548c<code>re<\/code>\u3002<code>collections.Counter<\/code>\u53ef\u4ee5\u5feb\u901f\u7edf\u8ba1\u5355\u8bcd\u9891\u7387\uff0c<code>nltk<\/code>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u529f\u80fd\uff0c<code>pandas<\/code>\u5219\u9002\u5408\u5904\u7406\u548c\u5206\u6790\u5927\u578b\u6570\u636e\u96c6\uff0c\u800c<code>re<\/code>\u7528\u4e8e\u6587\u672c\u7684\u9884\u5904\u7406\u548c\u6e05\u6d17\u3002\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u548c\u5927\u5c0f\u5199\u4ee5\u83b7\u5f97\u51c6\u786e\u7684\u8bcd\u6c47\u7edf\u8ba1\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u8bcd\u6c47\u7edf\u8ba1\u4e4b\u524d\uff0c\u6e05\u6d17\u6587\u672c\u662f\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08<code>re<\/code>\u5e93\uff09\u53bb\u9664\u6807\u70b9\u7b26\u53f7\uff0c\u5e76\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u5c0f\u5199\uff0c\u4ee5\u786e\u4fdd\u540c\u4e00\u4e2a\u5355\u8bcd\u7684\u4e0d\u540c\u5f62\u5f0f\u4e0d\u4f1a\u88ab\u91cd\u590d\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u5c06\u201cPython\u201d\u548c\u201cpython\u201d\u89c6\u4e3a\u540c\u4e00\u4e2a\u8bcd\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u7edf\u8ba1\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bf9\u8bcd\u6c47\u7edf\u8ba1\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528collections\u6a21\u5757\u7684Counter\u7c7b\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1007805,"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\/1007797"}],"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=1007797"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007797\/revisions"}],"predecessor-version":[{"id":1007808,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007797\/revisions\/1007808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007805"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}