{"id":989638,"date":"2024-12-27T08:13:17","date_gmt":"2024-12-27T00:13:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/989638.html"},"modified":"2024-12-27T08:13:20","modified_gmt":"2024-12-27T00:13:20","slug":"python%e4%b8%ad%e5%ad%97%e6%af%8d%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/989638.html","title":{"rendered":"python\u4e2d\u5b57\u6bcd\u5982\u4f55\u8ba1\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064733\/ae3325d6-b2a0-42ce-9931-5572ff7a700a.webp\" alt=\"python\u4e2d\u5b57\u6bcd\u5982\u4f55\u8ba1\u7b97\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8ba1\u7b97\u5b57\u6bcd\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5177\u4f53\u65b9\u6cd5\u5305\u62ec<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3001\u5229\u7528Counter\u7c7b\u3001\u901a\u8fc7\u5b57\u5178\u8ba1\u6570<\/strong>\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u901a\u8fc7\u5b57\u5178\u8ba1\u6570\u3002\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5b57\u6bcd\u51fa\u73b0\u7684\u6b21\u6570\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5224\u65ad\u6bcf\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\uff0c\u5982\u679c\u662f\u5219\u5c06\u5176\u8ba1\u6570\u5668\u589e\u52a0\u3002\u4e0b\u9762\u662f\u5177\u4f53\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u8ba1\u6570\u5b57\u6bcd<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u975e\u5e38\u9002\u5408\u7528\u6765\u5b58\u50a8\u5b57\u6bcd\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\u3002\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u8bb0\u5f55\u6bcf\u4e2a\u5b57\u6bcd\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u521d\u59cb\u5316\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u7a7a\u5b57\u5178\u6765\u5b58\u50a8\u5b57\u6bcd\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\u3002\u5f53\u904d\u5386\u5b57\u7b26\u4e32\u65f6\uff0c\u6211\u4eec\u5c06\u6bcf\u4e2a\u5b57\u6bcd\u4f5c\u4e3a\u952e\uff0c\u5e76\u8bb0\u5f55\u5176\u51fa\u73b0\u6b21\u6570\u4f5c\u4e3a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Your text here&quot;<\/p>\n<p>letter_count = {}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u904d\u5386\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u68c0\u67e5\u5b83\u662f\u5426\u662f\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for char in text:<\/p>\n<p>    if char.isalpha():<\/p>\n<p>        char = char.lower()  # \u5c06\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\uff0c\u4ee5\u786e\u4fdd\u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u66f4\u65b0\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5728\u786e\u8ba4\u5b57\u7b26\u662f\u5b57\u6bcd\u540e\uff0c\u6211\u4eec\u9700\u8981\u66f4\u65b0\u5b57\u5178\u3002\u5982\u679c\u5b57\u6bcd\u5df2\u7ecf\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u6211\u4eec\u5c06\u5176\u8ba1\u6570\u5668\u589e\u52a01\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u5c06\u8ba1\u6570\u5668\u8bbe\u7f6e\u4e3a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">        if char in letter_count:<\/p>\n<p>            letter_count[char] += 1<\/p>\n<p>        else:<\/p>\n<p>            letter_count[char] = 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u8f93\u51fa\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5b8c\u6210\u904d\u5386\u540e\uff0c\u5b57\u5178<code>letter_count<\/code>\u5c06\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u51fa\u73b0\u7684\u6b21\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u5b57\u5178\u5e76\u8f93\u51fa\u6bcf\u4e2a\u5b57\u6bcd\u53ca\u5176\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for letter, count in letter_count.items():<\/p>\n<p>    print(f&quot;{letter}: {count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Counter\u7c7b<\/h3>\n<\/p>\n<p><p>Python\u7684<code>collections<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>Counter<\/code>\u7c7b\uff0c\u53ef\u4ee5\u7528\u6765\u8f7b\u677e\u5b9e\u73b0\u5b57\u6bcd\u8ba1\u6570\u3002\u5b83\u662f\u5b57\u5178\u7684\u5b50\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u8ba1\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165Counter\u7c7b<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4ece<code>collections<\/code>\u6a21\u5757\u5bfc\u5165<code>Counter<\/code>\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8ba1\u6570\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>Counter<\/code>\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f20\u5165\u5b57\u7b26\u4e32\u6765\u8ba1\u6570\u3002\u4e3a\u4e86\u53ea\u8ba1\u6570\u5b57\u6bcd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u6389\u975e\u5b57\u6bcd\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Your text here&quot;<\/p>\n<p>letter_count = Counter(char.lower() for char in text if char.isalpha())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8f93\u51fa\u7ed3\u679c<\/h4>\n<\/p>\n<p><p><code>Counter<\/code>\u5bf9\u8c61\u7c7b\u4f3c\u4e8e\u5b57\u5178\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for letter, count in letter_count.items():<\/p>\n<p>    print(f&quot;{letter}: {count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u672c\u8eab\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8ba1\u7b97\u5b57\u6bcd\u7684\u6570\u91cf\u3002\u4f8b\u5982\uff0c<code>str.count()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u7279\u5b9a\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528str.count()<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u53ea\u5173\u5fc3\u67d0\u4e2a\u7279\u5b9a\u5b57\u6bcd\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.count()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u76f4\u63a5\u8fd4\u56de\u6307\u5b9a\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Your text here&quot;<\/p>\n<p>letter = &#39;a&#39;<\/p>\n<p>count = text.lower().count(letter)<\/p>\n<p>print(f&quot;The letter &#39;{letter}&#39; appears {count} times.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u904d\u5386\u5b57\u6bcd\u8868<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u8ba1\u7b97\u6240\u6709\u5b57\u6bcd\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u53ef\u4ee5\u904d\u5386\u5b57\u6bcd\u8868\uff0c\u4f7f\u7528<code>str.count()<\/code>\u65b9\u6cd5\u5bf9\u6bcf\u4e2a\u5b57\u6bcd\u8fdb\u884c\u8ba1\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import string<\/p>\n<p>text = &quot;Your text here&quot;<\/p>\n<p>for letter in string.ascii_lowercase:<\/p>\n<p>    count = text.lower().count(letter)<\/p>\n<p>    if count &gt; 0:<\/p>\n<p>        print(f&quot;{letter}: {count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8ba1\u7b97\u5b57\u6bcd\u7684\u9891\u7387\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u3001<code>Counter<\/code>\u7c7b\u548c\u5b57\u7b26\u4e32\u65b9\u6cd5\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u4f7f\u7528\u5b57\u5178\u53ef\u4ee5\u7075\u6d3b\u5730\u5bf9\u6bcf\u4e2a\u5b57\u6bcd\u8fdb\u884c\u8ba1\u6570\uff0c\u4f7f\u7528<code>Counter<\/code>\u7c7b\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u800c<code>str.count()<\/code>\u65b9\u6cd5\u5219\u9002\u5408\u7528\u4e8e\u5355\u4e2a\u5b57\u6bcd\u7684\u8ba1\u6570<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u5b57\u6bcd\u7684\u6570\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>count()<\/code> \u65b9\u6cd5\u6765\u8ba1\u7b97\u7279\u5b9a\u5b57\u6bcd\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff0c<code>my_string.count(&#39;a&#39;)<\/code> \u4f1a\u8fd4\u56de\u5b57\u7b26\u4e32 <code>my_string<\/code> \u4e2d\u5b57\u6bcd &#39;a&#39; \u7684\u51fa\u73b0\u6b21\u6570\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u624b\u52a8\u8ba1\u6570\u6bcf\u4e2a\u5b57\u6bcd\u3002<\/p>\n<p><strong>Python\u662f\u5426\u652f\u6301\u5b57\u6bcd\u7684ASCII\u503c\u8ba1\u7b97\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 <code>ord()<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5176\u5bf9\u5e94\u7684ASCII\u503c\u3002\u4f8b\u5982\uff0c<code>ord(&#39;A&#39;)<\/code> \u8fd4\u56de65\uff0c<code>ord(&#39;a&#39;)<\/code> \u8fd4\u56de97\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8fdb\u884c\u5b57\u6bcd\u7684ASCII\u503c\u8ba1\u7b97\u548c\u6bd4\u8f83\u3002<\/p>\n<p><strong>\u600e\u6837\u5728Python\u4e2d\u5bf9\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684 <code>sorted()<\/code> \u51fd\u6570\u5bf9\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\u3002\u5c06\u5305\u542b\u5b57\u6bcd\u7684\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9 <code>sorted()<\/code> \u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6392\u597d\u5e8f\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>sorted(&quot;python&quot;)<\/code> \u5c06\u8fd4\u56de <code>[&#39;h&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;t&#39;, &#39;y&#39;]<\/code>\u3002\u5982\u679c\u9700\u8981\u5c06\u7ed3\u679c\u518d\u7ec4\u5408\u6210\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>&#39;&#39;.join()<\/code> \u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8ba1\u7b97\u5b57\u6bcd\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3001\u5229\u7528Counter\u7c7b\u3001\u901a\u8fc7\u5b57\u5178\u8ba1\u6570 [&hellip;]","protected":false},"author":3,"featured_media":989648,"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\/989638"}],"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=989638"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989638\/revisions"}],"predecessor-version":[{"id":989652,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989638\/revisions\/989652"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/989648"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=989638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=989638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=989638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}