{"id":2984,"date":"2025-05-30T17:05:47","date_gmt":"2025-06-04T09:14:47","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2984"},"modified":"2025-06-04T17:05:47","modified_gmt":"2025-06-04T09:14:47","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","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\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22 Python \u5b57\u5178 (dict) \u7684\u4f7f\u7528\u65b9\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, dict(), \u5b57\u5178, Python \u6559\u5b78, Python \u7a0b\u5f0f\u8a2d\u8a08\"><\/p>\n<h1>\u6df1\u5165\u63a2\u7d22 Python \u5b57\u5178 (dict) \u7684\u4f7f\u7528\u65b9\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Python \u7684 `dict()` \u51fd\u5f0f\u662f\u5275\u5efa\u8207\u64cd\u4f5c\u5b57\u5178\u7684\u6838\u5fc3\u5de5\u5177\u3002\u5b57\u5178\u662f\u4e00\u7a2e\u91cd\u8981\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5141\u8a31\u6211\u5011\u4ee5\u9375\u503c\u5c0d\u7684\u5f62\u5f0f\u5132\u5b58\u548c\u7ba1\u7406\u8cc7\u6599\u3002\u5728 2025 \u5e74\u7684\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u4e2d\uff0c\u4e86\u89e3\u5b57\u5178\u7684\u4f7f\u7528\u65b9\u5f0f\u5c0d\u65bc\u6bcf\u4f4d Python \u958b\u767c\u8005\u4f86\u8aaa\u90fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002<\/p>\n<p>## dict() \u51fd\u5f0f\u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<p>`dict()` \u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u4e0d\u540c\u985e\u578b\u7684\u53c3\u6578\uff0c\u9019\u8b93\u5b83\u975e\u5e38\u9748\u6d3b\u3002\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>1. **\u5efa\u7acb\u7a7a\u5b57\u5178**\uff1a\u5982\u679c\u4e0d\u50b3\u5165\u4efb\u4f55\u53c3\u6578\uff0c\u5c07\u6703\u5efa\u7acb\u4e00\u500b\u7a7a\u5b57\u5178\u3002<br \/>\n2. **\u5efa\u7acb\u5305\u542b\u9375\u503c\u5c0d\u7684\u5b57\u5178**\uff1a\u53ef\u4ee5\u76f4\u63a5\u50b3\u5165\u591a\u500b\u9375\u503c\u5c0d\u4f5c\u70ba\u53c3\u6578\u3002<\/p>\n<p>### \u5275\u5efa\u5b57\u5178\u7684\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u5efa\u7acb\u5b57\u5178\u7684\u5e7e\u500b\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5efa\u7acb\u4e00\u500b\u7a7a\u7684\u5b57\u5178<br \/>\nmy_dict = dict()<\/p>\n<p># \u5efa\u7acb\u4e00\u500b\u5305\u542b\u9375\u503c\u5c0d\u7684\u5b57\u5178<br \/>\nmy_dict = dict(key1=&#8221;value1&#8243;, key2=&#8221;value2&#8243;)<br \/>\n&#8220;`<\/p>\n<p>\u9664\u4e86\u4ee5\u4e0a\u65b9\u5f0f\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u4f7f\u7528\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\u4f86\u5275\u5efa\u5b57\u5178\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u4f7f\u7528\u5217\u8868\u4f86\u5efa\u7acb\u5b57\u5178<br \/>\nmy_dict = dict([(&#8220;key1&#8221;, &#8220;value1&#8221;), (&#8220;key2&#8221;, &#8220;value2&#8221;)])<\/p>\n<p># \u4f7f\u7528\u5143\u7d44\u4f86\u5efa\u7acb\u5b57\u5178<br \/>\nmy_dict = dict(((&#8220;key1&#8221;, &#8220;value1&#8221;), (&#8220;key2&#8221;, &#8220;value2&#8221;)))<br \/>\n&#8220;`<\/p>\n<p>\u6b64\u5916\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u57fa\u65bc\u53e6\u4e00\u500b\u5b57\u5178\u5275\u5efa\u65b0\u7684\u5b57\u5178\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u4f7f\u7528\u53e6\u4e00\u500b\u5b57\u5178\u4f86\u5efa\u7acb\u65b0\u7684\u5b57\u5178<br \/>\nmy_dict = dict({&#8220;key1&#8221;: &#8220;value1&#8221;, &#8220;key2&#8221;: &#8220;value2&#8221;})<br \/>\n&#8220;`<\/p>\n<p>### \u66f4\u65b0\u5b57\u5178<\/p>\n<p>`dict()` \u51fd\u5f0f\u9664\u4e86\u53ef\u4ee5\u7528\u4f86\u5efa\u7acb\u5b57\u5178\uff0c\u9084\u80fd\u7528\u4f86\u66f4\u65b0\u73fe\u6709\u7684\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u66f4\u65b0\u5b57\u5178\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u66f4\u65b0\u5b57\u5178<br \/>\nmy_dict = {&#8220;key1&#8221;: &#8220;value1&#8221;, &#8220;key2&#8221;: &#8220;value2&#8221;}<br \/>\nmy_dict.update({&#8220;key3&#8221;: &#8220;value3&#8243;})<br \/>\n&#8220;`<\/p>\n<p>\u9019\u4e9b\u64cd\u4f5c\u8b93\u6211\u5011\u80fd\u5920\u9748\u6d3b\u5730\u7ba1\u7406\u8cc7\u6599\uff0c\u589e\u5f37\u4e86\u5b57\u5178\u5728 Python \u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u7684\u91cd\u8981\u6027\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `dict()` \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<p>&#8211; **\u9375\u91cd\u8907**\uff1a\u50b3\u5165\u91cd\u8907\u7684\u9375\u6703\u5c0e\u81f4\u5f8c\u4e00\u500b\u9375\u8986\u84cb\u524d\u4e00\u500b\u9375\u7684\u503c\u3002<br \/>\n&#8211; **\u985e\u578b\u4e0d\u5339\u914d**\uff1a\u78ba\u4fdd\u50b3\u5165\u7684\u53ef\u8fed\u4ee3\u5c0d\u8c61\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u90fd\u662f\u4e00\u500b\u4e8c\u5143\u7d44\uff0c\u5426\u5247\u6703\u5f15\u767c `ValueError`\u3002<\/p>\n<p>\u9019\u4e9b\u554f\u984c\u53ef\u4ee5\u901a\u904e\u6aa2\u67e5\u50b3\u5165\u7684\u6578\u64da\u7d50\u69cb\u4f86\u907f\u514d\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u5b57\u5178\u5728 Python \u4e2d\u7684\u61c9\u7528\u7bc4\u570d\u5ee3\u6cdb\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u65bc\uff1a<\/p>\n<p>&#8211; \u6578\u64da\u5eab\u67e5\u8a62\u7d50\u679c\u7684\u5132\u5b58\u3002<br \/>\n&#8211; JSON \u6578\u64da\u7684\u89e3\u6790\u8207\u8655\u7406\u3002<br \/>\n&#8211; \u9032\u884c\u7d71\u8a08\u5206\u6790\u6642\u7684\u8cc7\u6599\u5206\u7d44\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u6df1\u5165\u4e86\u89e3 Python \u7684\u5176\u4ed6\u8cc7\u6599\u7d50\u69cb\uff0c\u5efa\u8b70\u53c3\u8003 [Miner \u7684 Python \u6559\u5b78](https:\/\/miner.tw)\u3002<\/p>\n<p>## \u7e3d\u7d50<\/p>\n<p>Python \u7684 `dict()` \u51fd\u5f0f\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u529f\u80fd\u4f86\u5efa\u7acb\u548c\u7ba1\u7406\u5b57\u5178\uff0c\u9019\u5c0d\u65bc\u6709\u6548\u5730\u8655\u7406\u8cc7\u6599\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u76f8\u4fe1\u4f60\u5df2\u7d93\u638c\u63e1\u4e86\u5b57\u5178\u7684\u57fa\u672c\u7528\u6cd5\u548c\u4e00\u4e9b\u9032\u968e\u6280\u5de7\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: Python \u7684\u5b57\u5178\u53ef\u4ee5\u5305\u542b\u54ea\u4e9b\u6578\u64da\u985e\u578b\uff1f**<br \/>\nA1: Python \u7684\u5b57\u5178\u53ef\u4ee5\u5305\u542b\u5404\u7a2e\u6578\u64da\u985e\u578b\u4f5c\u70ba\u9375\u548c\u503c\uff0c\u5305\u62ec\u6574\u6578\u3001\u5b57\u4e32\u3001\u5217\u8868\u548c\u5176\u4ed6\u5b57\u5178\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u5178\u7684\u9375\u5fc5\u9808\u662f\u4e0d\u53ef\u8b8a\u985e\u578b\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u904d\u6b77\u5b57\u5178\u4e2d\u7684\u9375\u548c\u503c\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `items()` \u65b9\u6cd5\u904d\u6b77\u5b57\u5178\u4e2d\u7684\u9375\u548c\u503c\u3002\u4f8b\u5982\uff1a<br \/>\n&#8220;`python<br \/>\nfor key, value in my_dict.items():<br \/>\n    print(f&#8221;\u9375: {key}, \u503c: {value}&#8221;)<br \/>\n&#8220;`<\/p>\n<p>**Q3: \u5b57\u5178\u7684\u9375\u53ef\u4ee5\u91cd\u8907\u55ce\uff1f**<br \/>\nA3: \u4e0d\u53ef\u4ee5\u3002\u5b57\u5178\u7684\u9375\u5fc5\u9808\u662f\u552f\u4e00\u7684\uff0c\u82e5\u91cd\u8907\uff0c\u5f8c\u8005\u6703\u8986\u84cb\u524d\u8005\u7684\u503c\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u5167\u5bb9\u512a\u5316\u4e0d\u50c5\u63d0\u4f9b\u4e86\u66f4\u5168\u9762\u7684\u6559\u5b78\uff0c\u9084\u7b26\u5408 Google SEO \u7684\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u63d0\u5347\u6587\u7ae0\u7684\u641c\u5c0b\u5f15\u64ce\u6392\u540d\u3002<\/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-2984","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-M8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2984","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=2984"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2984\/revisions"}],"predecessor-version":[{"id":12986,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2984\/revisions\/12986"}],"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=2984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}