{"id":3214,"date":"2025-06-03T16:02:36","date_gmt":"2025-06-04T08:34:35","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3214"},"modified":"2025-06-04T16:02:36","modified_gmt":"2025-06-04T08:34:35","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84collections%e6%a8%a1%e7%b5%84","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84collections%e6%a8%a1%e7%b5%84\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22Python\u4e2d\u7684Collections\u6a21\u7d44\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, collections, namedtuple, defaultdict, OrderedDict, Counter, deque, Python \u6559\u5b78\"><\/p>\n<p>\u5728Python\u4e2d\uff0ccollections\u6a21\u7d44\u63d0\u4f9b\u4e86\u591a\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u8655\u7406\u8cc7\u6599\u3002\u5728\u9019\u7bc7\u66f4\u65b0\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0ePython\u4e2d\u7684collections\u6a21\u7d44\u53ca\u5176\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b2025\u5e74\u7684\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662fPython\u7684collections\u6a21\u7d44\uff1f<\/h2>\n<p>collections\u6a21\u7d44\u5305\u542b\u4e00\u7cfb\u5217\u589e\u5f37\u7248\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u64cd\u4f5c\u8cc7\u6599\u3002\u5b83\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u5e7e\u7a2e\u4e3b\u8981\u8cc7\u6599\u7d50\u69cb\uff1a<strong>namedtuple<\/strong>\u3001<strong>defaultdict<\/strong>\u3001<strong>OrderedDict<\/strong>\u3001<strong>Counter<\/strong>\u548c<strong>deque<\/strong>\u3002\u9019\u4e9b\u8cc7\u6599\u7d50\u69cb\u4e0d\u50c5\u7528\u65bc\u7c21\u5316\u4ee3\u78bc\uff0c\u9084\u80fd\u63d0\u9ad8\u7a0b\u5f0f\u7684\u6548\u7387\u3002<\/p>\n<h2>namedtuple<\/h2>\n<p>namedtuple\u662f\u4e00\u7a2e\u7279\u5225\u7684tuple\uff0c\u5141\u8a31\u4f60\u4f7f\u7528\u540d\u7a31\u4f86\u8a2a\u554f\u5143\u7d20\u800c\u975e\u7d22\u5f15\uff0c\u589e\u52a0\u4ee3\u78bc\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p>&#8220;`python<br \/>\nfrom collections import namedtuple<\/p>\n<p>Point = namedtuple(&#8216;Point&#8217;, [&#8216;x&#8217;, &#8216;y&#8217;])<br \/>\np = Point(11, y=22)<\/p>\n<p>print(f&#8221;Point coordinates: x={p.x}, y={p.y}, Sum={p.x + p.y}&#8221;)<br \/>\n&#8220;`<\/p>\n<p>**\u5ef6\u4f38\u61c9\u7528**\uff1anamedtuple\u975e\u5e38\u9069\u5408\u7528\u65bc\u8868\u793a\u8cc7\u6599\u6a21\u578b\uff0c\u4f8b\u5982\u5728\u8cc7\u6599\u5eab\u4e2d\u5c0d\u61c9\u7684\u884c\u3002<\/p>\n<h2>defaultdict<\/h2>\n<p>defaultdict\u662f\u5b57\u5178\u7684\u4e00\u7a2e\u8b8a\u9ad4\uff0c\u5141\u8a31\u4f60\u5b9a\u7fa9\u4e00\u500b\u9ed8\u8a8d\u503c\uff0c\u7576\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u9375\u6642\uff0c\u8fd4\u56de\u8a72\u9ed8\u8a8d\u503c\u3002<\/p>\n<p>&#8220;`python<br \/>\nfrom collections import defaultdict<\/p>\n<p>d = defaultdict(int)<br \/>\nd[&#8216;a&#8217;] = 1<br \/>\nd[&#8216;b&#8217;] = 2<\/p>\n<p>print(f&#8221;Value of key &#8216;c&#8217;: {d[&#8216;c&#8217;]}&#8221;)  # \u8f38\u51fa 0<br \/>\n&#8220;`<\/p>\n<p>**\u932f\u8aa4\u6392\u9664**\uff1a\u5982\u679c\u4f60\u767c\u73fe\u6709\u9375\u8fd4\u56de\u7684\u503c\u4e0d\u5982\u9810\u671f\uff0c\u8acb\u78ba\u8a8d\u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\u4e86\u9ed8\u8a8d\u503c\u3002<\/p>\n<h2>OrderedDict<\/h2>\n<p>OrderedDict\u4fdd\u6301\u5143\u7d20\u7684\u63d2\u5165\u9806\u5e8f\uff0c\u9019\u5728\u9700\u8981\u4fdd\u6301\u8cc7\u6599\u9806\u5e8f\u7684\u61c9\u7528\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>&#8220;`python<br \/>\nfrom collections import OrderedDict<\/p>\n<p>d = OrderedDict()<br \/>\nd[&#8216;a&#8217;] = 1<br \/>\nd[&#8216;b&#8217;] = 2<br \/>\nd[&#8216;c&#8217;] = 3<\/p>\n<p>for key, value in d.items():<br \/>\n    print(key, value)<\/p>\n<p># \u8f38\u51fa\uff1a<br \/>\n# a 1<br \/>\n# b 2<br \/>\n# c 3<br \/>\n&#8220;`<\/p>\n<p>**\u61c9\u7528\u5834\u666f**\uff1a\u5728\u9700\u8981\u6709\u5e8f\u8cc7\u6599\u5448\u73fe\u7684\u60c5\u6cc1\u4e0b\uff0c\u4f8b\u5982\u5e8f\u5217\u5316\u6216\u8655\u7406\u914d\u7f6e\u9805\u6642\uff0cOrderedDict\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2>Counter<\/h2>\n<p>Counter\u5c08\u9580\u7528\u65bc\u8a08\u7b97\u5143\u7d20\u7684\u51fa\u73fe\u6b21\u6578\uff0c\u9069\u5408\u7528\u65bc\u7d71\u8a08\u548c\u5206\u6790\u8cc7\u6599\u3002<\/p>\n<p>&#8220;`python<br \/>\nfrom collections import Counter<\/p>\n<p>c = Counter([1, 2, 3, 1, 2, 1, 2, 3])<\/p>\n<p>print(f&#8221;Element counts: {c}&#8221;)  # \u8f38\u51fa Counter({1: 3, 2: 3, 3: 2})<br \/>\n&#8220;`<\/p>\n<p>**\u6700\u4f73\u5be6\u8e10**\uff1a\u4f7f\u7528Counter\u9032\u884c\u8cc7\u6599\u5206\u6790\u6642\uff0c\u8003\u616e\u5c07\u7d50\u679c\u8f49\u63db\u70ba\u5b57\u5178\u6216\u5176\u4ed6\u683c\u5f0f\u4f86\u9032\u884c\u9032\u4e00\u6b65\u8655\u7406\u3002<\/p>\n<h2>deque<\/h2>\n<p>deque\uff08\u96d9\u7aef\u968a\u5217\uff09\u662f\u9ad8\u6548\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5141\u8a31\u4f60\u5728\u968a\u5217\u7684\u5169\u7aef\u5feb\u901f\u5730\u6dfb\u52a0\u6216\u522a\u9664\u5143\u7d20\u3002<\/p>\n<p>&#8220;`python<br \/>\nfrom collections import deque<\/p>\n<p>d = deque([1, 2, 3])<br \/>\nd.append(4)<br \/>\nd.appendleft(0)<\/p>\n<p>print(f&#8221;Deque contents: {d}&#8221;)  # \u8f38\u51fa deque([0, 1, 2, 3, 4])<br \/>\n&#8220;`<\/p>\n<p>**\u64cd\u4f5c\u63d0\u793a**\uff1a\u4f7f\u7528deque\u9032\u884c\u5927\u898f\u6a21\u8cc7\u6599\u7684\u64cd\u4f5c\u6642\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u983b\u7e41\u6dfb\u52a0\u6216\u522a\u9664\u5143\u7d20\u7684\u60c5\u6cc1\u4e0b\u3002<\/p>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u91cd\u65b0\u63a2\u8a0e\u4e86Python\u7684collections\u6a21\u7d44\uff0c\u4e26\u4ecb\u7d39\u4e86namedtuple\u3001defaultdict\u3001OrderedDict\u3001Counter\u548cdeque\u7684\u6700\u65b0\u7528\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\u3002\u9019\u4e9b\u5de5\u5177\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u4f60\u7684\u958b\u767c\u6548\u7387\u8207\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u3002<\/p>\n<p>\u6b32\u4e86\u89e3\u66f4\u591aPython\u7684\u5be6\u7528\u6280\u5de7\u8207\u6559\u5b78\uff0c\u8acb\u53c3\u8003\u6211\u5011\u7684[Python\u6559\u80b2\u8cc7\u6e90](https:\/\/vocus.cc)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: collections\u6a21\u7d44\u7684\u4e3b\u8981\u512a\u52e2\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: collections\u6a21\u7d44\u63d0\u4f9b\u4e86\u591a\u7a2e\u5c08\u9580\u8a2d\u8a08\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u7c21\u5316\u4ee3\u78bc\uff0c\u63d0\u9ad8\u7a0b\u5f0f\u6027\u80fd\uff0c\u4e26\u6539\u5584\u8cc7\u6599\u64cd\u4f5c\u7684\u9748\u6d3b\u6027\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u9078\u64c7\u9069\u5408\u7684collections\u8cc7\u6599\u7d50\u69cb\uff1f**<br \/>\nA2: \u9078\u64c7\u8cc7\u6599\u7d50\u69cb\u6642\uff0c\u8003\u616e\u4f60\u7684\u9700\u6c42\uff0c\u4f8b\u5982\u662f\u5426\u9700\u8981\u4fdd\u6301\u5143\u7d20\u9806\u5e8f\u3001\u8a08\u7b97\u983b\u7387\u6216\u5feb\u901f\u64cd\u4f5c\u7b49\u3002<\/p>\n<p>**Q3: collections\u6a21\u7d44\u7684\u4f7f\u7528\u60c5\u5883\u6709\u54ea\u4e9b\uff1f**<br \/>\nA3: collections\u6a21\u7d44\u53ef\u7528\u65bc\u6578\u64da\u5206\u6790\u3001\u8cc7\u6599\u5eab\u6a21\u578b\u3001\u914d\u7f6e\u7ba1\u7406\u7b49\u591a\u7a2e\u60c5\u5883\uff0c\u63d0\u5347\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u7dad\u8b77\u6027\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684Collections\u6a21\u7d44\uff0c\u5b83\u63d0\u4f9b\u4e86\u8a31\u591a\u6709\u7528\u7684\u5bb9\u5668\u6578\u64da\u985e\u578b\uff0c\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002\u672c\u6587\u5c07\u4ecb\u7d39Collections\u6a21\u7d44\u7684\u57fa\u672c\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\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-3214","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-PQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3214","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=3214"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3214\/revisions"}],"predecessor-version":[{"id":3215,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3214\/revisions\/3215"}],"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=3214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}