{"id":1035920,"date":"2024-12-31T12:02:51","date_gmt":"2024-12-31T04:02:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035920.html"},"modified":"2024-12-31T12:02:54","modified_gmt":"2024-12-31T04:02:54","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%af%b9%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%ae%a1%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1035920.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5bf9\u5b57\u7b26\u4e32\u8ba1\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/55ff7e30-89ec-497d-a4be-aff7f60227e9.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u5bf9\u5b57\u7b26\u4e32\u8ba1\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u8ba1\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684count()\u65b9\u6cd5\u3001\u4f7f\u7528collections.Counter\u7c7b\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5\u53ca\u624b\u52a8\u904d\u5386\u5b57\u7b26\u4e32\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u8ba1\u6570\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5185\u7f6e\u7684count()\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684count()\u65b9\u6cd5\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u7edf\u8ba1\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.count(sub, start= 0,end=len(string))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>sub<\/code> \u662f\u8981\u8ba1\u6570\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>start<\/code> \u662f\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5f00\u59cb\u8ba1\u6570\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u503c\u4e3a0\u3002<\/li>\n<li><code>end<\/code> \u662f\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u7ed3\u675f\u8ba1\u6570\u7684\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u503c\u4e3a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<\/li>\n<\/ul>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684count()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>count()\u65b9\u6cd5\u662f\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u51fa\u73b0\u6b21\u6570\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002\u5b83\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u5bf9\u8c61\u4e0a\u8c03\u7528\uff0c\u5e76\u4f20\u5165\u8981\u8ba1\u6570\u7684\u5b50\u5b57\u7b26\u4e32\u5373\u53ef\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u7edf\u8ba1\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u6b21\u6570\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python is a great programming language. Python is popular.&quot;<\/p>\n<p>count = text.count(&quot;Python&quot;)<\/p>\n<p>print(f&quot;The word &#39;Python&#39; appears {count} times.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528collections.Counter\u7c7b<\/h3>\n<\/p>\n<p><p>collections\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2aCounter\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u8ba1\u6570\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u5143\u7d20\uff0c\u503c\u662f\u5143\u7d20\u7684\u8ba1\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>text = &quot;Python is a great programming language. Python is popular.&quot;<\/p>\n<p>counter = Counter(text)<\/p>\n<p>print(counter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regex\uff09\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u641c\u7d22\u529f\u80fd\u3002\u4f7f\u7528re\u6a21\u5757\u4e2d\u7684findall()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u7684\u5217\u8868\u3002\u7136\u540e\uff0c\u901a\u8fc7\u8ba1\u7b97\u5217\u8868\u7684\u957f\u5ea6\u5373\u53ef\u5f97\u51fa\u5b50\u5b57\u7b26\u4e32\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Python is a great programming language. Python is popular.&quot;<\/p>\n<p>matches = re.findall(r&quot;Python&quot;, text)<\/p>\n<p>count = len(matches)<\/p>\n<p>print(f&quot;The word &#39;Python&#39; appears {count} times.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u624b\u52a8\u904d\u5386\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u624b\u52a8\u904d\u5386\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u7075\u6d3b\u4f46\u8f83\u4e3a\u7e41\u7410\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u548c\u7d22\u5f15\u6765\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u6548\u7387\u4e0d\u9ad8\uff0c\u4f46\u9002\u5408\u9700\u8981\u81ea\u5b9a\u4e49\u590d\u6742\u8ba1\u6570\u903b\u8f91\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python is a great programming language. Python is popular.&quot;<\/p>\n<p>sub = &quot;Python&quot;<\/p>\n<p>count = 0<\/p>\n<p>start = 0<\/p>\n<p>while True:<\/p>\n<p>    start = text.find(sub, start) + 1<\/p>\n<p>    if start &gt; 0:<\/p>\n<p>        count += 1<\/p>\n<p>    else:<\/p>\n<p>        break<\/p>\n<p>print(f&quot;The word &#39;Python&#39; appears {count} times.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u7684count()\u65b9\u6cd5\u662f\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u51fa\u73b0\u6b21\u6570\u7684\u6700\u7b80\u5355\u65b9\u6cd5<\/strong>\u3002\u5b83\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u5bf9\u8c61\u4e0a\u8c03\u7528\uff0c\u5e76\u4f20\u5165\u8981\u8ba1\u6570\u7684\u5b50\u5b57\u7b26\u4e32\u5373\u53ef\u3002\u5bf9\u4e8e\u9700\u8981\u5feb\u901f\u7edf\u8ba1\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u5728\u6574\u4e2a\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u6b21\u6570\u7684\u60c5\u51b5\uff0ccount()\u65b9\u6cd5\u662f\u6700\u63a8\u8350\u7684\u3002<\/p>\n<\/p>\n<p><p><strong>collections.Counter\u7c7b\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd<\/strong>\uff0c\u9002\u5408\u5904\u7406\u66f4\u590d\u6742\u7684\u8ba1\u6570\u9700\u6c42\u3002\u4f8b\u5982\uff0cCounter\u7c7b\u53ef\u4ee5\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u800c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u3002<\/p>\n<\/p>\n<p><p><strong>\u624b\u52a8\u904d\u5386\u5b57\u7b26\u4e32\u867d\u7136\u7075\u6d3b\uff0c\u4f46\u8f83\u4e3a\u7e41\u7410<\/strong>\uff0c\u901a\u5e38\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u9664\u975e\u9700\u8981\u81ea\u5b9a\u4e49\u590d\u6742\u7684\u8ba1\u6570\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4f53\u6765\u8bf4\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u8ba1\u6570\u9700\u6c42\u548c\u5b57\u7b26\u4e32\u7684\u590d\u6742\u6027\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5185\u7f6e\u7684count()\u65b9\u6cd5\u5df2\u7ecf\u80fd\u591f\u6ee1\u8db3\u9700\u6c42\uff0c\u4f46\u5728\u9700\u8981\u66f4\u590d\u6742\u7684\u8ba1\u6570\u903b\u8f91\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528collections.Counter\u7c7b\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\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\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>Counter<\/code>\uff0c\u7136\u540e\u5c06\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9\u5b83\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\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>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u4e00\u4e2a\u5b57\u5178\uff0c\u663e\u793a\u6bcf\u4e2a\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5185\u7f6e\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u5b50\u5b57\u7b26\u4e32\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u786e\u5b9e\uff0cPython\u5b57\u7b26\u4e32\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>count<\/code>\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u76f4\u63a5\u8ba1\u7b97\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u5728\u539f\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">string = &quot;banana&quot;\ncount_ban = string.count(&quot;an&quot;)\nprint(count_ban)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de<code>2<\/code>\uff0c\u8868\u793a<code>&quot;an&quot;<\/code>\u5728<code>&quot;banana&quot;<\/code>\u4e2d\u51fa\u73b0\u4e86\u4e24\u6b21\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u5355\u8bcd\u8fdb\u884c\u8ba1\u6570\u800c\u4e0d\u4ec5\u4ec5\u662f\u5b57\u7b26\uff1f<\/strong><br \/>\u8981\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u8bcd\uff0c\u7136\u540e\u4f7f\u7528<code>Counter<\/code>\u6765\u7edf\u8ba1\u6bcf\u4e2a\u5355\u8bcd\u7684\u9891\u7387\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nstring = &quot;apple banana apple orange banana&quot;\nword_count = Counter(string.split())\nprint(word_count)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5c06\u4f1a\u663e\u793a\u6bcf\u4e2a\u5355\u8bcd\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5982<code>{&#39;apple&#39;: 2, &#39;banana&#39;: 2, &#39;orange&#39;: 1}<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u5904\u7406\u957f\u6587\u672c\u4e2d\u7684\u5355\u8bcd\u9891\u7387\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u8ba1\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684count()\u65b9\u6cd5\u3001\u4f7f\u7528collectio [&hellip;]","protected":false},"author":3,"featured_media":1035930,"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\/1035920"}],"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=1035920"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035920\/revisions"}],"predecessor-version":[{"id":1035931,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035920\/revisions\/1035931"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1035930"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1035920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1035920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1035920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}