{"id":929696,"date":"2024-12-26T17:00:50","date_gmt":"2024-12-26T09:00:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/929696.html"},"modified":"2024-12-26T17:00:53","modified_gmt":"2024-12-26T09:00:53","slug":"python%e5%a6%82%e4%bd%95%e7%ae%97%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/929696.html","title":{"rendered":"python\u5982\u4f55\u7b97\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064811\/0cd30aad-63f0-4ed8-ae9f-82bfe6a38d79.webp\" alt=\"python\u5982\u4f55\u7b97\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8ba1\u7b97\u4e2a\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570len()\u3001\u4f7f\u7528\u5217\u8868\u7684count()\u65b9\u6cd5\u3001\u4f7f\u7528\u5b57\u5178\u7684get()\u65b9\u6cd5\u3001\u901a\u8fc7\u5faa\u73af\u8ba1\u6570\u7b49\u3002len()\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5217\u8868\u3001\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u957f\u5ea6\uff1bcount()\u65b9\u6cd5\u4e13\u7528\u4e8e\u5217\u8868\uff0c\u7528\u4e8e\u8ba1\u7b97\u7279\u5b9a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff1b\u5b57\u5178\u7684get()\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u7684\u9891\u7387\uff1b\u5faa\u73af\u8ba1\u6570\u5219\u662f\u4e00\u79cd\u901a\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u81ea\u5b9a\u4e49\u6761\u4ef6\u8fdb\u884c\u8ba1\u6570\u3002<\/strong>\u5176\u4e2d\uff0clen()\u51fd\u6570\u662f\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u76f4\u63a5\u8fd4\u56de\u5bf9\u8c61\u7684\u957f\u5ea6\uff0c\u65e0\u9700\u989d\u5916\u7684\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570len()<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0clen()\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u957f\u5ea6\u3002\u65e0\u8bba\u662f\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u8fd8\u662f\u5176\u4ed6\u5bb9\u5668\u7c7b\u578b\uff0clen()\u90fd\u53ef\u4ee5\u5feb\u901f\u5730\u8fd4\u56de\u5176\u5143\u7d20\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528len()\u51fd\u6570\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u7b80\u5355\u6027\u548c\u9ad8\u6548\u6027\u3002\u5b83\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u76f4\u63a5\u5bf9\u5bf9\u8c61\u7684\u957f\u5ea6\u8fdb\u884c\u8bbf\u95ee\uff0c\u800c\u65e0\u9700\u904d\u5386\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5217\u8868\u800c\u8a00\uff0clen()\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(1)\uff0c\u56e0\u4e3aPython\u5728\u5185\u90e8\u7ef4\u62a4\u4e86\u5217\u8868\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>print(len(my_list))  # \u8f93\u51fa\uff1a5<\/p>\n<p>my_string = &quot;Hello, World!&quot;<\/p>\n<p>print(len(my_string))  # \u8f93\u51fa\uff1a13<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u7684count()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>count()\u65b9\u6cd5\u662f\u5217\u8868\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u7edf\u8ba1\u67d0\u4e2a\u7279\u5b9a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4e0elen()\u4e0d\u540c\uff0ccount()\u65b9\u6cd5\u9700\u8981\u904d\u5386\u6574\u4e2a\u5217\u8868\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u5176\u4e2dn\u662f\u5217\u8868\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>count()\u65b9\u6cd5\u5728\u9700\u8981\u7edf\u8ba1\u7279\u5b9a\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5982\u679c\u5217\u8868\u975e\u5e38\u5927\uff0c\u4e14\u9700\u8981\u591a\u6b21\u7edf\u8ba1\u4e0d\u540c\u5143\u7d20\u7684\u6b21\u6570\uff0c\u5219\u53ef\u80fd\u9700\u8981\u8003\u8651\u6027\u80fd\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>my_list = [1, 2, 3, 2, 4, 2]<\/p>\n<p>print(my_list.count(2))  # \u8f93\u51fa\uff1a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u5728\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u7ef4\u62a4\u4e00\u4e2a\u9891\u7387\u8868\uff0c\u5176\u4e2d\u952e\u662f\u5143\u7d20\uff0c\u503c\u662f\u8be5\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u540c\u65f6\u8bb0\u5f55\u591a\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u4e14\u67e5\u627e\u548c\u66f4\u65b0\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5e73\u5747\u4e3aO(1)\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;]<\/p>\n<p>count_dict = {}<\/p>\n<p>for item in my_list:<\/p>\n<p>    if item in count_dict:<\/p>\n<p>        count_dict[item] += 1<\/p>\n<p>    else:<\/p>\n<p>        count_dict[item] = 1<\/p>\n<p>print(count_dict)  # \u8f93\u51fa\uff1a{&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;orange&#39;: 1}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u901a\u8fc7\u5faa\u73af\u8ba1\u6570<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u7279\u5b9a\u7684\u6761\u4ef6\u6765\u8ba1\u6570\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u624b\u52a8\u5b9e\u73b0\u8ba1\u6570\u3002\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6216while\u5faa\u73af\uff0c\u6839\u636e\u7279\u5b9a\u7684\u6761\u4ef6\u5224\u65ad\u662f\u5426\u589e\u52a0\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u8f83\u4e3a\u7075\u6d3b\uff0c\u4f46\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u6ce8\u610f\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<p>count = 0<\/p>\n<p>for item in my_list:<\/p>\n<p>    if item &gt; 25:<\/p>\n<p>        count += 1<\/p>\n<p>print(count)  # \u8f93\u51fa\uff1a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Counter\u7c7b<\/p>\n<\/p>\n<p><p>Python\u7684collections\u6a21\u5757\u63d0\u4f9b\u4e86Counter\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u8ba1\u6570\u64cd\u4f5c\u3002Counter\u7c7b\u662f\u5b57\u5178\u7684\u5b50\u7c7b\uff0c\u7528\u4e8e\u8ddf\u8e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5b83\u7684\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982most_common()\uff0c\u7528\u4e8e\u83b7\u53d6\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>Counter\u7c7b\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7b80\u5316\u4e86\u8ba1\u6570\u64cd\u4f5c\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>from collections import Counter<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;]<\/p>\n<p>counter = Counter(my_list)<\/p>\n<p>print(counter)  # \u8f93\u51fa\uff1aCounter({&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;orange&#39;: 1})<\/p>\n<p>print(counter.most_common(1))  # \u8f93\u51fa\uff1a[(&#39;apple&#39;, 3)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u8ba1\u6570\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u3002\u5982\u679c\u53ea\u9700\u8981\u7b80\u5355\u5730\u83b7\u53d6\u957f\u5ea6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528len()\u3002\u5982\u679c\u9700\u8981\u7edf\u8ba1\u7279\u5b9a\u5143\u7d20\u7684\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528count()\u6216Counter\u7c7b\u3002\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u8ba1\u6570\u6761\u4ef6\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8ba1\u6570\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u9891\u7387\u7edf\u8ba1\uff0c\u5efa\u8bae\u4f7f\u7528\u5b57\u5178\u6216Counter\u7c7b\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5728Python\u4e2d\u7edf\u8ba1\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>len()<\/code>\u51fd\u6570\u6765\u7edf\u8ba1\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4, 5]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>len(my_list)<\/code>\u6765\u83b7\u53d6\u5217\u8868\u7684\u957f\u5ea6\uff0c\u5373\u5143\u7d20\u4e2a\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u4efb\u4f55\u7c7b\u578b\u7684\u5e8f\u5217\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\u7b49\u3002<\/p>\n<p><strong>2. Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u8981\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u67d0\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>count()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;hello world&quot;<\/code>\uff0c\u8981\u7edf\u8ba1\u5b57\u7b26&#39;l&#39;\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_string.count(&#39;l&#39;)<\/code>\uff0c\u7ed3\u679c\u5c06\u8fd4\u56de2\u3002\u6b64\u5916\uff0c\u82e5\u9700\u8981\u8ba1\u7b97\u6240\u6709\u5b57\u7b26\u7684\u9891\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.Counter<\/code>\u6765\u751f\u6210\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8ba1\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>collections.Counter<\/code>\u7c7b\u6765\u5bf9\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8ba1\u6570\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;a&#39;: 3}<\/code>\uff0c\u4f7f\u7528<code>Counter(my_dict)<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u8ba1\u6570\u5668\u5bf9\u8c61\uff0c\u663e\u793a\u6bcf\u4e2a\u952e\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5c24\u5176\u9ad8\u6548\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8ba1\u7b97\u4e2a\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570len()\u3001\u4f7f\u7528\u5217\u8868\u7684count()\u65b9\u6cd5\u3001\u4f7f\u7528\u5b57\u5178\u7684get()\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":929700,"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\/929696"}],"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=929696"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/929696\/revisions"}],"predecessor-version":[{"id":929704,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/929696\/revisions\/929704"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/929700"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=929696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=929696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=929696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}