{"id":3236,"date":"2025-06-02T15:42:50","date_gmt":"2025-06-04T08:29:50","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3236"},"modified":"2025-06-04T15:42:50","modified_gmt":"2025-06-04T08:29:50","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84counter%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84counter%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3Python\u4e2dCounter()\u51fd\u6578\uff1a\u529f\u80fd\u8207\u5be6\u4f5c\u7bc4\u4f8b\uff082025\u6700\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, Counter(), Python \u6559\u5b78, \u6578\u64da\u8a08\u7b97\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3Python\u4e2dCounter()\u51fd\u6578\uff1a\u529f\u80fd\u8207\u5be6\u4f5c\u7bc4\u4f8b\uff082025\u6700\u65b0\uff09<\/h1>\n<p>Python\u7684Counter()\u51fd\u5f0f\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u53ef\u8fed\u4ee3\u7269\u4ef6\u4e2d\u6bcf\u500b\u5143\u7d20\u51fa\u73fe\u7684\u6b21\u6578\u3002\u9019\u500b\u529f\u80fd\u8b93\u6211\u5011\u80fd\u5920\u5feb\u901f\u7372\u53d6\u6578\u64da\u7684\u983b\u7387\u5206\u4f48\uff0c\u7121\u9700\u624b\u52d5\u8a08\u7b97\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39Counter()\u7684\u57fa\u672c\u7528\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u4ee5\u53ca\u4e00\u4e9b\u9032\u968e\u61c9\u7528\u3002<\/p>\n<p>### Counter()\u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<p>Counter()\u51fd\u5f0f\u4f86\u81ea`collections`\u6a21\u7d44\uff0c\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u53ef\u8fed\u4ee3\u7269\u4ef6\uff0c\u4f8b\u5982\u5b57\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002\u5b83\u5c07\u53ef\u8fed\u4ee3\u7269\u4ef6\u7684\u5143\u7d20\u8f49\u63db\u70bakey-value\u7684\u5f62\u5f0f\uff0ckey\u70ba\u5143\u7d20\uff0cvalue\u70ba\u5143\u7d20\u51fa\u73fe\u7684\u6b21\u6578\u3002<\/p>\n<p>### \u4f7f\u7528\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u5217\u8868\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]\n&#8220;`<\/p>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528Counter()\u51fd\u5f0f\u4f86\u8a08\u7b97\u6bcf\u500b\u5143\u7d20\u7684\u51fa\u73fe\u6b21\u6578\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom collections import Counter<\/p>\n<p>my_counter = Counter(my_list)<\/p>\n<p>print(my_counter)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<p>&#8220;`<br \/>\nCounter({1: 2, 2: 2, 3: 2, 4: 2, 5: 2})<br \/>\n&#8220;`<\/p>\n<p>\u5f9e\u9019\u500b\u7d50\u679c\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230Counter()\u5c07\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f49\u63db\u70bakey-value\u7684\u5f62\u5f0f\uff0c\u4e26\u986f\u793a\u6bcf\u500b\u5143\u7d20\u7684\u51fa\u73fe\u6b21\u6578\u3002<\/p>\n<p>### \u8a08\u7b97\u5169\u500b\u53ef\u8fed\u4ee3\u7269\u4ef6\u7684\u5143\u7d20\u983b\u7387<\/p>\n<p>Counter()\u9084\u53ef\u4ee5\u8a08\u7b97\u5169\u500b\u53ef\u8fed\u4ee3\u7269\u4ef6\u4e2d\u5143\u7d20\u7684\u51fa\u73fe\u6b21\u6578\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nlist1 = [1, 2, 3, 4, 5]\nlist2 = [2, 4, 6, 8, 10]\n<p>counter1 = Counter(list1)<br \/>\ncounter2 = Counter(list2)<\/p>\n<p>counter3 = counter1 + counter2<\/p>\n<p>print(counter3)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<p>&#8220;`<br \/>\nCounter({2: 2, 4: 2, 1: 1, 3: 1, 5: 1, 6: 1, 8: 1, 10: 1})<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u8a08\u7b97\u5169\u500b\u5217\u8868\u4e2d\u5143\u7d20\u7684\u51fa\u73fe\u6b21\u6578\uff0c\u4e26\u5c07\u7d50\u679c\u4ee5key-value\u7684\u5f62\u5f0f\u8f38\u51fa\u3002<\/p>\n<p>### \u5e38\u898b\u932f\u8aa4\u8207\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528Counter()\u6642\uff0c\u6709\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\u53ef\u80fd\u6703\u5f71\u97ff\u7d50\u679c\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u50b3\u5165\u975e\u53ef\u8fed\u4ee3\u7269\u4ef6**\uff1a\u78ba\u4fdd\u50b3\u5165\u7684\u7269\u4ef6\u662f\u53ef\u8fed\u4ee3\u7684\uff0c\u5426\u5247\u6703\u5831\u932f\u3002<br \/>\n&#8211; **\u8a08\u7b97\u7a7a\u5217\u8868**\uff1aCounter\u5c0d\u65bc\u7a7a\u5217\u8868\u5c07\u8fd4\u56de`Counter()`\uff0c\u9019\u662f\u6b63\u5e38\u7684\u884c\u70ba\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>Counter()\u51fd\u5f0f\u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u6578\u64da\u5206\u6790\uff0c\u9084\u53ef\u4ee5\u61c9\u7528\u65bc\u6587\u672c\u8655\u7406\u3001\u7d71\u8a08\u5b78\u3001\u6a5f\u5668\u5b78\u7fd2\u7b49\u9818\u57df\u3002\u5c0d\u65bc\u9032\u968e\u4f7f\u7528\u8005\uff0c\u60a8\u53ef\u4ee5\u8003\u616e\u5c07Counter\u8207\u5176\u4ed6Python\u6578\u64da\u8655\u7406\u5eab\uff08\u5982Pandas\uff09\u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u7372\u5f97\u66f4\u5f37\u5927\u7684\u6578\u64da\u5206\u6790\u80fd\u529b\u3002<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bcPython\u7684\u6559\u5b78\uff0c\u8acb\u53c3\u8003[\u9019\u88e1\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc)\uff0c\u9019\u5c07\u5e6b\u52a9\u60a8\u6df1\u5165\u5b78\u7fd2Python\u8a9e\u8a00\u7684\u66f4\u591a\u7279\u6027\u3002<\/p>\n<p>### \u7d50\u8ad6<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython\u7684Counter()\u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5feb\u901f\u8a08\u7b97\u53ef\u8fed\u4ee3\u7269\u4ef6\u4e2d\u6bcf\u500b\u5143\u7d20\u7684\u51fa\u73fe\u6b21\u6578\uff0c\u4e26\u4e14\u652f\u6301\u591a\u7a2e\u9032\u968e\u61c9\u7528\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Counter()\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: Counter()\u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u54ea\u4e9b\u985e\u578b\u7684\u53ef\u8fed\u4ee3\u7269\u4ef6\uff1f**<br \/>\n**A1:** Counter()\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u53ef\u8fed\u4ee3\u7269\u4ef6\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5c07Counter()\u7684\u7d50\u679c\u8f49\u63db\u70ba\u5b57\u5178\u683c\u5f0f\uff1f**<br \/>\n**A2:** \u60a8\u53ef\u4ee5\u4f7f\u7528`dict()`\u51fd\u5f0f\u5c07Counter\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u5178\uff0c\u4f8b\u5982`dict(my_counter)`\u3002<\/p>\n<p>**Q3: Counter()\u662f\u5426\u652f\u6301\u8a08\u7b97\u6d6e\u9ede\u6578\u7684\u51fa\u73fe\u6b21\u6578\uff1f**<br \/>\n**A3:** \u662f\u7684\uff0cCounter()\u53ef\u4ee5\u8a08\u7b97\u6d6e\u9ede\u6578\u7684\u51fa\u73fe\u6b21\u6578\uff0c\u8207\u6574\u6578\u4f7f\u7528\u65b9\u6cd5\u76f8\u540c\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Counter()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u5167\u5efa\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u53ef\u8fed\u4ee3\u5c0d\u8c61\u4e2d\u5143\u7d20\u51fa\u73fe\u7684\u6b21\u6578\uff0c\u672c\u6587\u5c07\u4ecb\u7d39Counter()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3236","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Qc","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3236"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3236\/revisions"}],"predecessor-version":[{"id":3237,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3236\/revisions\/3237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}