{"id":1133514,"date":"2025-01-08T21:07:07","date_gmt":"2025-01-08T13:07:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133514.html"},"modified":"2025-01-08T21:07:10","modified_gmt":"2025-01-08T13:07:10","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%ad%97%e5%85%b8%e4%b8%ad%e5%a2%9e%e5%8a%a0%e4%b8%80%e4%b8%aa%e9%94%ae%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133514.html","title":{"rendered":"python\u5982\u4f55\u5728\u5b57\u5178\u4e2d\u589e\u52a0\u4e00\u4e2a\u952e\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102737\/f01040c1-79e6-4e85-a446-095bdce8e1fd.webp\" alt=\"python\u5982\u4f55\u5728\u5b57\u5178\u4e2d\u589e\u52a0\u4e00\u4e2a\u952e\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u5b57\u5178\u4e2d\u589e\u52a0\u4e00\u4e2a\u952e\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5305\u62ec\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update()\u65b9\u6cd5\u7b49\u3002<\/strong> \u76f4\u63a5\u8d4b\u503c\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528\u8d77\u6765\u4e5f\u975e\u5e38\u76f4\u89c2\uff1b\u800cupdate()\u65b9\u6cd5\u5219\u53ef\u4ee5\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u8bb2\u8ff0\u8fd9\u4e24\u79cd\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u5e76\u7ed3\u5408\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u5e38\u89c1\u3001\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u53ea\u9700\u7528\u65b9\u62ec\u53f7\u6307\u5b9a\u65b0\u7684\u952e\uff0c\u7136\u540e\u8d4b\u503c\u5373\u53ef\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u76f4\u63a5\u8d4b\u503c\u4f1a\u8986\u76d6\u539f\u6709\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}<\/p>\n<p>my_dict[&quot;city&quot;] = &quot;New York&quot;<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u7b80\u5355\u5730\u5728\u5b57\u5178\u4e2d\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&quot;city&quot;: &quot;New York&quot;<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528update()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>update()\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}<\/p>\n<p>my_dict.update({&quot;city&quot;: &quot;New York&quot;, &quot;country&quot;: &quot;USA&quot;})<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528update()\u65b9\u6cd5\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528setdefault()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>setdefault()\u65b9\u6cd5\u9664\u4e86\u6dfb\u52a0\u65b0\u952e\u503c\u5bf9\u5916\uff0c\u8fd8\u53ef\u4ee5\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u8be5\u952e\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}<\/p>\n<p>my_dict.setdefault(&quot;city&quot;, &quot;New York&quot;)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528setdefault()\u65b9\u6cd5\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u540c\u65f6\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5b57\u5178\u89e3\u5305\uff08Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\uff09<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u89e3\u5305\uff08dictionary unpacking\uff09\u662f\u4e00\u79cd\u9ad8\u7ea7\u7528\u6cd5\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}<\/p>\n<p>additional_info = {&quot;city&quot;: &quot;New York&quot;, &quot;country&quot;: &quot;USA&quot;}<\/p>\n<p>new_dict = {&lt;strong&gt;my_dict, &lt;\/strong&gt;additional_info}<\/p>\n<p>print(new_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u89e3\u5305\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\uff08dictionary comprehension\uff09<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u5b57\u5178\u65f6\u8fdb\u884c\u590d\u6742\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 25, &#39;New York&#39;]<\/p>\n<p>my_dict = {k: v for k, v in zip(keys, values)}<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5e76\u589e\u52a0\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u589e\u52a0\u5b57\u5178\u952e\u503c\u5bf9\u7684\u65b9\u6cd5\u975e\u5e38\u591a\u6837\u5316\uff0c<strong>\u5305\u62ec\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u4f7f\u7528setdefault()\u65b9\u6cd5\u3001\u5b57\u5178\u89e3\u5305\u548c\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u76f4\u63a5\u8d4b\u503c\u548cupdate()\u65b9\u6cd5\u6700\u4e3a\u5e38\u89c1\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u7b80\u5355\u573a\u666f\uff1bsetdefault()\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u7684\u60c5\u51b5\uff1b\u5b57\u5178\u89e3\u5305\u548c\u5b57\u5178\u63a8\u5bfc\u5f0f\u5219\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u548c\u5408\u5e76\u5b57\u5178\u7684\u60c5\u51b5\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u64cd\u4f5cPython\u5b57\u5178\uff0c\u63d0\u5347\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002\u65e0\u8bba\u662f\u5f00\u53d1\u7b80\u5355\u7684\u5c0f\u7a0b\u5e8f\uff0c\u8fd8\u662f\u6784\u5efa\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u7cfb\u7edf\uff0c\u8fd9\u4e9b\u6280\u5de7\u90fd\u5c06\u4e3a\u60a8\u5e26\u6765\u6781\u5927\u7684\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\u6765\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178\uff0c\u6bd4\u5982<code>my_dict = {}<\/code>\uff0c\u4f60\u53ea\u9700\u4f7f\u7528<code>my_dict[&#39;new_key&#39;] = &#39;new_value&#39;<\/code>\u7684\u65b9\u5f0f\uff0c\u5c31\u80fd\u6210\u529f\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002\u8fd9\u6837\uff0c<code>my_dict<\/code>\u5c06\u53d8\u4e3a<code>{&#39;new_key&#39;: &#39;new_value&#39;}<\/code>\u3002<\/p>\n<p><strong>\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4e3a\u4e86\u540c\u65f6\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\u3002\u6bd4\u5982\uff0c<code>my_dict.update({&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;})<\/code>\u5c06\u540c\u65f6\u6dfb\u52a0<code>key1<\/code>\u548c<code>key2<\/code>\u5230\u5b57\u5178\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u4e5f\u63d0\u9ad8\u4e86\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u5b57\u5178\u4e2d\u952e\u503c\u5bf9\u7684\u952e\u662f\u5426\u53ef\u4ee5\u91cd\u590d\uff1f<\/strong><br \/>\u5728Python\u5b57\u5178\u4e2d\uff0c\u952e\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u5c1d\u8bd5\u6dfb\u52a0\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u952e\uff0c\u65b0\u7684\u503c\u5c06\u8986\u76d6\u65e7\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5148\u6267\u884c<code>my_dict[&#39;key&#39;] = &#39;value1&#39;<\/code>\uff0c\u518d\u6267\u884c<code>my_dict[&#39;key&#39;] = &#39;value2&#39;<\/code>\uff0c\u6700\u7ec8\u7ed3\u679c\u5c06\u662f<code>{&#39;key&#39;: &#39;value2&#39;}<\/code>\uff0c\u56e0\u4e3a<code>value1<\/code>\u88ab<code>value2<\/code>\u66ff\u6362\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u5b57\u5178\u4e2d\u589e\u52a0\u4e00\u4e2a\u952e\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5305\u62ec\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update()\u65b9\u6cd5\u7b49\u3002 \u76f4\u63a5\u8d4b\u503c\u662f\u6700\u7b80\u5355 [&hellip;]","protected":false},"author":3,"featured_media":1133526,"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\/1133514"}],"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=1133514"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133514\/revisions"}],"predecessor-version":[{"id":1133529,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133514\/revisions\/1133529"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133526"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}