{"id":3410,"date":"2025-06-01T15:51:31","date_gmt":"2025-06-04T07:55:31","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3410"},"modified":"2025-06-04T15:51:31","modified_gmt":"2025-06-04T07:55:31","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84collections%e6%a8%a1%e7%b5%84-2","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-2\/","title":{"rendered":"\u5168\u9762\u4e86\u89e3 Python Collections \u6a21\u7d44\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, collections, Python \u51fd\u5f0f\u5eab, \u7a0b\u5f0f\u6559\u5b78, collections \u6a21\u7d44\"><\/p>\n<h1>\u5168\u9762\u4e86\u89e3 Python Collections \u6a21\u7d44<\/h1>\n<p>Python Collections \u6a21\u7d44\u662f Python \u6a19\u6e96\u5eab\u4e2d\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u6a21\u7d44\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u4f7f\u7528\u7684\u8cc7\u6599\u7d50\u69cb\u3002\u9019\u4e9b\u8cc7\u6599\u7d50\u69cb\u5305\u62ec\u4f46\u4e0d\u9650\u65bc\uff1a<strong>namedtuple<\/strong>\u3001<strong>deque<\/strong>\u3001<strong>defaultdict<\/strong>\u3001<strong>OrderedDict<\/strong>\u3001<strong>Counter<\/strong> \u7b49\u7b49\uff0c\u9019\u4e9b\u5bb9\u5668\u985e\u578b\u8b93\u958b\u767c\u8005\u80fd\u5920\u66f4\u9748\u6d3b\u5730\u64cd\u4f5c\u8cc7\u6599\u3002<\/p>\n<p><!--more--><\/p>\n<h2>1. namedtuple<\/h2>\n<p>`namedtuple` \u662f\u4e00\u7a2e\u7c21\u5316\u7684\u5143\u7d44\uff0c\u5141\u8a31\u958b\u767c\u8005\u4f7f\u7528\u540d\u7a31\u800c\u975e\u7d22\u5f15\u4f86\u8a2a\u554f\u8cc7\u6599\uff0c\u9019\u4f7f\u5f97\u4ee3\u78bc\u66f4\u5177\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(p.x + p.y)  # Output: 33<br \/>\n&#8220;`<\/p>\n<p>**\u5ef6\u4f38\u61c9\u7528**\uff1a`namedtuple` \u53ef\u4ee5\u7528\u4f86\u66ff\u4ee3\u50b3\u7d71\u7684\u985e\u5225\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u8f15\u91cf\u7d1a\u7269\u4ef6\u6642\u3002<\/p>\n<h2>2. deque<\/h2>\n<p>`deque` \u662f\u96d9\u7aef\u4f47\u5217\uff0c\u5141\u8a31\u5728\u5169\u7aef\u5feb\u901f\u5730\u6dfb\u52a0\u6216\u522a\u9664\u5143\u7d20\uff0c\u7279\u5225\u9069\u5408\u7528\u65bc\u9700\u8981\u983b\u7e41\u63d2\u5165\u6216\u522a\u9664\u7684\u5834\u666f\u3002<\/p>\n<p>&#8220;`python<br \/>\nfrom collections import deque<\/p>\n<p>d = deque()<br \/>\nd.append(&#8216;a&#8217;)<br \/>\nd.append(&#8216;b&#8217;)<br \/>\nd.append(&#8216;c&#8217;)<br \/>\nd.appendleft(&#8216;z&#8217;)  # \u5728\u5de6\u5074\u6dfb\u52a0\u5143\u7d20<\/p>\n<p>print(d)  # Output: deque([&#8216;z&#8217;, &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;])<br \/>\n&#8220;`<\/p>\n<p>**\u932f\u8aa4\u6392\u9664**\uff1a\u5982\u679c\u4f60\u5728\u4f7f\u7528 `deque` \u6642\u9047\u5230 IndexError\uff0c\u8acb\u6aa2\u67e5\u662f\u5426\u5728\u7a7a\u4f47\u5217\u4e0a\u9032\u884c pop \u64cd\u4f5c\u3002<\/p>\n<h2>3. defaultdict<\/h2>\n<p>`defaultdict` \u662f\u4e00\u7a2e\u64f4\u5c55\u7684\u5b57\u5178\uff0c\u5141\u8a31\u4f60\u5728\u67e5\u627e\u4e0d\u5b58\u5728\u7684\u9375\u6642\u81ea\u52d5\u521d\u59cb\u5316\u70ba\u6307\u5b9a\u7684\u503c\uff0c\u907f\u514d\u4e86 KeyError \u7684\u554f\u984c\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(d[&#8216;c&#8217;])  # Output: 0<br \/>\n&#8220;`<\/p>\n<p>**\u6700\u4f73\u5be6\u8e10**\uff1a\u4f7f\u7528 `defaultdict` \u53ef\u4ee5\u7c21\u5316\u8a08\u6578\u6216\u7d2f\u52a0\u64cd\u4f5c\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5b57\u5178\u6642\u3002<\/p>\n<h2>4. OrderedDict<\/h2>\n<p>`OrderedDict` \u4fdd\u7559\u4e86\u5143\u7d20\u7684\u63d2\u5165\u9806\u5e8f\uff0c\u9019\u5728\u9700\u8981\u6709\u5e8f\u8cc7\u6599\u7684\u60c5\u6cc1\u4e0b\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># Output:<br \/>\n# a 1<br \/>\n# b 2<br \/>\n# c 3<br \/>\n&#8220;`<\/p>\n<p>**\u5ef6\u4f38\u61c9\u7528**\uff1a`OrderedDict` \u53ef\u4ee5\u7528\u65bc\u9700\u8981\u4fdd\u6301\u8cc7\u6599\u9806\u5e8f\u7684\u60c5\u6cc1\uff0c\u4f8b\u5982\u5728\u8655\u7406\u914d\u7f6e\u6216\u53c3\u6578\u6642\u3002<\/p>\n<h2>5. Counter<\/h2>\n<p>`Counter` \u662f\u5c08\u9580\u7528\u4f86\u8a08\u7b97\u53ef\u54c8\u5e0c\u7269\u4ef6\u7684\u6578\u91cf\u7684\u5b57\u5178\u5b50\u985e\u5225\uff0c\u975e\u5e38\u9069\u5408\u7528\u65bc\u7d71\u8a08\u548c\u8a08\u6578\u3002<\/p>\n<p>&#8220;`python<br \/>\nfrom collections import Counter<\/p>\n<p>c = Counter()<br \/>\nfor word in [&#8216;red&#8217;, &#8216;blue&#8217;, &#8216;red&#8217;, &#8216;green&#8217;, &#8216;blue&#8217;, &#8216;blue&#8217;]:<br \/>\n    c[word] += 1<\/p>\n<p>print(c)  # Output: Counter({&#8216;blue&#8217;: 3, &#8216;red&#8217;: 2, &#8216;green&#8217;: 1})<br \/>\n&#8220;`<\/p>\n<p>**\u6700\u4f73\u5be6\u8e10**\uff1a\u4f7f\u7528 `Counter` \u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u9032\u884c\u8a5e\u983b\u7d71\u8a08\uff0c\u7279\u5225\u662f\u5728\u6587\u5b57\u8655\u7406\u548c\u5206\u6790\u9818\u57df\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Python Collections \u6a21\u7d44\u63d0\u4f9b\u4e86\u591a\u7a2e\u9ad8\u6548\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u9019\u4e9b\u5de5\u5177\u4f7f\u5f97\u958b\u767c\u8005\u5728\u8655\u7406\u8cc7\u6599\u6642\u66f4\u52a0\u9748\u6d3b\u8207\u65b9\u4fbf\u3002\u82e5\u60f3\u6df1\u5165\u4e86\u89e3 Python Collections \u6a21\u7d44\u7684\u66f4\u591a\u61c9\u7528\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78](https:\/\/vocus.cc) \u4ee5\u7372\u53d6\u66f4\u591a\u5be6\u7528\u7684\u7bc4\u4f8b\u8207\u6280\u5de7\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u70ba\u4ec0\u9ebc\u8981\u4f7f\u7528 `namedtuple` \u800c\u4e0d\u662f\u666e\u901a\u7684\u5143\u7d44\uff1f**<br \/>\nA1: `namedtuple` \u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u53ef\u8b80\u6027\uff0c\u8b93\u4f60\u7684\u4ee3\u78bc\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u7279\u5225\u662f\u7576\u5143\u7d44\u4e2d\u5305\u542b\u591a\u500b\u5143\u7d20\u6642\u3002<\/p>\n<p>**Q2: `deque` \u548c\u666e\u901a\u7684\u5217\u8868\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA2: `deque` \u5728\u5169\u7aef\u7684\u63d2\u5165\u548c\u522a\u9664\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(1)\uff0c\u800c\u5217\u8868\u5728\u9019\u4e9b\u64cd\u4f5c\u4e0a\u5247\u662f O(n)\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u5728 `defaultdict` \u4e2d\u4f7f\u7528\u5176\u4ed6\u8cc7\u6599\u985e\u578b\uff1f**<br \/>\nA3: \u4f60\u53ef\u4ee5\u5728\u5275\u5efa `defaultdict` \u6642\u50b3\u5165\u4efb\u4f55\u53ef\u547c\u53eb\u7684\u5c0d\u8c61\uff0c\u6bd4\u5982 `list`\u3001`set` \u7b49\uff0c\u4ee5\u4fbf\u5728\u67e5\u627e\u4e0d\u5b58\u5728\u7684\u9375\u6642\u8fd4\u56de\u76f8\u61c9\u7684\u985e\u578b\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\u6982\u5ff5\uff0c\u4ee5\u53ca\u5b83\u7684\u4e3b\u8981\u529f\u80fd\u548c\u7528\u6cd5\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-3410","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-T0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3410","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=3410"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3410\/revisions"}],"predecessor-version":[{"id":3411,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3410\/revisions\/3411"}],"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=3410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}