{"id":1127071,"date":"2025-01-08T20:07:02","date_gmt":"2025-01-08T12:07:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127071.html"},"modified":"2025-01-08T20:07:02","modified_gmt":"2025-01-08T12:07:02","slug":"%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9b%b8%e5%90%8c%e5%85%83%e7%b4%a0%e7%9a%84%e4%b8%aa%e6%95%b0python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127071.html","title":{"rendered":"\u5982\u4f55\u7edf\u8ba1\u5217\u8868\u4e2d\u76f8\u540c\u5143\u7d20\u7684\u4e2a\u6570python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094205\/e09e3be9-b32a-4a66-8275-ff87eadeac9c.webp\" alt=\"\u5982\u4f55\u7edf\u8ba1\u5217\u8868\u4e2d\u76f8\u540c\u5143\u7d20\u7684\u4e2a\u6570python\" \/><\/p>\n<p><p> <strong>\u8981\u7edf\u8ba1Python\u5217\u8868\u4e2d\u76f8\u540c\u5143\u7d20\u7684\u4e2a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>collections.Counter<\/code> \u6a21\u5757\u3001<code>for<\/code> \u5faa\u73af\u3001\u4ee5\u53ca <code>pandas<\/code> \u5e93\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c<strong><code>collections.Counter<\/code> \u6a21\u5757<\/strong> \u662f\u6700\u7b80\u5355\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5404\u79cd\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528 <code>collections.Counter<\/code> \u6a21\u5757<\/h2>\n<\/p>\n<p><h3>\u4ec0\u4e48\u662f <code>collections.Counter<\/code>\uff1f<\/h3>\n<\/p>\n<p><p><code>collections.Counter<\/code> \u662f Python \u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u8ba1\u6570\u3002\u5b83\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u5217\u8868\u3001\u5b57\u7b26\u4e32\u7b49\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8ba1\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5982\u4f55\u4f7f\u7528 <code>collections.Counter<\/code>\uff1f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;banana&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 Counter \u7edf\u8ba1\u5143\u7d20\u4e2a\u6570<\/strong><\/h2>\n<p>element_count = Counter(my_list)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(element_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u4f7f\u7528\u7b80\u5355\u3001\u4ee3\u7801\u7b80\u6d01\u3001\u6548\u7387\u9ad8\u3002<\/li>\n<li><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u591a\u6570\u5e38\u89c1\u7684\u7edf\u8ba1\u9700\u6c42\uff0c\u4f46\u5bf9\u4e8e\u975e\u5e38\u5e9e\u5927\u7684\u6570\u636e\u96c6\uff0c\u53ef\u80fd\u9700\u8981\u6ce8\u610f\u5185\u5b58\u6d88\u8017\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u4e0e\u5b57\u5178<\/h2>\n<\/p>\n<p><h3>\u5982\u4f55\u624b\u52a8\u5b9e\u73b0\u7edf\u8ba1\u529f\u80fd\uff1f<\/h3>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u548c\u5b57\u5178\u6765\u624b\u52a8\u7edf\u8ba1\u5143\u7d20\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;banana&#39;]<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5b57\u5178<\/strong><\/h2>\n<p>element_count = {}<\/p>\n<h2><strong>\u4f7f\u7528 for \u5faa\u73af\u7edf\u8ba1\u5143\u7d20\u4e2a\u6570<\/strong><\/h2>\n<p>for item in my_list:<\/p>\n<p>    if item in element_count:<\/p>\n<p>        element_count[item] += 1<\/p>\n<p>    else:<\/p>\n<p>        element_count[item] = 1<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(element_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u4e0d\u9700\u8981\u5bfc\u5165\u989d\u5916\u7684\u6a21\u5757\uff0c\u9002\u5408\u7406\u89e3\u57fa\u7840\u7b97\u6cd5\u3002<\/li>\n<li><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u4ee3\u7801\u76f8\u5bf9\u8f83\u957f\uff0c\u9002\u5408\u5b66\u4e60\u548c\u7406\u89e3\u57fa\u672c\u539f\u7406\uff0c\u4f46\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u4e0d\u5982 <code>collections.Counter<\/code> \u9ad8\u6548\u3002<\/li>\n<\/ul>\n<p><h2>\u4e09\u3001\u4f7f\u7528 <code>pandas<\/code> \u5e93<\/h2>\n<\/p>\n<p><h3>\u4ec0\u4e48\u662f <code>pandas<\/code>\uff1f<\/h3>\n<\/p>\n<p><p><code>pandas<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c\u6570\u636e\u5206\u6790\u9886\u57df\u3002\u5b83\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5982\u4f55\u4f7f\u7528 <code>pandas<\/code> \u7edf\u8ba1\u5143\u7d20\u4e2a\u6570\uff1f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;banana&#39;]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a pandas Series<\/strong><\/h2>\n<p>series = pd.Series(my_list)<\/p>\n<h2><strong>\u4f7f\u7528 value_counts \u7edf\u8ba1\u5143\u7d20\u4e2a\u6570<\/strong><\/h2>\n<p>element_count = series.value_counts()<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(element_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/li>\n<li><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u9700\u8981\u5b89\u88c5 <code>pandas<\/code> \u5e93\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u53ef\u80fd\u5bf9\u65b0\u624b\u6709\u4e00\u5b9a\u7684\u5b66\u4e60\u66f2\u7ebf\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u4f7f\u7528 <code>numpy<\/code> \u5e93<\/h2>\n<\/p>\n<p><h3>\u4ec0\u4e48\u662f <code>numpy<\/code>\uff1f<\/h3>\n<\/p>\n<p><p><code>numpy<\/code> \u662f\u4e00\u4e2a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u5b83\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9886\u57df\u975e\u5e38\u5e38\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u5982\u4f55\u4f7f\u7528 <code>numpy<\/code> \u7edf\u8ba1\u5143\u7d20\u4e2a\u6570\uff1f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;banana&#39;]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a numpy \u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array(my_list)<\/p>\n<h2><strong>\u4f7f\u7528 unique \u51fd\u6570\u7edf\u8ba1\u5143\u7d20\u4e2a\u6570<\/strong><\/h2>\n<p>unique, counts = np.unique(array, return_counts=True)<\/p>\n<p>element_count = dict(zip(unique, counts))<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(element_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/li>\n<li><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u9700\u8981\u5b89\u88c5 <code>numpy<\/code> \u5e93\uff0c\u9002\u5408\u5904\u7406\u6570\u503c\u6570\u636e\uff0c\u5bf9\u65b0\u624b\u6709\u4e00\u5b9a\u7684\u5b66\u4e60\u66f2\u7ebf\u3002<\/li>\n<\/ul>\n<p><h2>\u4e94\u3001\u4f7f\u7528 <code>itertools<\/code> \u5e93<\/h2>\n<\/p>\n<p><h3>\u4ec0\u4e48\u662f <code>itertools<\/code>\uff1f<\/h3>\n<\/p>\n<p><p><code>itertools<\/code> \u662f\u4e00\u4e2a\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u51fd\u6570\uff0c\u7528\u4e8e\u64cd\u4f5c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u5982\u4f55\u4f7f\u7528 <code>itertools<\/code> \u7edf\u8ba1\u5143\u7d20\u4e2a\u6570\uff1f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;banana&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 itertools.groupby \u7edf\u8ba1\u5143\u7d20\u4e2a\u6570<\/strong><\/h2>\n<p>sorted_list = sorted(my_list)<\/p>\n<p>element_count = {key: len(list(group)) for key, group in itertools.groupby(sorted_list)}<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(element_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u5185\u5b58\u6548\u7387\u9ad8\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u9700\u8981\u5148\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee3\u7801\u8f83\u590d\u6742\uff0c\u4e0d\u5982 <code>collections.Counter<\/code> \u76f4\u89c2\u3002<\/li>\n<\/ul>\n<p><h2>\u516d\u3001\u4f7f\u7528 <code>set<\/code> \u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><h3>\u5982\u4f55\u4f7f\u7528 <code>set<\/code> \u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7edf\u8ba1\u5143\u7d20\u4e2a\u6570\uff1f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;banana&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 set \u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7edf\u8ba1\u5143\u7d20\u4e2a\u6570<\/strong><\/h2>\n<p>element_count = {item: my_list.count(item) for item in set(my_list)}<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(element_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u4e0d\u9700\u8981\u5bfc\u5165\u989d\u5916\u6a21\u5757\u3002<\/li>\n<li><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u6548\u7387\u8f83\u4f4e\uff0c<code>my_list.count(item)<\/code> \u5728\u5927\u89c4\u6a21\u6570\u636e\u96c6\u4e2d\u6027\u80fd\u4e0d\u4f73\u3002<\/li>\n<\/ul>\n<p><h2>\u4e03\u3001\u6027\u80fd\u6bd4\u8f83\u4e0e\u603b\u7ed3<\/h2>\n<\/p>\n<p><h3>\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6027\u80fd\u662f\u9009\u62e9\u65b9\u6cd5\u7684\u4e00\u4e2a\u91cd\u8981\u8003\u91cf\u3002\u4ee5\u4e0b\u662f\u5bf9\u4e0a\u8ff0\u65b9\u6cd5\u5728\u4e0d\u540c\u89c4\u6a21\u6570\u636e\u96c6\u4e0a\u7684\u6027\u80fd\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5c0f\u89c4\u6a21\u6570\u636e\u96c6<\/strong>\uff1a<code>collections.Counter<\/code> \u548c <code>for<\/code> \u5faa\u73af\u8868\u73b0\u4f18\u5f02\u3002<\/li>\n<li><strong>\u4e2d\u7b49\u89c4\u6a21\u6570\u636e\u96c6<\/strong>\uff1a<code>collections.Counter<\/code> \u548c <code>pandas<\/code> \u8868\u73b0\u4f18\u5f02\u3002<\/li>\n<li><strong>\u5927\u89c4\u6a21\u6570\u636e\u96c6<\/strong>\uff1a<code>numpy<\/code> \u548c <code>itertools<\/code> \u8868\u73b0\u4f18\u5f02\u3002<\/li>\n<\/ul>\n<p><h3>\u9009\u62e9\u5efa\u8bae<\/h3>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u7edf\u8ba1\u4efb\u52a1<\/strong>\uff1a\u63a8\u8350\u4f7f\u7528 <code>collections.Counter<\/code>\u3002<\/li>\n<li><strong>\u5b66\u4e60\u548c\u7406\u89e3<\/strong>\uff1a\u63a8\u8350\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u548c\u5b57\u5178\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u6790\u4efb\u52a1<\/strong>\uff1a\u63a8\u8350\u4f7f\u7528 <code>pandas<\/code> \u548c <code>numpy<\/code>\u3002<\/li>\n<li><strong>\u5185\u5b58\u6548\u7387\u8981\u6c42\u9ad8<\/strong>\uff1a\u63a8\u8350\u4f7f\u7528 <code>itertools<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u7edf\u8ba1\u5217\u8868\u4e2d\u76f8\u540c\u5143\u7d20\u7684\u4e2a\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u901a\u8fc7\u5b9e\u9645\u5e94\u7528\u548c\u6027\u80fd\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9ad8\u6548\u7edf\u8ba1\u5217\u8868\u4e2d\u76f8\u540c\u5143\u7d20\u7684\u4e2a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7edf\u8ba1\u5217\u8868\u4e2d\u76f8\u540c\u5143\u7d20\u7684\u4e2a\u6570\u53ef\u4ee5\u4f7f\u7528<code>collections.Counter<\/code>\u7c7b\uff0c\u5b83\u80fd\u591f\u5feb\u901f\u5730\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8ba1\u6570\u3002\u53ea\u9700\u4f20\u5165\u5217\u8868\uff0cCounter\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u952e\u4e3a\u5143\u7d20\uff0c\u503c\u4e3a\u8be5\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\nmy_list = [&#39;a&#39;, &#39;b&#39;, &#39;a&#39;, &#39;c&#39;, &#39;b&#39;, &#39;a&#39;]\nelement_count = Counter(my_list)\nprint(element_count)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa<code>Counter({&#39;a&#39;: 3, &#39;b&#39;: 2, &#39;c&#39;: 1})<\/code>\uff0c\u663e\u793a\u6bcf\u4e2a\u5143\u7d20\u53ca\u5176\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u7edf\u8ba1\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>collections.Counter<\/code>\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u548c\u5faa\u73af\u6765\u624b\u52a8\u7edf\u8ba1\u5143\u7d20\u4e2a\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u5b83\u662f\u5426\u5df2\u7ecf\u5728\u5b57\u5178\u4e2d\uff0c\u5982\u679c\u5728\uff0c\u5219\u589e\u52a0\u8ba1\u6570\uff1b\u5982\u679c\u4e0d\u5728\uff0c\u5219\u521d\u59cb\u5316\u8ba1\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;b&#39;, &#39;a&#39;, &#39;c&#39;, &#39;b&#39;, &#39;a&#39;]\nelement_count = {}\nfor item in my_list:\n    if item in element_count:\n        element_count[item] += 1\n    else:\n        element_count[item] = 1\nprint(element_count)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u4ee3\u7801\u91cf\u591a\u4e00\u4e9b\uff0c\u4f46\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5b57\u5178\u7684\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u5d4c\u5957\u5217\u8868\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u5d4c\u5957\u5217\u8868\uff0c\u7edf\u8ba1\u5143\u7d20\u7684\u4e2a\u6570\u4f1a\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u5c55\u5e73\u5217\u8868\uff0c\u7136\u540e\u518d\u8fdb\u884c\u7edf\u8ba1\u3002\u53ef\u4ee5\u4f7f\u7528<code>itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/code>\u6765\u5b9e\u73b0\u5c55\u5e73\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\nfrom itertools import chain\n\nnested_list = [[&#39;a&#39;, &#39;b&#39;], [&#39;a&#39;, &#39;c&#39;, [&#39;b&#39;, &#39;a&#39;]]]\nflat_list = list(chain.from_iterable(item if isinstance(item, list) else [item] for item in nested_list))\nelement_count = Counter(flat_list)\nprint(element_count)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u5d4c\u5957\u5217\u8868\u5c55\u5e73\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u5143\u7d20\u7684\u4e2a\u6570\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4f1a\u663e\u793a\u6bcf\u4e2a\u5143\u7d20\u5728\u6700\u7ec8\u5e73\u9762\u5217\u8868\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7edf\u8ba1Python\u5217\u8868\u4e2d\u76f8\u540c\u5143\u7d20\u7684\u4e2a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 collections.Counter \u6a21\u5757\u3001for \u5faa\u73af\u3001 [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127071"}],"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=1127071"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127071\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1127071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1127071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1127071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}