{"id":3512,"date":"2025-06-02T15:06:26","date_gmt":"2025-06-04T07:28:26","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3512"},"modified":"2025-06-04T15:06:26","modified_gmt":"2025-06-04T07:28:26","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84setitem%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd%ef%bc%8c%e8%ae%93%e4%bd%a0%e7%9a%84%e7%a8%8b%e5%bc%8f%e6%9b%b4%e5%8a%a0%e9%9d%88%e6%b4%bb","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84setitem%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd%ef%bc%8c%e8%ae%93%e4%bd%a0%e7%9a%84%e7%a8%8b%e5%bc%8f%e6%9b%b4%e5%8a%a0%e9%9d%88%e6%b4%bb\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `setitem()` \u51fd\u6578\uff1a\u63d0\u5347\u5b57\u5178\u64cd\u4f5c\u7684\u9748\u6d3b\u6027\u8207\u6548\u7387"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, setitem, \u5b57\u5178\u64cd\u4f5c, Python\u6559\u5b78\"><\/p>\n<h1>Python \u4e2d\u7684 `setitem()` \u65b9\u6cd5<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`setitem()` \u65b9\u6cd5\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u7528\u65bc\u5c0d\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d\u9032\u884c\u8a2d\u5b9a\u3002\u9019\u500b\u65b9\u6cd5\u4e0d\u50c5\u53ef\u4ee5\u66f4\u65b0\u73fe\u6709\u7684\u9375\u503c\u5c0d\uff0c\u9084\u80fd\u5920\u6dfb\u52a0\u65b0\u7684\u9375\u503c\u5c0d\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u52a0\u9748\u6d3b\u8207\u9ad8\u6548\u3002<\/p>\n<h2>\u4f7f\u7528 `setitem()` \u65b9\u6cd5<\/h2>\n<p>\u8981\u4f7f\u7528 `setitem()` \u65b9\u6cd5\uff0c\u6211\u5011\u9996\u5148\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u5b57\u5178\uff0c\u7136\u5f8c\u5229\u7528\u9019\u500b\u65b9\u6cd5\u5c0d\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d\u9032\u884c\u8a2d\u5b9a\u3002\u4ee5\u4e0b\u662f2025\u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u5b57\u5178<br \/>\nmy_dict = {<br \/>\n    &#8216;name&#8217;: &#8216;John&#8217;,<br \/>\n    &#8216;age&#8217;: 20<br \/>\n}<\/p>\n<p># \u4f7f\u7528 setitem() \u65b9\u6cd5\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d<br \/>\nmy_dict.__setitem__(&#8216;name&#8217;, &#8216;Mary&#8217;)<\/p>\n<p># \u4f7f\u7528 setitem() \u65b9\u6cd5\u6dfb\u52a0\u65b0\u7684\u9375\u503c\u5c0d<br \/>\nmy_dict.__setitem__(&#8216;gender&#8217;, &#8216;female&#8217;)<\/p>\n<p># \u8f38\u51fa\u5b57\u5178<br \/>\nprint(my_dict)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u5b57\u5178\uff0c\u7136\u5f8c\u4f7f\u7528 `__setitem__()` \u65b9\u6cd5\u5c0d\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d\u9032\u884c\u8a2d\u5b9a\uff0c\u6700\u5f8c\u8f38\u51fa\u5b57\u5178\uff0c\u8f38\u51fa\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n{&#8216;name&#8217;: &#8216;Mary&#8217;, &#8216;age&#8217;: 20, &#8216;gender&#8217;: &#8216;female&#8217;}<br \/>\n&#8220;`<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528 `setitem()` \u65b9\u6cd5\u6210\u529f\u5730\u66f4\u65b0\u4e86\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d\uff0c\u4e26\u4e14\u6dfb\u52a0\u4e86\u65b0\u7684\u9375\u503c\u5c0d\u3002<\/p>\n<h2>\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u4f7f\u7528 `setitem()` \u65b9\u6cd5\u6642\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u5982\u679c\u5b57\u5178\u4e2d\u5df2\u7d93\u5b58\u5728\u8a72\u9375\uff0c\u5247\u6703\u66f4\u65b0\u8a72\u9375\u7684\u503c\uff1b<\/strong><\/li>\n<li><strong>\u5982\u679c\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u8a72\u9375\uff0c\u5247\u6703\u6dfb\u52a0\u8a72\u9375\u503c\u5c0d\uff1b<\/strong><\/li>\n<li><strong>\u5982\u679c\u8981\u522a\u9664\u67d0\u500b\u9375\u503c\u5c0d\uff0c\u53ef\u4ee5\u5c07\u5176\u503c\u8a2d\u70ba None\u3002<\/strong><\/li>\n<\/ul>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\uff0c\u6bd4\u5982\uff1a<\/p>\n<p>1. **KeyError**\uff1a\u7576\u4f60\u8a66\u5716\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u9375\u6642\uff0c\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u5728\u4f7f\u7528 `setitem()` \u65b9\u6cd5\u4e4b\u524d\uff0c\u6aa2\u67e5\u8a72\u9375\u662f\u5426\u5b58\u5728\u65bc\u5b57\u5178\u4e2d\u3002<br \/>\n2. **TypeError**\uff1a\u82e5\u5617\u8a66\u5c07\u4e0d\u652f\u6301\u7684\u985e\u578b\u4f5c\u70ba\u9375\uff08\u5982\u5217\u8868\uff09\uff0c\u5247\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u4f60\u7684\u9375\u662f\u53ef\u54c8\u5e0c\u7684\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32\u6216\u5143\u7d44\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`setitem()` \u65b9\u6cd5\u4e0d\u50c5\u9069\u7528\u65bc\u57fa\u672c\u7684\u5b57\u5178\u64cd\u4f5c\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u66f4\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5d4c\u5957\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u5d4c\u5957\u5b57\u5178<br \/>\nnested_dict = {<br \/>\n    &#8216;user&#8217;: {<br \/>\n        &#8216;name&#8217;: &#8216;John&#8217;,<br \/>\n        &#8216;age&#8217;: 20<br \/>\n    }<br \/>\n}<\/p>\n<p># \u66f4\u65b0\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c<br \/>\nnested_dict[&#8216;user&#8217;].__setitem__(&#8216;name&#8217;, &#8216;Mary&#8217;)<br \/>\nprint(nested_dict)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u64cd\u4f5c\u4f7f\u5f97\u5b57\u5178\u7684\u7ba1\u7406\u66f4\u52a0\u9748\u6d3b\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u4e2d\u7684 `setitem()` \u65b9\u6cd5\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u9748\u6d3b\u5730\u66f4\u65b0\u548c\u6dfb\u52a0\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d\u3002\u4f7f\u7528\u9019\u500b\u65b9\u6cd5\u6642\uff0c\u52d9\u5fc5\u6ce8\u610f\u9375\u7684\u5b58\u5728\u6027\u4ee5\u53ca\u985e\u578b\u9650\u5236\uff0c\u4ee5\u907f\u514d\u5e38\u898b\u7684\u932f\u8aa4\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u66f4\u591a\u64cd\u4f5c\uff0c\u5efa\u8b70\u67e5\u770b\u9019\u7bc7 [Python \u8cc7\u6599\u7d50\u69cb\u6559\u5b78](https:\/\/vocus.cc) \u4ee5\u7372\u53d6\u66f4\u591a\u5be6\u7528\u8cc7\u8a0a\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `setitem()` \u65b9\u6cd5\u548c\u76f4\u63a5\u4f7f\u7528 `my_dict[key] = value` \u6709\u4f55\u4e0d\u540c\uff1f**<br \/>\nA1: `setitem()` \u65b9\u6cd5\u662f Python \u7684\u5167\u5efa\u65b9\u6cd5\uff0c\u7528\u65bc\u81ea\u5b9a\u7fa9\u985e\u5225\u6642\u7684\u9375\u503c\u8a2d\u7f6e\uff0c\u800c `my_dict[key] = value` \u662f\u66f4\u5e38\u898b\u7684\u8a9e\u6cd5\uff0c\u4e8c\u8005\u6700\u7d42\u6548\u679c\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u4f7f\u7528\u5f8c\u8005\u66f4\u70ba\u7c21\u6f54\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u522a\u9664\u5b57\u5178\u4e2d\u7684\u9375\u503c\u5c0d\uff1f**<br \/>\nA2: \u82e5\u60f3\u522a\u9664\u4e00\u500b\u9375\u503c\u5c0d\uff0c\u53ef\u4ee5\u4f7f\u7528 `del my_dict[key]` \u6216\u5c07\u5176\u503c\u8a2d\u70ba None\uff0c\u9019\u6a23\u53ef\u4ee5\u6839\u64da\u9700\u8981\u9078\u64c7\u4e0d\u540c\u7684\u522a\u9664\u65b9\u5f0f\u3002<\/p>\n<p>**Q3: `setitem()` \u65b9\u6cd5\u53ef\u4ee5\u7528\u65bc\u6240\u6709\u6578\u64da\u985e\u578b\u55ce\uff1f**<br \/>\nA3: `setitem()` \u65b9\u6cd5\u4e3b\u8981\u7528\u65bc\u5b57\u5178\uff0c\u9375\u5fc5\u9808\u662f\u53ef\u54c8\u5e0c\u7684\u985e\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u6216\u5143\u7d44\uff0c\u4e0d\u80fd\u4f7f\u7528\u5217\u8868\u7b49\u4e0d\u53ef\u54c8\u5e0c\u7684\u985e\u578b\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u66f4\u6df1\u5165\u5730\u4e86\u89e3 Python \u7684 `setitem()` \u65b9\u6cd5\uff0c\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u6280\u5de7\uff01<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684setitem()\u51fd\u6578\uff0c\u53ef\u4ee5\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u52a0\u9748\u6d3b\uff0c\u66f4\u6709\u6548\u7387\u5730\u64cd\u4f5c\u8cc7\u6599\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u52a0\u5bb9\u6613\u7dad\u8b77\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-3512","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-UE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3512","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=3512"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3512\/revisions"}],"predecessor-version":[{"id":12812,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3512\/revisions\/12812"}],"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=3512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}