{"id":1105466,"date":"2025-01-08T16:31:12","date_gmt":"2025-01-08T08:31:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105466.html"},"modified":"2025-01-08T16:31:18","modified_gmt":"2025-01-08T08:31:18","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%ae%97%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%85%83%e7%b4%a0%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1105466.html","title":{"rendered":"\u5982\u4f55\u7528Python\u7b97\u5b57\u7b26\u4e32\u5143\u7d20\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070351\/6c1ab022-01b9-4700-a756-9d52de5840a8.webp\" alt=\"\u5982\u4f55\u7528Python\u7b97\u5b57\u7b26\u4e32\u5143\u7d20\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u7528Python\u8ba1\u7b97\u5b57\u7b26\u4e32\u5143\u7d20\u4e2a\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u5185\u7f6e\u51fd\u6570len()\u3001for\u5faa\u73af\u904d\u5386\u8ba1\u6570\u3001collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b\u3001str.count()\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u5185\u7f6e\u51fd\u6570len() \u662f\u6700\u5e38\u7528\u4e14\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u7ed9\u51fa\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5185\u7f6e\u51fd\u6570len()<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570 len()\uff0c\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u8fd9\u4e2a\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>length = len(string)<\/p>\n<p>print(&quot;\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f:&quot;, length)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 len() \u51fd\u6570\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u76ee\u6807\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u51fd\u6570\u4e2d\uff0c\u51fd\u6570\u4f1a\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u603b\u6570\u3002\u6b64\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u4e5f\u975e\u5e38\u9ad8\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001for\u5faa\u73af\u904d\u5386\u8ba1\u6570<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u7075\u6d3b\u7684\u8ba1\u6570\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u624b\u52a8\u8ba1\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u9700\u6c42\uff0c\u5982\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>count = 0<\/p>\n<p>for char in string:<\/p>\n<p>    count += 1<\/p>\n<p>print(&quot;\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f:&quot;, count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6ca1\u6709 len() \u51fd\u6570\u7b80\u5355\uff0c\u4f46\u53ef\u4ee5\u7075\u6d3b\u5730\u6dfb\u52a0\u66f4\u591a\u903b\u8f91\uff0c\u4f8b\u5982\u5ffd\u7565\u67d0\u4e9b\u5b57\u7b26\u6216\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b<\/p>\n<\/p>\n<p><p>Python \u7684 collections \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a Counter \u7c7b\uff0c\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5404\u4e2a\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7edf\u8ba1\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u8fd8\u53ef\u4ee5\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>from collections import Counter<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>counter = Counter(string)<\/p>\n<p>print(&quot;\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f:&quot;, sum(counter.values()))<\/p>\n<p>print(&quot;\u5404\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570:&quot;, counter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Counter \u7c7b\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u4e3a\u5b57\u7b26\uff0c\u503c\u4e3a\u8be5\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u901a\u8fc7 sum(counter.values()) \u53ef\u4ee5\u5f97\u5230\u5b57\u7b26\u4e32\u7684\u603b\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001str.count()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>str.count() \u65b9\u6cd5\u7528\u4e8e\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u5982\u679c\u9700\u8981\u7edf\u8ba1\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4f7f\u7528 len() \u66f4\u4e3a\u5408\u9002\uff0c\u4f46 str.count() \u53ef\u7528\u4e8e\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>char = &#39;o&#39;<\/p>\n<p>count = string.count(char)<\/p>\n<p>print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u51fa\u73b0\u7684\u6b21\u6570:&quot;, count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u7528\u4e8e\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5404\u7c7b\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5982\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u7a7a\u683c\u548c\u5176\u4ed6\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>from collections import Counter<\/p>\n<p>string = &quot;Hello, World! 123&quot;<\/p>\n<p>counter = Counter(string)<\/p>\n<p>letters = sum(c.isalpha() for c in string)<\/p>\n<p>digits = sum(c.isdigit() for c in string)<\/p>\n<p>spaces = sum(c.isspace() for c in string)<\/p>\n<p>others = len(string) - letters - digits - spaces<\/p>\n<p>print(&quot;\u5b57\u6bcd\u7684\u4e2a\u6570:&quot;, letters)<\/p>\n<p>print(&quot;\u6570\u5b57\u7684\u4e2a\u6570:&quot;, digits)<\/p>\n<p>print(&quot;\u7a7a\u683c\u7684\u4e2a\u6570:&quot;, spaces)<\/p>\n<p>print(&quot;\u5176\u4ed6\u5b57\u7b26\u7684\u4e2a\u6570:&quot;, others)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 isalpha()\u3001isdigit() \u548c isspace() \u5206\u522b\u7edf\u8ba1\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u7a7a\u683c\u7684\u4e2a\u6570\uff0c\u6700\u540e\u901a\u8fc7\u5b57\u7b26\u4e32\u7684\u603b\u957f\u5ea6\u51cf\u53bb\u8fd9\u4e9b\u5b57\u7b26\u7684\u4e2a\u6570\u5f97\u5230\u5176\u4ed6\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97\u5b57\u7b26\u4e32\u5143\u7d20\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u662f\u5185\u7f6e\u51fd\u6570 len()\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002\u5bf9\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u904d\u5386\u3001collections \u6a21\u5757\u4e2d\u7684 Counter \u7c7b\u4ee5\u53ca str.count() \u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>collections.Counter<\/code>\u7c7b\u6765\u8f7b\u677e\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9Counter\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u5b57\u7b26\u4f5c\u4e3a\u952e\uff0c\u51fa\u73b0\u6b21\u6570\u4f5c\u4e3a\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nstring = &quot;hello world&quot;\nchar_count = Counter(string)\nprint(char_count)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u8f93\u51fa\u6bcf\u4e2a\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5305\u62ec\u7a7a\u683c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u7279\u5b9a\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684<code>count()<\/code>\u65b9\u6cd5\u6765\u8ba1\u7b97\u7279\u5b9a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u53ea\u9700\u5c06\u60f3\u8981\u8ba1\u6570\u7684\u5b57\u7b26\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8be5\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">string = &quot;hello world&quot;\ncount_a = string.count(&#39;o&#39;)\nprint(count_a)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u5b57\u7b26&#39;o&#39;\u5728\u5b57\u7b26\u4e32&quot;hello world&quot;\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u6570\u91cf\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u548c\u8ba1\u6570\u7279\u5b9a\u5b57\u7b26\u6216\u6a21\u5f0f\u7684\u51fa\u73b0\u6b21\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\nstring = &quot;hello world&quot;\nmatch = re.findall(&#39;o&#39;, string)\nprint(len(match))\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c06\u8fd4\u56de\u5b57\u7b26&#39;o&#39;\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u5339\u914d\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u8ba1\u7b97\u5b57\u7b26\u4e32\u5143\u7d20\u4e2a\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u5185\u7f6e\u51fd\u6570len()\u3001for\u5faa\u73af\u904d\u5386\u8ba1\u6570\u3001collections\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1105480,"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\/1105466"}],"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=1105466"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105466\/revisions"}],"predecessor-version":[{"id":1105482,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105466\/revisions\/1105482"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1105480"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1105466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1105466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1105466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}