{"id":1157482,"date":"2025-01-13T18:29:11","date_gmt":"2025-01-13T10:29:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1157482.html"},"modified":"2025-01-13T18:29:13","modified_gmt":"2025-01-13T10:29:13","slug":"python%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e5%87%ba%e7%8e%b0%e6%ac%a1%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1157482.html","title":{"rendered":"python\u5982\u4f55\u7edf\u8ba1\u51fa\u73b0\u6b21\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25195653\/c16dc663-57d8-4ba5-936a-a15bf42c8d8f.webp\" alt=\"python\u5982\u4f55\u7edf\u8ba1\u51fa\u73b0\u6b21\u6570\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u7edf\u8ba1\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002<strong>\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b\u3001\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528\u5217\u8868\u7684count\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b<\/strong> \u662f\u4e00\u79cd\u9ad8\u6548\u4e14\u7b80\u6d01\u7684\u65b9\u6cd5\u3002Counter\u662fcollections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u53ef\u54c8\u5e0c\u5bf9\u8c61\u3002\u5b83\u662f\u4e00\u4e2a\u65e0\u5e8f\u5bb9\u5668\u7c7b\u578b\uff0c\u4ee5\u5b57\u5178\u7684\u952e\u503c\u5bf9\u5f62\u5f0f\u5b58\u50a8\uff0c\u5176\u4e2d\u5143\u7d20\u662f\u952e\uff0c\u5b83\u4eec\u7684\u8ba1\u6570\u662f\u503c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b<\/h3>\n<\/p>\n<p><p>Counter\u7c7b\u662fPython\u6807\u51c6\u5e93collections\u6a21\u5757\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u7edf\u8ba1\u53ef\u54c8\u5e0c\u5bf9\u8c61\u7684\u9891\u6b21\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b57\u7b26\u4e32\u7b49\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u4f7f\u7528Counter\u7c7b\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b\u3002<\/li>\n<li>\u521b\u5efaCounter\u5bf9\u8c61\uff0c\u5e76\u5c06\u9700\u8981\u7edf\u8ba1\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f20\u9012\u7ed9\u5b83\u3002<\/li>\n<li>\u4f7f\u7528Counter\u5bf9\u8c61\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u6765\u83b7\u53d6\u7edf\u8ba1\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Counter\u7c7b\u6765\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684\u5217\u8868<\/strong><\/h2>\n<p>numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]<\/p>\n<h2><strong>\u521b\u5efaCounter\u5bf9\u8c61<\/strong><\/h2>\n<p>counter = Counter(numbers)<\/p>\n<h2><strong>\u6253\u5370\u7edf\u8ba1\u7ed3\u679c<\/strong><\/h2>\n<p>print(counter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Counter({4: 4, 3: 3, 2: 2, 1: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u6570\u5b574\u51fa\u73b0\u4e864\u6b21\uff0c\u6570\u5b573\u51fa\u73b0\u4e863\u6b21\uff0c\u6570\u5b572\u51fa\u73b0\u4e862\u6b21\uff0c\u6570\u5b571\u51fa\u73b0\u4e861\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u51fa\u73b0\u6b21\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528Counter\u7c7b\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u50a8\u548c\u67e5\u627e\u5143\u7d20\u7684\u9891\u6b21\u3002\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff0c\u7528\u4e8e\u5b58\u50a8\u5143\u7d20\u548c\u5b83\u4eec\u7684\u8ba1\u6570\u3002<\/li>\n<li>\u904d\u5386\u9700\u8981\u7edf\u8ba1\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u5b83\u662f\u5426\u5df2\u7ecf\u5728\u5b57\u5178\u4e2d\u3002\u5982\u679c\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u5c06\u5bf9\u5e94\u7684\u8ba1\u6570\u52a01\uff1b\u5982\u679c\u4e0d\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u5c06\u8ba1\u6570\u8bbe\u7f6e\u4e3a1\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684\u5217\u8868<\/p>\n<p>numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/strong><\/h2>\n<p>count_dict = {}<\/p>\n<h2><strong>\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570<\/strong><\/h2>\n<p>for number in numbers:<\/p>\n<p>    # \u68c0\u67e5\u6574\u6570\u662f\u5426\u5df2\u7ecf\u5728\u5b57\u5178\u4e2d<\/p>\n<p>    if number in count_dict:<\/p>\n<p>        # \u5c06\u5bf9\u5e94\u7684\u8ba1\u6570\u52a01<\/p>\n<p>        count_dict[number] += 1<\/p>\n<p>    else:<\/p>\n<p>        # \u5c06\u6574\u6570\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u5c06\u8ba1\u6570\u8bbe\u7f6e\u4e3a1<\/p>\n<p>        count_dict[number] = 1<\/p>\n<h2><strong>\u6253\u5370\u7edf\u8ba1\u7ed3\u679c<\/strong><\/h2>\n<p>print(count_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>{1: 1, 2: 2, 3: 3, 4: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u6570\u5b574\u51fa\u73b0\u4e864\u6b21\uff0c\u6570\u5b573\u51fa\u73b0\u4e863\u6b21\uff0c\u6570\u5b572\u51fa\u73b0\u4e862\u6b21\uff0c\u6570\u5b571\u51fa\u73b0\u4e861\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u5217\u8868\u7684count\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u573a\u666f\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684count\u65b9\u6cd5\u6765\u7edf\u8ba1\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002count\u65b9\u6cd5\u662f\u5217\u8868\u5bf9\u8c61\u7684\u4e00\u4e2a\u5185\u7f6e\u65b9\u6cd5\uff0c\u7528\u4e8e\u7edf\u8ba1\u6307\u5b9a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f7f\u7528count\u65b9\u6cd5\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u8c03\u7528\u5217\u8868\u5bf9\u8c61\u7684count\u65b9\u6cd5\uff0c\u5e76\u5c06\u9700\u8981\u7edf\u8ba1\u7684\u5143\u7d20\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002<\/li>\n<li>count\u65b9\u6cd5\u8fd4\u56de\u8be5\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528count\u65b9\u6cd5\u6765\u7edf\u8ba1\u67d0\u4e2a\u6574\u6570\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684\u5217\u8868<\/p>\n<p>numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]<\/p>\n<h2><strong>\u7edf\u8ba1\u6570\u5b574\u51fa\u73b0\u7684\u6b21\u6570<\/strong><\/h2>\n<p>count_4 = numbers.count(4)<\/p>\n<h2><strong>\u6253\u5370\u7edf\u8ba1\u7ed3\u679c<\/strong><\/h2>\n<p>print(count_4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u6570\u5b574\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u4e864\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\u6765\u7edf\u8ba1\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u6a21\u5f0f\u5339\u914d\u3001\u67e5\u627e\u548c\u66ff\u6362\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5355\u8bcd\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548cCounter\u7c7b\u6765\u7edf\u8ba1\u6bcf\u4e2a\u5355\u8bcd\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5355\u8bcd\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>text = &quot;This is a test. This test is only a test.&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>words = re.findall(r&#39;\\b\\w+\\b&#39;, text.lower())<\/p>\n<h2><strong>\u521b\u5efaCounter\u5bf9\u8c61<\/strong><\/h2>\n<p>counter = Counter(words)<\/p>\n<h2><strong>\u6253\u5370\u7edf\u8ba1\u7ed3\u679c<\/strong><\/h2>\n<p>print(counter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Counter({&#39;test&#39;: 3, &#39;this&#39;: 2, &#39;is&#39;: 2, &#39;a&#39;: 2, &#39;only&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u5355\u8bcd&quot;test&quot;\u51fa\u73b0\u4e863\u6b21\uff0c\u5355\u8bcd&quot;this&quot;\u3001&quot;is&quot;\u548c&quot;a&quot;\u5404\u51fa\u73b0\u4e862\u6b21\uff0c\u5355\u8bcd&quot;only&quot;\u51fa\u73b0\u4e861\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Pandas\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u6b21\u6570<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pandas\u7684value_counts\u65b9\u6cd5\u6765\u7edf\u8ba1Series\u5bf9\u8c61\u4e2d\u6bcf\u4e2a\u503c\u7684\u51fa\u73b0\u6b21\u6570\u3002value_counts\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u503c\u548c\u5b83\u4eec\u51fa\u73b0\u6b21\u6570\u7684Series\u5bf9\u8c61\uff0c\u6309\u51fa\u73b0\u6b21\u6570\u964d\u5e8f\u6392\u5217\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684Series\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528value_counts\u65b9\u6cd5\u6765\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684Series\u5bf9\u8c61<\/strong><\/h2>\n<p>numbers = pd.Series([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])<\/p>\n<h2><strong>\u4f7f\u7528value_counts\u65b9\u6cd5\u7edf\u8ba1\u51fa\u73b0\u6b21\u6570<\/strong><\/h2>\n<p>count_series = numbers.value_counts()<\/p>\n<h2><strong>\u6253\u5370\u7edf\u8ba1\u7ed3\u679c<\/strong><\/h2>\n<p>print(count_series)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>4    4<\/p>\n<p>3    3<\/p>\n<p>2    2<\/p>\n<p>1    1<\/p>\n<p>dtype: int64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u6570\u5b574\u51fa\u73b0\u4e864\u6b21\uff0c\u6570\u5b573\u51fa\u73b0\u4e863\u6b21\uff0c\u6570\u5b572\u51fa\u73b0\u4e862\u6b21\uff0c\u6570\u5b571\u51fa\u73b0\u4e861\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Numpy\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u6b21\u6570<\/h3>\n<\/p>\n<p><p>Numpy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\u3002\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Numpy\u7684unique\u51fd\u6570\u6765\u7edf\u8ba1\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u503c\u7684\u51fa\u73b0\u6b21\u6570\u3002unique\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u552f\u4e00\u503c\u7684\u6570\u7ec4\u548c\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u552f\u4e00\u503c\u51fa\u73b0\u6b21\u6570\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684Numpy\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528unique\u51fd\u6570\u6765\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684Numpy\u6570\u7ec4<\/strong><\/h2>\n<p>numbers = np.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])<\/p>\n<h2><strong>\u4f7f\u7528unique\u51fd\u6570\u7edf\u8ba1\u51fa\u73b0\u6b21\u6570<\/strong><\/h2>\n<p>unique_values, counts = np.unique(numbers, return_counts=True)<\/p>\n<h2><strong>\u6253\u5370\u7edf\u8ba1\u7ed3\u679c<\/strong><\/h2>\n<p>print(dict(zip(unique_values, counts)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>{1: 1, 2: 2, 3: 3, 4: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u6570\u5b574\u51fa\u73b0\u4e864\u6b21\uff0c\u6570\u5b573\u51fa\u73b0\u4e863\u6b21\uff0c\u6570\u5b572\u51fa\u73b0\u4e862\u6b21\uff0c\u6570\u5b571\u51fa\u73b0\u4e861\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528SQL\u7edf\u8ba1\u6570\u636e\u5e93\u4e2d\u6570\u636e\u51fa\u73b0\u6b21\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5e93\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8be2\u6765\u7edf\u8ba1\u8868\u4e2d\u6bcf\u4e2a\u503c\u7684\u51fa\u73b0\u6b21\u6570\u3002SQL\u63d0\u4f9b\u4e86GROUP BY\u548cCOUNT\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u548c\u8ba1\u6570\u3002\u4f7f\u7528SQL\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u6b21\u6570\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002<\/li>\n<li>\u6267\u884c\u5305\u542bGROUP BY\u548cCOUNT\u51fd\u6570\u7684SQL\u67e5\u8be2\u3002<\/li>\n<li>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u5904\u7406\u7edf\u8ba1\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684\u6570\u636e\u5e93\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8be2\u6765\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u5305\u542bGROUP BY\u548cCOUNT\u51fd\u6570\u7684SQL\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;<\/p>\n<p>    SELECT number, COUNT(*) <\/p>\n<p>    FROM numbers <\/p>\n<p>    GROUP BY number<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>results = cursor.fetchall()<\/p>\n<h2><strong>\u6253\u5370\u7edf\u8ba1\u7ed3\u679c<\/strong><\/h2>\n<p>print(dict(results))<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>{1: 1, 2: 2, 3: 3, 4: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u6570\u5b574\u51fa\u73b0\u4e864\u6b21\uff0c\u6570\u5b573\u51fa\u73b0\u4e863\u6b21\uff0c\u6570\u5b572\u51fa\u73b0\u4e862\u6b21\uff0c\u6570\u5b571\u51fa\u73b0\u4e861\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528MapReduce\u7edf\u8ba1\u5206\u5e03\u5f0f\u6570\u636e\u51fa\u73b0\u6b21\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u5206\u5e03\u5f0f\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528MapReduce\u7f16\u7a0b\u6a21\u578b\u6765\u7edf\u8ba1\u6bcf\u4e2a\u503c\u7684\u51fa\u73b0\u6b21\u6570\u3002MapReduce\u662f\u4e00\u79cd\u5206\u5e03\u5f0f\u8ba1\u7b97\u6a21\u578b\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002MapReduce\u6a21\u578b\u5305\u542b\u4e24\u4e2a\u4e3b\u8981\u9636\u6bb5\uff1aMap\u9636\u6bb5\u548cReduce\u9636\u6bb5\u3002\u5728Map\u9636\u6bb5\uff0c\u6211\u4eec\u5c06\u6570\u636e\u5206\u5272\u6210\u5c0f\u5757\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5c0f\u5757\u8fdb\u884c\u5e76\u884c\u5904\u7406\u3002\u5728Reduce\u9636\u6bb5\uff0c\u6211\u4eec\u5c06\u4e2d\u95f4\u7ed3\u679c\u5408\u5e76\uff0c\u5f97\u5230\u6700\u7ec8\u7684\u7edf\u8ba1\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684\u5206\u5e03\u5f0f\u6570\u636e\u96c6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528MapReduce\u6a21\u578b\u6765\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mrjob.job import MRJob<\/p>\n<p>class MRCount(MRJob):<\/p>\n<p>    def mapper(self, _, line):<\/p>\n<p>        # \u5206\u5272\u884c\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570<\/p>\n<p>        for number in line.split():<\/p>\n<p>            # \u5c06\u6574\u6570\u8f6c\u6362\u4e3aint\u7c7b\u578b\uff0c\u5e76\u751f\u6210\u952e\u503c\u5bf9<\/p>\n<p>            yield int(number), 1<\/p>\n<p>    def reducer(self, number, counts):<\/p>\n<p>        # \u8ba1\u7b97\u6bcf\u4e2a\u6574\u6570\u7684\u603b\u8ba1\u6570<\/p>\n<p>        yield number, sum(counts)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    MRCount.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2aMapReduce\u4f5c\u4e1a\uff0c\u7528\u4e8e\u7edf\u8ba1\u5206\u5e03\u5f0f\u6570\u636e\u96c6\u4e2d\u6bcf\u4e2a\u6574\u6570\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5728mapper\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5c06\u884c\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570\u5206\u5272\u5f00\uff0c\u5e76\u751f\u6210\u952e\u503c\u5bf9\u3002\u5728reducer\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u8ba1\u7b97\u6bcf\u4e2a\u6574\u6570\u7684\u603b\u8ba1\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u56fe\u8868\u53ef\u89c6\u5316\u7edf\u8ba1\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u5728\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u6b21\u6570\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56fe\u8868\u6765\u53ef\u89c6\u5316\u7edf\u8ba1\u7ed3\u679c\u3002\u53ef\u89c6\u5316\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\u5206\u5e03\u548c\u6a21\u5f0f\u3002Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u53ef\u89c6\u5316\u5e93\uff0c\u5982Matplotlib\u3001Seaborn\u548cPlotly\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6574\u6570\u7684\u7edf\u8ba1\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u521b\u5efa\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5b9a\u4e49\u7edf\u8ba1\u7ed3\u679c<\/strong><\/h2>\n<p>count_dict = {1: 1, 2: 2, 3: 3, 4: 4}<\/p>\n<h2><strong>\u521b\u5efa\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>plt.bar(count_dict.keys(), count_dict.values())<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Number Frequency&#39;)<\/p>\n<p>plt.xlabel(&#39;Number&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u67f1\u72b6\u56fe\uff0c\u663e\u793a\u6bcf\u4e2a\u6574\u6570\u7684\u51fa\u73b0\u6b21\u6570\u3002\u67f1\u72b6\u56fe\u7684\u6a2a\u8f74\u8868\u793a\u6574\u6570\uff0c\u7eb5\u8f74\u8868\u793a\u9891\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u7edf\u8ba1\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002<strong>\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b\u3001\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528\u5217\u8868\u7684count\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\u3001\u4f7f\u7528Pandas\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u6b21\u6570\u3001\u4f7f\u7528Numpy\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u6b21\u6570\u3001\u4f7f\u7528SQL\u7edf\u8ba1\u6570\u636e\u5e93\u4e2d\u6570\u636e\u51fa\u73b0\u6b21\u6570\u3001\u4f7f\u7528MapReduce\u7edf\u8ba1\u5206\u5e03\u5f0f\u6570\u636e\u51fa\u73b0\u6b21\u6570\u3001\u4f7f\u7528\u56fe\u8868\u53ef\u89c6\u5316\u7edf\u8ba1\u7ed3\u679c<\/strong>\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u548c\u6570\u636e\u7c7b\u578b\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5206\u5e03\u5f0f\u8ba1\u7b97\u6a21\u578b\uff0c\u5982MapReduce\u3002\u5728\u5904\u7406\u6570\u636e\u5e93\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8be2\u3002\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u7edf\u8ba1\u6570\u636e\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u901a\u8fc7\u53ef\u89c6\u5316\u624b\u6bb5\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u7edf\u8ba1\u7ed3\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7edf\u8ba1\u5217\u8868\u4e2d\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.Counter<\/code>\u7c7b\u6765\u7b80\u4fbf\u5730\u7edf\u8ba1\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nmy_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;]\ncount = Counter(my_list)\nprint(count)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u6bcf\u4e2a\u5143\u7d20\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u7ed3\u679c\u4e3a<code>Counter({&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;orange&#39;: 1})<\/code>\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u901a\u8fc7\u904d\u5386\u5217\u8868\u5e76\u4f7f\u7528\u5b57\u5178\u8bb0\u5f55\u6bcf\u4e2a\u5143\u7d20\u7684\u8ba1\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;]\ncount_dict = {}\n\nfor item in my_list:\n    count_dict[item] = count_dict.get(item, 0) + 1\n\nprint(count_dict)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a<code>{&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;orange&#39;: 1}<\/code>\uff0c\u8fd9\u8868\u660e\u5b57\u5178\u4e5f\u80fd\u6709\u6548\u5730\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u529e\u6cd5\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u51fa\u73b0\u9891\u7387\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u51fa\u73b0\u9891\u7387\u7684\u65b9\u6cd5\u4e0e\u5217\u8868\u7c7b\u4f3c\u3002\u53ef\u4ee5\u4f7f\u7528<code>Counter<\/code>\u7c7b\u6216\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>Counter<\/code>\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nmy_string = &quot;hello world&quot;\nchar_count = Counter(my_string)\nprint(char_count)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5305\u62ec\u7a7a\u683c\uff0c\u7ed3\u679c\u7c7b\u4f3c\u4e8e<code>Counter({&#39;l&#39;: 3, &#39;o&#39;: 2, &#39;h&#39;: 1, &#39;e&#39;: 1, &#39; &#39;: 1, &#39;w&#39;: 1, &#39;r&#39;: 1, &#39;d&#39;: 1})<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u7edf\u8ba1\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1157484,"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\/1157482"}],"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=1157482"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157482\/revisions"}],"predecessor-version":[{"id":1157487,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157482\/revisions\/1157487"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1157484"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1157482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1157482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1157482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}