{"id":3366,"date":"2025-06-03T16:00:36","date_gmt":"2025-06-04T08:06:36","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3366"},"modified":"2025-06-04T16:00:36","modified_gmt":"2025-06-04T08:06:36","slug":"%e4%ba%86%e8%a7%a3python%e5%ad%97%e5%85%b8dict%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-3","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e5%ad%97%e5%85%b8dict%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-3\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u5b57\u5178 (dict) \u7684\u6700\u65b0\u7528\u6cd5\u8207\u5be6\u52d9\u61c9\u7528"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, dict(), \u5b57\u5178, Python \u6559\u5b78, Python \u5be6\u4f5c\"><\/p>\n<h1>Python dict() \u7684\u4f7f\u7528\u65b9\u6cd5\uff082025 \u6700\u65b0\u7248\uff09<\/h1>\n<p>Python \u7684 `dict()` \u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u4ee5\u9375\u503c\u5c0d\u7684\u65b9\u5f0f\u4f86\u5132\u5b58\u548c\u67e5\u627e\u8cc7\u6599\uff0c\u9019\u6a23\u7684\u8cc7\u6599\u7d50\u69cb\u5728 Python \u4e2d\u6975\u70ba\u91cd\u8981\u3002\u672c\u6587\u5c07\u4ecb\u7d39 `dict()` \u7684\u57fa\u672c\u7528\u6cd5\u3001\u9032\u968e\u61c9\u7528\u53ca\u5176\u5728\u5be6\u52d9\u4e2d\u7684\u61c9\u7528\u6848\u4f8b\uff0c\u4e26\u63d0\u4f9b\u932f\u8aa4\u6392\u9664\u7684\u5efa\u8b70\u3002<\/p>\n<h2>dict() \u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`dict()` \u51fd\u5f0f\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5c07\u4e00\u500b\u53ef\u8fed\u4ee3\u7684\u7269\u4ef6\uff08\u5982\u5143\u7d44\u6216\u5217\u8868\uff09\u8f49\u63db\u6210\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u5e7e\u500b\u57fa\u672c\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07\u5143\u7d44\u8f49\u63db\u6210\u5b57\u5178<br \/>\nmy_dict = dict(((&#8216;a&#8217;, 1), (&#8216;b&#8217;, 2), (&#8216;c&#8217;, 3)))<\/p>\n<p># \u5c07\u5217\u8868\u8f49\u63db\u6210\u5b57\u5178<br \/>\nmy_dict = dict([[&#8216;a&#8217;, 1], [&#8216;b&#8217;, 2], [&#8216;c&#8217;, 3]])<br \/>\n&#8220;`<\/p>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u5c07\u5efa\u7acb\u4e00\u500b\u5b57\u5178\uff0c\u5176\u4e2d\u5305\u542b\u4e09\u500b\u9375\u503c\u5c0d\uff1a`a:1`, `b:2`, `c:3`\u3002<\/p>\n<h2>dict() \u7684\u9032\u968e\u7528\u6cd5<\/h2>\n<p>`dict()` \u51fd\u5f0f\u9084\u53ef\u4ee5\u63a5\u53d7\u5169\u500b\u53c3\u6578\uff0c\u7b2c\u4e00\u500b\u53c3\u6578\u662f\u53ef\u8fed\u4ee3\u7684\u7269\u4ef6\uff0c\u7b2c\u4e8c\u500b\u53c3\u6578\u662f\u4e00\u500b\u6620\u5c04\u51fd\u5f0f\uff0c\u9019\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u9748\u6d3b\u5730\u8f49\u63db\u8cc7\u6599\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07\u5143\u7d44\u8f49\u63db\u6210\u5b57\u5178\uff0c\u4e26\u6839\u64da\u7b2c\u4e8c\u500b\u5143\u7d20\u6392\u5e8f<br \/>\nmy_dict = dict(sorted(((&#8216;a&#8217;, 1), (&#8216;b&#8217;, 2), (&#8216;c&#8217;, 3)), key=lambda x: x[1]))<\/p>\n<p># \u5c07\u5217\u8868\u8f49\u63db\u6210\u5b57\u5178\uff0c\u4e26\u6839\u64da\u7b2c\u4e8c\u500b\u5143\u7d20\u6392\u5e8f<br \/>\nmy_dict = dict(sorted([[&#8216;a&#8217;, 1], [&#8216;b&#8217;, 2], [&#8216;c&#8217;, 3]], key=lambda x: x[1]))<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u5efa\u7acb\u4e00\u500b\u5b57\u5178\uff0c\u5305\u542b\u9375\u503c\u5c0d\u6309\u503c\u6392\u5e8f\uff1a`1:a`, `2:b`, `3:c`\u3002<\/p>\n<h2>dict() \u7684\u61c9\u7528<\/h2>\n<p>`dict()` \u51fd\u5f0f\u4e0d\u50c5\u9650\u65bc\u7c21\u55ae\u7684\u9375\u503c\u5c0d\u5132\u5b58\uff0c\u5b83\u5728\u8cc7\u6599\u8655\u7406\u548c\u6578\u64da\u5206\u6790\u4e2d\u4e5f\u767c\u63ee\u8457\u91cd\u8981\u7684\u4f5c\u7528\u3002\u4f8b\u5982\uff0c\u7576\u4f60\u9700\u8981\u5f9e\u8cc7\u6599\u5eab\u4e2d\u63d0\u53d6\u6578\u64da\u4e26\u5c07\u5176\u8f49\u63db\u70ba\u5b57\u5178\u683c\u5f0f\u6642\uff0c`dict()` \u53ef\u4ee5\u7c21\u5316\u9019\u4e00\u904e\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u5be6\u969b\u6848\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5047\u8a2d\u5f9e\u8cc7\u6599\u5eab\u7372\u53d6\u7684\u8cc7\u6599<br \/>\ndata = [(&#8216;Alice&#8217;, 25), (&#8216;Bob&#8217;, 30), (&#8216;Charlie&#8217;, 35)]\n<p># \u5c07\u8cc7\u6599\u8f49\u63db\u70ba\u5b57\u5178<br \/>\nuser_dict = dict(data)<br \/>\nprint(user_dict)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u4f60\u5c31\u80fd\u5f97\u5230\u4e00\u500b\u65b9\u4fbf\u67e5\u627e\u7684\u5b57\u5178\uff1a`{&#8216;Alice&#8217;: 25, &#8216;Bob&#8217;: 30, &#8216;Charlie&#8217;: 35}`\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `dict()` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u932f\u8aa4\u7684\u8cc7\u6599\u683c\u5f0f**\uff1a\u78ba\u4fdd\u50b3\u5165\u7684\u53ef\u8fed\u4ee3\u7269\u4ef6\u662f\u7531\u53ef\u54c8\u5e0c\u7684\u9375\u548c\u5c0d\u61c9\u7684\u503c\u7d44\u6210\u7684\u3002<br \/>\n&#8211; **\u91cd\u8907\u9375**\uff1a\u5982\u679c\u5728\u8f38\u5165\u8cc7\u6599\u4e2d\u5b58\u5728\u91cd\u8907\u7684\u9375\uff0c\u5f8c\u8005\u6703\u8986\u84cb\u524d\u8005\u7684\u503c\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Python \u7684 `dict()` \u51fd\u5f0f\u662f\u5efa\u7acb\u548c\u64cd\u4f5c\u5b57\u5178\u8cc7\u6599\u7d50\u69cb\u7684\u57fa\u672c\u5de5\u5177\u3002\u900f\u904e\u9019\u4e9b\u7bc4\u4f8b\u548c\u61c9\u7528\uff0c\u958b\u767c\u8005\u80fd\u5920\u66f4\u6709\u6548\u7387\u5730\u7ba1\u7406\u8cc7\u6599\u3002\u96a8\u8457 Python \u7684\u4e0d\u65b7\u66f4\u65b0\uff0c\u4e86\u89e3\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u80fd\u529b\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u5b78\u7fd2\u95dc\u65bc Python \u7684\u5176\u4ed6\u4e3b\u984c\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7[Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/601ff6a2fd89780001e0037c)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: dict() \u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u54ea\u4e9b\u985e\u578b\u7684\u8cc7\u6599\uff1f**<br \/>\nA1: `dict()` \u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u7269\u4ef6\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5143\u7d44\u6216\u5176\u4ed6\u5b57\u5178\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406 dict() \u4e2d\u7684\u91cd\u8907\u9375\uff1f**<br \/>\nA2: \u5728\u4f7f\u7528 `dict()` \u6642\uff0c\u82e5\u9047\u5230\u91cd\u8907\u7684\u9375\uff0c\u5f8c\u9762\u7684\u9375\u503c\u6703\u8986\u84cb\u524d\u9762\u7684\u9375\u503c\u3002\u82e5\u9700\u4fdd\u7559\u6240\u6709\u9375\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `collections.defaultdict`\u3002<\/p>\n<p>**Q3: dict() \u51fd\u5f0f\u662f\u5426\u652f\u6301\u5d4c\u5957\u5b57\u5178\uff1f**<br \/>\nA3: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728\u5b57\u5178\u4e2d\u7684\u503c\u90e8\u5206\u4f7f\u7528\u53e6\u4e00\u500b\u5b57\u5178\uff0c\u9019\u6a23\u53ef\u4ee5\u5275\u5efa\u591a\u5c64\u6b21\u7684\u8cc7\u6599\u7d50\u69cb\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2Python\u5b57\u5178(dict)\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Python\u5b57\u5178\u4f86\u5132\u5b58\u548c\u64cd\u4f5c\u8cc7\u6599\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u4f86\u63d0\u9ad8\u7a0b\u5f0f\u7684\u6548\u7387\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-3366","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-Si","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3366","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=3366"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3366\/revisions"}],"predecessor-version":[{"id":3367,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3366\/revisions\/3367"}],"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=3366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}