{"id":176998,"date":"2024-05-08T19:21:50","date_gmt":"2024-05-08T11:21:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176998.html"},"modified":"2024-05-08T19:21:55","modified_gmt":"2024-05-08T11:21:55","slug":"%e6%80%8e%e4%b9%88%e7%94%a8python%e7%bb%9f%e8%ae%a1%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e6%af%8f%e4%b8%aa%e5%ad%97%e7%ac%a6%e5%87%ba%e7%8e%b0%e7%9a%84%e6%ac%a1%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/176998.html","title":{"rendered":"\u600e\u4e48\u7528python\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060827\/9b13de79-e15e-4536-a223-ee435d8b757c.webp\" alt=\"\u600e\u4e48\u7528python\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\" \/><\/p>\n<p><p>\u4f7f\u7528Python\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec<strong>\u5229\u7528\u5b57\u5178\u3001Counter\u7c7b<\/strong>\u4ee5\u53ca<strong>defaultdict\u7c7b<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u7279\u70b9\uff0c\u53ef\u4ee5\u9488\u5bf9\u4e0d\u540c\u7684\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u4e3b\u8981\u9488\u5bf9<strong>\u5229\u7528\u5b57\u5178<\/strong>\u8fd9\u79cd\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u5229\u7528\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5229\u7528\u5b57\u5178\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u53ef\u53d8\u5bb9\u5668\u6a21\u578b\uff0c\u4e14\u53ef\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\u5bf9\u8c61\u3002\u5b57\u5178\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\u4e2d\u952e\u7528\u4e8e\u552f\u4e00\u6807\u8bc6\u5143\u7d20\uff0c\u503c\u5b58\u653e\u4e0e\u952e\u76f8\u5173\u8054\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5b57\u5178<\/strong>\uff0c\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u5b57\u7b26\uff0c<strong>\u68c0\u67e5\u662f\u5426\u5df2\u5728\u5b57\u5178\u4e2d<\/strong>\u3002\u5982\u679c\u662f\uff0c\u589e\u52a0\u8be5\u5b57\u7b26\u7684\u8ba1\u6570\uff1b\u5982\u679c\u4e0d\u662f\uff0c\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u8bbe\u7f6e\u8ba1\u6570\u4e3a1\u3002<\/li>\n<\/ol>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u5173\u952e\u5728\u4e8e\uff0c\u5b57\u5178\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u67e5\u627e\u548c\u66f4\u65b0\u5143\u7d20\u3002\u5728Python\u4e2d\u7684\u5b9e\u73b0\u975e\u5e38\u7b80\u6d01\uff0c\u5bf9\u4e8e\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u6765\u8bf4\uff0c\u8db3\u591f\u9ad8\u6548\u548c\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Counter\u7c7b<\/h3>\n<\/p>\n<p><p>Counter\u7c7b\u662fcollections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2aCounter\u5bf9\u8c61\uff0c\u5176\u5143\u7d20\u4e3a\u8f93\u5165\u7684\u5143\u7d20\uff0c\u8ba1\u6570\u503c\u4e3a\u5143\u7d20\u5728\u539f\u8f93\u5165\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u8fd9\u662f\u4e00\u4e2a\u5feb\u901f\u800c\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4ececollections\u6a21\u5757\u5bfc\u5165Counter\u7c7b\u3002<\/li>\n<li>\u5c06\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9Counter\u7c7b\uff0c\u4ea7\u751f\u4e00\u4e2aCounter\u5bf9\u8c61\u3002<\/li>\n<li>\u8be5\u5bf9\u8c61\u5c06\u81ea\u52a8\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u4ee5\u5b57\u5178\u683c\u5f0f\u5b58\u50a8\u3002<\/li>\n<\/ol>\n<p><h3>\u5229\u7528defaultdict\u7c7b<\/h3>\n<\/p>\n<p><p>defaultdict\u7c7b\u4e5f\u662fcollections\u6a21\u5757\u7684\u4e00\u90e8\u5206\u3002\u4e0e\u666e\u901a\u7684\u5b57\u5178\u76f8\u6bd4\uff0cdefaultdict\u7684\u4e3b\u8981\u7279\u70b9\u662f\u5b83\u4f1a\u4e3a\u90a3\u4e9b\u8fd8\u6ca1\u6709\u8bbe\u7f6e\u7684\u952e\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u4f7f\u5f97\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u65f6\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4ececollections\u6a21\u5757\u5bfc\u5165defaultdict\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2adefaultdict\u5bf9\u8c61\uff0c\u6307\u5b9a\u9ed8\u8ba4\u7c7b\u578b\u4e3aint\uff0c\u56e0\u4e3a\u5b57\u7b26\u8ba1\u6570\u662f\u6570\u5b57\u3002<\/li>\n<li>\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u8ba1\u6570\u3002<\/li>\n<\/ol>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><h4>\u5229\u7528\u5b57\u5178<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def count_chars_with_dict(s):<\/p>\n<p>    count_dict = {}<\/p>\n<p>    for char in s:<\/p>\n<p>        if char in count_dict:<\/p>\n<p>            count_dict[char] += 1<\/p>\n<p>        else:<\/p>\n<p>            count_dict[char] = 1<\/p>\n<p>    return count_dict<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Counter\u7c7b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_chars_with_counter(s):<\/p>\n<p>    return Counter(s)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5229\u7528defaultdict\u7c7b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>def count_chars_with_defaultdict(s):<\/p>\n<p>    count_dict = defaultdict(int)<\/p>\n<p>    for char in s:<\/p>\n<p>        count_dict[char] += 1<\/p>\n<p>    return count_dict<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u53ef\u4ee5\u900f\u8fc7\u4e0d\u540c\u7684\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5176\u4e2d\u5229\u7528\u5b57\u5178\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u6839\u636e\u5177\u4f53\u60c5\u51b5\u548c\u4e2a\u4eba\u504f\u597d\uff0c\u53ef\u4ee5\u9009\u62e9\u9002\u7528\u6027\u6700\u5f3a\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u76f4\u63a5\u4f7f\u7528\u5b57\u5178\uff0c\u8fd8\u662f\u5229\u7528Counter\u7c7b\u6216defaultdict\u7c7b\uff0cPython\u4e3a\u8fd9\u79cd\u5e38\u89c1\u7684\u9700\u6c42\u63d0\u4f9b\u4e86\u7b80\u5355\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u6211\u8be5\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4ee3\u7801\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178(Dictionary)\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u5b57\u5178\u6765\u5b58\u50a8\u7ed3\u679c\u3002\u7136\u540e\uff0c\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u5b57\u7b26\u5df2\u7ecf\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u5bf9\u5e94\u7684\u503c\u52a01\uff1b\u5982\u679c\u5b57\u7b26\u4e0d\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\uff0c\u5e76\u5c06\u5bf9\u5e94\u7684\u503c\u521d\u59cb\u5316\u4e3a1\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">def count_characters(string):\n    char_count = {}\n    for char in string:\n        if char in char_count:\n            char_count[char] += 1\n        else:\n            char_count[char] = 1\n    return char_count\n\nstring = &quot;Hello, World!&quot;\nresult = count_characters(string)\nprint(result)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>{&#039;H&#039;: 1, &#039;e&#039;: 1, &#039;l&#039;: 3, &#039;o&#039;: 2, &#039;,&#039;: 1, &#039; &#039;: 1, &#039;W&#039;: 1, &#039;r&#039;: 1, &#039;d&#039;: 1, &#039;!&#039;: 1}\n<\/code><\/pre>\n<p><strong>2. \u5982\u4f55\u7528Python\u7f16\u5199\u4ee3\u7801\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u5e76\u6309\u7167\u6b21\u6570\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\uff1f<\/strong><\/p>\n<p>\u5728\u524d\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u5230\u5982\u4f55\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u6309\u7167\u51fa\u73b0\u6b21\u6570\u7684\u503c\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>sorted()<\/code>\u548c\u5b57\u5178\u7684<code>items()<\/code>\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">def count_characters(string):\n    char_count = {}\n    for char in string:\n        if char in char_count:\n            char_count[char] += 1\n        else:\n            char_count[char] = 1\n    return char_count\n\nstring = &quot;Hello, World!&quot;\nresult = count_characters(string)\nsorted_result = sorted(result.items(), key=lambda x: x[1], reverse=True)\nprint(sorted_result)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>[(&#039;l&#039;, 3), (&#039;o&#039;, 2), (&#039;H&#039;, 1), (&#039;e&#039;, 1), (&#039;,&#039;, 1), (&#039; &#039;, 1), (&#039;W&#039;, 1), (&#039;r&#039;, 1), (&#039;d&#039;, 1), (&#039;!&#039;, 1)]\n<\/code><\/pre>\n<p><strong>3. \u5728Python\u4e2d\uff0c\u6709\u6ca1\u6709\u73b0\u6210\u7684\u5e93\u6216\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0cPython\u4e2d\u6709\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u5e93\u51fd\u6570<code>collections.Counter()<\/code>\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u8be5\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u503c\u662f\u5bf9\u5e94\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>collections.Counter()<\/code>\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nstring = &quot;Hello, World!&quot;\nresult = Counter(string)\nprint(result)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>Counter({&#039;l&#039;: 3, &#039;o&#039;: 2, &#039;H&#039;: 1, &#039;e&#039;: 1, &#039;,&#039;: 1, &#039; &#039;: 1, &#039;W&#039;: 1, &#039;r&#039;: 1, &#039;d&#039;: 1, &#039;!&#039;: 1})\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>collections.Counter()<\/code>\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u5e76\u4e14\u5b83\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u4e00\u4e9b\u4fbf\u5229\u7684\u65b9\u6cd5\u6765\u5904\u7406\u51fa\u73b0\u6b21\u6570\u7684\u7edf\u8ba1\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec\u5229\u7528\u5b57\u5178\u3001Counter\u7c7b\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":177006,"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\/176998"}],"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=176998"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176998\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177006"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}