{"id":999653,"date":"2024-12-27T09:43:19","date_gmt":"2024-12-27T01:43:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999653.html"},"modified":"2024-12-27T09:43:21","modified_gmt":"2024-12-27T01:43:21","slug":"python%e5%a6%82%e4%bd%95%e4%b8%ba%e5%ad%97%e5%85%b8%e6%b7%bb%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/999653.html","title":{"rendered":"python\u5982\u4f55\u4e3a\u5b57\u5178\u6dfb\u52a0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074556\/7e994ee0-c054-443d-94e4-de9ee0d4bb2f.webp\" alt=\"python\u5982\u4f55\u4e3a\u5b57\u5178\u6dfb\u52a0\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4e3a\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u6709<strong>\u4f7f\u7528\u952e\u503c\u5bf9\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5176\u4e2d<strong>\u4f7f\u7528\u952e\u503c\u5bf9\u76f4\u63a5\u8d4b\u503c<\/strong>\u662f\u6700\u5e38\u7528\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u4e3a\u5b57\u5178\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u6216\u8005\u66f4\u65b0\u73b0\u6709\u952e\u7684\u503c\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u7684\u6280\u5de7\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u952e\u503c\u5bf9\u76f4\u63a5\u8d4b\u503c<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u952e\u503c\u5bf9\u76f4\u63a5\u8d4b\u503c\u662f\u4e3a\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002\u53ea\u9700\u901a\u8fc7\u5b57\u5178\u540d\u548c\u65b0\u7684\u952e\uff0c\u5c06\u503c\u8d4b\u7ed9\u5b83\u5373\u53ef\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u66f4\u65b0\u8be5\u952e\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>my_dict[&#39;c&#39;] = 3  # \u6dfb\u52a0\u65b0\u952e\u503c\u5bf9<\/p>\n<p>my_dict[&#39;a&#39;] = 4  # \u66f4\u65b0\u5df2\u6709\u952e\u7684\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e<strong>\u7b80\u5355\u76f4\u89c2<\/strong>\uff0c\u5e76\u4e14\u80fd\u591f\u540c\u65f6\u5b8c\u6210\u6dfb\u52a0\u548c\u66f4\u65b0\u7684\u64cd\u4f5c\uff0c\u7f3a\u70b9\u662f<strong>\u65e0\u6cd5\u540c\u65f6\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9<\/strong>\u3002\u56e0\u6b64\uff0c\u5728\u9700\u8981\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u573a\u666f\u4e0b\uff0c<code>update()<\/code>\u65b9\u6cd5\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u952e\u503c\u5bf9\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5408\u5e76\u5230\u5f53\u524d\u5b57\u5178\u4e2d\u3002\u5b83\u80fd\u591f\u540c\u65f6\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u5e76\u4e14\u5728\u9047\u5230\u76f8\u540c\u952e\u65f6\u4f1a\u66f4\u65b0\u5176\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>additional_items = {&#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p>my_dict.update(additional_items)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>update()<\/code>\u65b9\u6cd5\u5c06<code>additional_items<\/code>\u4e2d\u7684\u952e\u503c\u5bf9\u5408\u5e76\u5230\u4e86<code>my_dict<\/code>\u4e2d\u3002<strong>\u5f53\u51fa\u73b0\u76f8\u540c\u952e\u65f6\uff0c\u5b57\u5178\u4f1a\u66f4\u65b0\u8be5\u952e\u7684\u503c<\/strong>\u3002\u8fd9\u4e2a\u7279\u6027\u975e\u5e38\u9002\u5408\u9700\u8981\u6279\u91cf\u66f4\u65b0\u6216\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>setdefault()<\/code>\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u5b57\u5178\u4e2d\u6307\u5b9a\u952e\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u6dfb\u52a0\u8be5\u952e\uff0c\u5e76\u5c06\u5176\u503c\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u952e\u7684\u503c\uff0c\u6216\u8005\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>value = my_dict.setdefault(&#39;c&#39;, 3)  # &#39;c&#39;\u4e0d\u5b58\u5728\uff0c\u6dfb\u52a0\u5e76\u8fd4\u56de3<\/p>\n<p>value_existing = my_dict.setdefault(&#39;a&#39;, 4)  # &#39;a&#39;\u5b58\u5728\uff0c\u8fd4\u56de1\u4e14\u4e0d\u66f4\u65b0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>setdefault()<\/code>\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e<strong>\u80fd\u591f\u5728\u83b7\u53d6\u952e\u503c\u7684\u540c\u65f6\u786e\u4fdd\u952e\u5b58\u5728<\/strong>\uff0c\u5e76\u4e14\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u8fd9\u5728\u5904\u7406\u5b57\u5178\u65f6\u80fd\u591f\u51cf\u5c11\u6f5c\u5728\u7684<code>KeyError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u7528\u4e8e\u6839\u636e\u73b0\u6709\u6570\u636e\u6784\u9020\u5b57\u5178\u3002\u867d\u7136\u4e0d\u76f4\u63a5\u7528\u4e8e\u6dfb\u52a0\u5143\u7d20\uff0c\u4f46\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5b57\u5178\uff0c\u6216\u5728\u9700\u8981\u52a8\u6001\u8ba1\u7b97\u952e\u503c\u5bf9\u7684\u573a\u666f\u4e0b\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>values = [1, 2, 3]<\/p>\n<p>my_dict = {k: v for k, v in zip(keys, values)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u9700\u8981\u4ece\u4e24\u4e2a\u5217\u8868\uff08\u952e\u548c\u503c\uff09\u751f\u6210\u5b57\u5178\uff0c\u6216\u8005\u9700\u8981\u6839\u636e\u7279\u5b9a\u89c4\u5219\u751f\u6210\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u6709\u51e0\u4e2a\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u952e\u7684\u552f\u4e00\u6027<\/strong>\uff1a\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5982\u679c\u91cd\u590d\u8d4b\u503c\u76f8\u540c\u7684\u952e\uff0c\u540e\u8005\u4f1a\u8986\u76d6\u524d\u8005\u3002<\/li>\n<li><strong>\u952e\u7684\u53ef\u54c8\u5e0c\u6027<\/strong>\uff1a\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u53ef\u54c8\u5e0c\u7684\uff08\u901a\u5e38\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5143\u7ec4\uff09\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u6dfb\u52a0\u548c\u66f4\u65b0\u5b57\u5178\u7684\u64cd\u4f5c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u5185\u5b58\u5206\u914d\u548c\u5783\u573e\u56de\u6536\u53ef\u80fd\u5e26\u6765\u7684\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u548c\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5728Python\u4e2d\u64cd\u4f5c\u5b57\u5178\uff0c\u4e3a\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u952e\u503c\u5bf9\u8d4b\u503c\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6279\u91cf\u66f4\u65b0\uff0cPython\u7684\u5b57\u5178\u64cd\u4f5c\u90fd\u80fd\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5411\u5b57\u5178\u6dfb\u52a0\u65b0\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u3002\u4f7f\u7528\u5b57\u5178\u540d\u79f0\u548c\u65b9\u62ec\u53f7\u6765\u6307\u5b9a\u952e\uff0c\u7136\u540e\u8d4b\u503c\u5373\u53ef\u3002\u4f8b\u5982\uff0c<code>my_dict[&#39;new_key&#39;] = &#39;new_value&#39;<\/code>\u5c06\u4f1a\u5728\u5b57\u5178<code>my_dict<\/code>\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e<code>&#39;new_key&#39;<\/code>\uff0c\u5176\u503c\u4e3a<code>&#39;new_value&#39;<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u5141\u8bb8\u60a8\u4f20\u5165\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u53ef\u8fed\u4ee3\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c<code>my_dict.update({&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;})<\/code>\u4f1a\u5c06<code>key1<\/code>\u548c<code>key2<\/code>\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u952e\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8ePython\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u5728\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u4e4b\u524d\uff0c\u60a8\u53ef\u80fd\u60f3\u786e\u8ba4\u67d0\u4e2a\u952e\u662f\u5426\u5df2\u5b58\u5728\u3002\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u6765\u68c0\u67e5\uff0c\u4f8b\u5982\uff0c<code>if &#39;existing_key&#39; in my_dict:<\/code>\u3002\u5982\u679c\u952e\u5b58\u5728\uff0c\u5219\u53ef\u4ee5\u9009\u62e9\u66f4\u65b0\u5176\u503c\u6216\u4fdd\u6301\u4e0d\u53d8\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u610f\u5916\u8986\u76d6\u5df2\u6709\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4e3a\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u6709\u4f7f\u7528\u952e\u503c\u5bf9\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u4f7f\u7528setd [&hellip;]","protected":false},"author":3,"featured_media":999660,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999653"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=999653"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999653\/revisions"}],"predecessor-version":[{"id":999661,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999653\/revisions\/999661"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999660"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}