{"id":1095392,"date":"2025-01-08T14:50:40","date_gmt":"2025-01-08T06:50:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095392.html"},"modified":"2025-01-08T14:50:42","modified_gmt":"2025-01-08T06:50:42","slug":"python%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e6%af%8f%e4%b8%aa%e6%95%b4%e6%95%b0%e5%87%ba%e7%8e%b0%e9%a2%91%e7%8e%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095392.html","title":{"rendered":"python\u5982\u4f55\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u9891\u7387"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211053\/f43d6730-41b3-4c76-b50b-3c7cb906563f.webp\" alt=\"python\u5982\u4f55\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u9891\u7387\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u9891\u7387\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528Counter\u6a21\u5757\u3001\u4f7f\u7528Pandas\u5e93\u3002<\/strong> \u8fd9\u4e09\u79cd\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u5728\u6570\u636e\u96c6\u4e2d\u51fa\u73b0\u7684\u9891\u7387\u3002<strong>\u4f7f\u7528\u5b57\u5178<\/strong>\u7684\u65b9\u6cd5\u662f\u5c06\u6bcf\u4e2a\u6574\u6570\u4f5c\u4e3a\u952e\uff0c\u51fa\u73b0\u7684\u6b21\u6570\u4f5c\u4e3a\u503c\uff0c\u8fdb\u884c\u9891\u7387\u7edf\u8ba1\u3002<strong>\u4f7f\u7528Counter\u6a21\u5757<\/strong>\u7684\u65b9\u6cd5\u5219\u662f\u5229\u7528collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b\uff0c\u5b83\u4e13\u4e3a\u7edf\u8ba1\u9891\u7387\u800c\u8bbe\u8ba1\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u4fbf\u3002<strong>\u4f7f\u7528Pandas\u5e93<\/strong>\u7684\u65b9\u6cd5\u5219\u9002\u7528\u4e8e\u8f83\u5927\u6570\u636e\u96c6\u6216\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u636e\u5206\u6790\u7684\u60c5\u51b5\uff0cPandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u9891\u7387<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u6574\u6570\u51fa\u73b0\u7684\u9891\u7387\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u65b9\u6cd5\u3002\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8bb0\u5f55\u6bcf\u4e2a\u6574\u6570\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002<\/li>\n<li>\u904d\u5386\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u6574\u6570\u3002<\/li>\n<li>\u5982\u679c\u8be5\u6574\u6570\u5df2\u7ecf\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u5c06\u5176\u5bf9\u5e94\u7684\u503c\u52a01\u3002<\/li>\n<li>\u5982\u679c\u8be5\u6574\u6570\u4e0d\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u5c06\u5176\u503c\u8bbe\u4e3a1\u3002<\/li>\n<\/ol>\n<p><h4>2. \u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def count_frequency(int_list):<\/p>\n<p>    frequency_dict = {}<\/p>\n<p>    for num in int_list:<\/p>\n<p>        if num in frequency_dict:<\/p>\n<p>            frequency_dict[num] += 1<\/p>\n<p>        else:<\/p>\n<p>            frequency_dict[num] = 1<\/p>\n<p>    return frequency_dict<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]<\/p>\n<p>frequency = count_frequency(data)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Counter\u6a21\u5757\u7edf\u8ba1\u9891\u7387<\/h3>\n<\/p>\n<p><p>Counter\u662fcollections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u7684\u9891\u7387\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u5e76\u4e14\u6027\u80fd\u8f83\u597d\uff0c\u9002\u5408\u7528\u4e8e\u5404\u79cd\u9891\u7387\u7edf\u8ba1\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\u5bfc\u5165collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b\u3002<\/li>\n<li>\u5c06\u6570\u636e\u96c6\u4f20\u9012\u7ed9Counter\u7c7b\uff0c\u751f\u6210\u4e00\u4e2aCounter\u5bf9\u8c61\u3002<\/li>\n<li>\u76f4\u63a5\u8f93\u51faCounter\u5bf9\u8c61\uff0c\u5373\u53ef\u5f97\u5230\u6bcf\u4e2a\u6574\u6570\u7684\u9891\u7387\u7edf\u8ba1\u3002<\/li>\n<\/ol>\n<p><h4>2. \u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]<\/p>\n<p>frequency = Counter(data)<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u7edf\u8ba1\u9891\u7387<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002\u4f7f\u7528Pandas\u8fdb\u884c\u9891\u7387\u7edf\u8ba1\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528\u5176\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\u5bfc\u5165pandas\u5e93\u3002<\/li>\n<li>\u5c06\u6570\u636e\u96c6\u8f6c\u6362\u4e3aPandas\u7684Series\u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528value_counts()\u65b9\u6cd5\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u7684\u9891\u7387\u3002<\/li>\n<\/ol>\n<p><h4>2. \u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]<\/p>\n<p>series = pd.Series(data)<\/p>\n<p>frequency = series.value_counts()<\/p>\n<p>print(frequency)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6bd4\u8f83\u4e0e\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8fd9\u4e09\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u800c\u5b9a\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a\u5b9e\u73b0\u7b80\u5355\uff0c\u9002\u5408\u5c0f\u89c4\u6a21\u6570\u636e\u96c6\u3002<\/p>\n<p>\u7f3a\u70b9\uff1a\u4ee3\u7801\u8f83\u4e3a\u5197\u957f\uff0c\u4e0d\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Counter\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a\u5b9e\u73b0\u7b80\u6d01\uff0c\u6027\u80fd\u8f83\u597d\uff0c\u9002\u5408\u4e00\u822c\u89c4\u6a21\u7684\u6570\u636e\u96c6\u3002<\/p>\n<p>\u7f3a\u70b9\uff1a\u5bf9\u4e8e\u7279\u522b\u5927\u578b\u7684\u6570\u636e\u96c6\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/p>\n<p>\u7f3a\u70b9\uff1a\u4f9d\u8d56\u4e8ePandas\u5e93\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\u7684\u5bf9\u6bd4\u548c\u5b9e\u8df5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u7edf\u8ba1\u9891\u7387\u7684\u65b9\u6cd5\u3002\u5728\u65e5\u5e38\u7684\u7f16\u7a0b\u4efb\u52a1\u4e2d\uff0c\u638c\u63e1\u8fd9\u4e09\u79cd\u65b9\u6cd5\u80fd\u591f\u5e94\u5bf9\u4e0d\u540c\u89c4\u6a21\u548c\u590d\u6742\u5ea6\u7684\u6570\u636e\u7edf\u8ba1\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\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\u6bcf\u4e2a\u6574\u6570\u7684\u51fa\u73b0\u9891\u7387\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>collections.Counter<\/code>\u7c7b\u6765\u7edf\u8ba1\u6574\u6570\u51fa\u73b0\u7684\u9891\u7387\u3002\u53ea\u9700\u5c06\u6574\u6570\u5217\u8868\u4f20\u9012\u7ed9Counter\u5bf9\u8c61\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u4e3a\u6574\u6570\uff0c\u503c\u4e3a\u5176\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nnumbers = [1, 2, 2, 3, 3, 3, 4]\nfrequency = Counter(numbers)\nprint(frequency)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<code>Counter({3: 3, 2: 2, 1: 1, 4: 1})<\/code>\uff0c\u8868\u793a3\u51fa\u73b0\u4e863\u6b21\uff0c2\u51fa\u73b0\u4e862\u6b21\uff0c1\u548c4\u5404\u51fa\u73b01\u6b21\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u7edf\u8ba1\u6574\u6570\u7684\u9891\u7387\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\uff0cPandas\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u7edf\u8ba1\u9891\u7387\u3002\u53ef\u4ee5\u5c06\u6574\u6570\u5217\u8868\u8f6c\u6362\u4e3aSeries\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>value_counts()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\nnumbers = [1, 2, 2, 3, 3, 3, 4]\nfrequency = pd.Series(numbers).value_counts()\nprint(frequency)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u4e00\u4e2a\u6309\u9891\u7387\u6392\u5e8f\u7684Series\uff0c\u663e\u793a\u6bcf\u4e2a\u6574\u6570\u53ca\u5176\u5bf9\u5e94\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5305\u542b\u8d1f\u6574\u6570\u548c\u96f6\u7684\u9891\u7387\u7edf\u8ba1\uff1f<\/strong><br \/>\u65e0\u8bba\u5217\u8868\u4e2d\u5305\u542b\u6b63\u6570\u3001\u8d1f\u6570\u8fd8\u662f\u96f6\uff0c\u4f7f\u7528<code>Counter<\/code>\u6216<code>value_counts()<\/code>\u65b9\u6cd5\u90fd\u80fd\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, -1, 0, -1, 0, 0]\nfrequency = Counter(numbers)\nprint(frequency)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u5c06\u663e\u793a\u6bcf\u4e2a\u6574\u6570\u53ca\u5176\u51fa\u73b0\u6b21\u6570\uff0c\u5305\u62ec\u8d1f\u6570\u548c\u96f6\uff1a<code>Counter({0: 3, -1: 2, 1: 1})<\/code>\u3002\u8fd9\u8868\u660e\u96f6\u51fa\u73b0\u4e863\u6b21\uff0c-1\u51fa\u73b0\u4e862\u6b21\uff0c1\u51fa\u73b0\u4e861\u6b21\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7edf\u8ba1\u6bcf\u4e2a\u6574\u6570\u51fa\u73b0\u9891\u7387\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528Counter\u6a21\u5757\u3001\u4f7f\u7528Pandas\u5e93\u3002 \u8fd9\u4e09 [&hellip;]","protected":false},"author":3,"featured_media":1095399,"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\/1095392"}],"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=1095392"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095392\/revisions"}],"predecessor-version":[{"id":1095400,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095392\/revisions\/1095400"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095399"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}