{"id":1092203,"date":"2025-01-08T14:19:02","date_gmt":"2025-01-08T06:19:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1092203.html"},"modified":"2025-01-08T14:19:04","modified_gmt":"2025-01-08T06:19:04","slug":"python%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e5%88%97%e8%a1%a8%e4%b8%ad%e6%95%b0%e5%ad%97%e4%b8%aa%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1092203.html","title":{"rendered":"python\u5982\u4f55\u7edf\u8ba1\u5217\u8868\u4e2d\u6570\u5b57\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205336\/5b1692c2-f6f0-4d20-ada6-8e50483a368c.webp\" alt=\"python\u5982\u4f55\u7edf\u8ba1\u5217\u8868\u4e2d\u6570\u5b57\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7edf\u8ba1\u5217\u8868\u4e2d\u6570\u5b57\u7684\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53caCounter\u6a21\u5757\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>count()<\/code>\u51fd\u6570\u662f\u6700\u7b80\u5355\u3001\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u7edf\u8ba1\u5217\u8868\u4e2d\u67d0\u4e2a\u7279\u5b9a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>list.count(element)<\/code>\u6765\u7edf\u8ba1\u67d0\u4e2a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 2, 1, 4, 5, 1]<\/p>\n<p>count_of_ones = numbers.count(1)<\/p>\n<p>print(f&quot;Number of 1s: {count_of_ones}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u4f46\u53ea\u80fd\u7edf\u8ba1\u5355\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u7edf\u8ba1\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5219\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868\u662f\u4e00\u79cd\u901a\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7edf\u8ba1\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5b57\u5178\u6765\u5b58\u50a8\u6bcf\u4e2a\u6570\u5b57\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 2, 1, 4, 5, 1]<\/p>\n<p>count_dict = {}<\/p>\n<p>for number in numbers:<\/p>\n<p>    if number in count_dict:<\/p>\n<p>        count_dict[number] += 1<\/p>\n<p>    else:<\/p>\n<p>        count_dict[number] = 1<\/p>\n<p>print(count_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u901a\u7528\u6027\u5f3a\uff0c\u53ef\u4ee5\u7edf\u8ba1\u4efb\u610f\u7c7b\u578b\u7684\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u3002\u4f46\u9700\u8981\u7f16\u5199\u66f4\u591a\u7684\u4ee3\u7801\uff0c\u4e14\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u5217\u8868\u751f\u6210\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408<code>set<\/code>\u548c<code>list<\/code>\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 2, 1, 4, 5, 1]<\/p>\n<p>unique_numbers = set(numbers)<\/p>\n<p>count_dict = {number: numbers.count(number) for number in unique_numbers}<\/p>\n<p>print(count_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u3001\u6613\u8bfb\uff0c\u4f46\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u4e3a<code>count()<\/code>\u51fd\u6570\u5728\u5185\u90e8\u4f1a\u904d\u5386\u6574\u4e2a\u5217\u8868\uff0c\u5bf9\u4e8e\u5927\u5217\u8868\u6765\u8bf4\u6027\u80fd\u8f83\u5dee\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Counter\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684<code>collections.Counter<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u800c\u4e14\u6027\u80fd\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>numbers = [1, 2, 3, 2, 1, 4, 5, 1]<\/p>\n<p>count_dict = Counter(numbers)<\/p>\n<p>print(count_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>Counter<\/code>\u6a21\u5757\u7684\u4f18\u70b9\u662f\u6027\u80fd\u9ad8\u3001\u4f7f\u7528\u7b80\u5355\uff0c\u800c\u4e14\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5982\u83b7\u53d6\u6700\u5e38\u89c1\u7684\u5143\u7d20\u7b49\u3002\u662f\u7edf\u8ba1\u5217\u8868\u4e2d\u6570\u5b57\u4e2a\u6570\u7684\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5185\u7f6e\u51fd\u6570<code>count()<\/code><\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u4f46\u53ea\u80fd\u7edf\u8ba1\u5355\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong>\u5faa\u73af\u904d\u5386\u5217\u8868<\/strong>\uff1a\u901a\u7528\u6027\u5f3a\uff0c\u4f46\u4ee3\u7801\u91cf\u5927\uff0c\u6613\u8bfb\u6027\u5dee\u3002<\/li>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u4f46\u6027\u80fd\u8f83\u4f4e\u3002<\/li>\n<li><strong>Counter\u6a21\u5757<\/strong>\uff1a\u6027\u80fd\u9ad8\u3001\u4f7f\u7528\u7b80\u5355\uff0c\u529f\u80fd\u4e30\u5bcc\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u53ea\u662f\u7edf\u8ba1\u5355\u4e2a\u5143\u7d20\u7684\u6b21\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>count()<\/code>\u51fd\u6570\uff1b\u5982\u679c\u9700\u8981\u7edf\u8ba1\u6240\u6709\u5143\u7d20\u7684\u6b21\u6570\uff0c\u5efa\u8bae\u4f7f\u7528<code>Counter<\/code>\u6a21\u5757\u3002\u5bf9\u4e8e\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>count()<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u6027\u80fd\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f18\u5316\u6027\u80fd\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u5217\u8868\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u6027\u80fd\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong>\uff1a\u751f\u6210\u5668\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u6027\u80fd\u66f4\u597d\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u4f1a\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/li>\n<li><strong>\u5e76\u884c\u5904\u7406<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5e76\u884c\u5904\u7406\u5217\u8868\uff0c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002<\/li>\n<li><strong>\u4f18\u5316\u7b97\u6cd5<\/strong>\uff1a\u5c3d\u91cf\u907f\u514d\u91cd\u590d\u904d\u5386\u5217\u8868\uff0c\u4f7f\u7528\u9ad8\u6548\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u7edf\u8ba1\u5217\u8868\u4e2d\u6570\u5b57\u7684\u4e2a\u6570\uff0c\u5305\u62ec\u5185\u7f6e\u51fd\u6570\u3001\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548cCounter\u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u5c3d\u91cf\u4f18\u5316\u6027\u80fd\uff0c\u4ee5\u63d0\u9ad8\u5904\u7406\u6548\u7387\u3002\u901a\u8fc7\u5bf9\u6bd4\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5b83\u4eec\u7684\u9002\u7528\u573a\u666f\uff0c\u4ece\u800c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u505a\u51fa\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5feb\u901f\u7edf\u8ba1\u5217\u8868\u4e2d\u4e0d\u540c\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>Counter<\/code>\u7c7b\u6765\u8f7b\u677e\u7edf\u8ba1\u5217\u8868\u4e2d\u6bcf\u4e2a\u6570\u5b57\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\nnumbers = [1, 2, 2, 3, 4, 4, 4, 5]\ncount = Counter(numbers)\nprint(count)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u6bcf\u4e2a\u6570\u5b57\u53ca\u5176\u5bf9\u5e94\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u53ea\u60f3\u7edf\u8ba1\u5217\u8868\u4e2d\u67d0\u4e2a\u7279\u5b9a\u6570\u5b57\u7684\u4e2a\u6570\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>list.count()<\/code>\u65b9\u6cd5\u6765\u7edf\u8ba1\u7279\u5b9a\u6570\u5b57\u7684\u4e2a\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u7edf\u8ba1\u6570\u5b572\u7684\u4e2a\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, 2, 2, 3, 4, 4, 4, 5]\ncount_of_two = numbers.count(2)\nprint(count_of_two)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de2\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u7edf\u8ba1\u6240\u6709\u6570\u5b57\u7684\u4e2a\u6570\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u6570\u5b57\u7684\u4e2a\u6570\u3002\u867d\u7136\u65b9\u5f0f\u4e0d\u5982<code>Counter<\/code>\u9ad8\u6548\uff0c\u4f46\u5bf9\u4e8e\u5c0f\u578b\u5217\u8868\u6765\u8bf4\u662f\u53ef\u884c\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, 2, 2, 3, 4, 4, 4, 5]\nunique_numbers = set(numbers)\ncount_dict = {num: numbers.count(num) for num in unique_numbers}\nprint(count_dict)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4e3a\u6bcf\u4e2a\u552f\u4e00\u6570\u5b57\u751f\u6210\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u5b83\u4eec\u5728\u5217\u8868\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7edf\u8ba1\u5217\u8868\u4e2d\u6570\u5b57\u7684\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53caCou [&hellip;]","protected":false},"author":3,"featured_media":1092206,"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\/1092203"}],"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=1092203"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092203\/revisions"}],"predecessor-version":[{"id":1092209,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092203\/revisions\/1092209"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1092206"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1092203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1092203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1092203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}