{"id":1084891,"date":"2025-01-08T13:11:16","date_gmt":"2025-01-08T05:11:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084891.html"},"modified":"2025-01-08T13:11:19","modified_gmt":"2025-01-08T05:11:19","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%90%91%e5%ad%97%e5%85%b8%e6%b7%bb%e5%8a%a0%e5%85%83%e7%b4%a0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1084891.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195035\/c7ef46e0-0916-417a-b832-f019762eab9e.webp\" alt=\"python\u4e2d\u5982\u4f55\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u65b9\u62ec\u53f7\u8bed\u6cd5\u3001\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u65b9\u62ec\u53f7\u8bed\u6cd5\u662f\u6700\u5e38\u89c1\u7684\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u65b9\u62ec\u53f7\u8bed\u6cd5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u65b9\u62ec\u53f7\u8bed\u6cd5\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u952e\u548c\u503c\u6765\u6dfb\u52a0\u65b0\u5143\u7d20\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u66f4\u65b0\u5176\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528\u65b9\u62ec\u53f7\u8bed\u6cd5\u6dfb\u52a0\u65b0\u5143\u7d20<\/strong><\/h2>\n<p>my_dict[&#39;address&#39;] = &#39;123 M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n St&#39;<\/p>\n<h2><strong>\u66f4\u65b0\u5df2\u6709\u5143\u7d20<\/strong><\/h2>\n<p>my_dict[&#39;age&#39;] = 26<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u65b9\u62ec\u53f7\u8bed\u6cd5\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;address&#39;: &#39;123 Main St&#39;<\/code>\uff0c\u540c\u65f6\u66f4\u65b0\u4e86\u952e<code>&#39;age&#39;<\/code>\u7684\u503c\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\u7528\u6765\u6279\u91cf\u6dfb\u52a0\u6216\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u5b83\u63a5\u53d7\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528update()\u65b9\u6cd5\u6dfb\u52a0\u65b0\u5143\u7d20<\/strong><\/h2>\n<p>my_dict.update({&#39;address&#39;: &#39;123 Main St&#39;, &#39;email&#39;: &#39;alice@example.com&#39;})<\/p>\n<h2><strong>\u66f4\u65b0\u5df2\u6709\u5143\u7d20<\/strong><\/h2>\n<p>my_dict.update({&#39;age&#39;: 26})<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u4e00\u6b21\u6027\u6dfb\u52a0\u4e86\u4e24\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;address&#39;: &#39;123 Main St&#39;<\/code>\u548c<code>&#39;email&#39;: &#39;alice@example.com&#39;<\/code>\uff0c\u540c\u65f6\u66f4\u65b0\u4e86\u952e<code>&#39;age&#39;<\/code>\u7684\u503c\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\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\uff0c\u4f46\u53ea\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u6dfb\u52a0\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u800c\u4e0d\u4f1a\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528setdefault()\u65b9\u6cd5\u6dfb\u52a0\u65b0\u5143\u7d20<\/strong><\/h2>\n<p>my_dict.setdefault(&#39;address&#39;, &#39;123 Main St&#39;)<\/p>\n<h2><strong>\u5c1d\u8bd5\u4f7f\u7528setdefault()\u65b9\u6cd5\u66f4\u65b0\u5df2\u6709\u5143\u7d20\uff08\u4e0d\u4f1a\u66f4\u65b0\uff09<\/strong><\/h2>\n<p>my_dict.setdefault(&#39;age&#39;, 26)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;address&#39;: &#39;123 Main St&#39;<\/code>\uff0c\u4f46\u5c1d\u8bd5\u66f4\u65b0\u952e<code>&#39;age&#39;<\/code>\u7684\u503c\u65f6\uff0c\u6ca1\u6709\u6210\u529f\uff0c\u56e0\u4e3a<code>setdefault()<\/code>\u53ea\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u6dfb\u52a0\u65b0\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5b57\u5178\u3002\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u521b\u5efa\u5b57\u5178\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u6dfb\u52a0\u6216\u66f4\u65b0\u73b0\u6709\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6dfb\u52a0\u65b0\u5143\u7d20<\/strong><\/h2>\n<p>new_elements = {k: v for k, v in [(&#39;address&#39;, &#39;123 Main St&#39;), (&#39;email&#39;, &#39;alice@example.com&#39;)]}<\/p>\n<h2><strong>\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {&lt;strong&gt;my_dict, &lt;\/strong&gt;new_elements}<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u65b0\u5143\u7d20\u7684\u5b57\u5178<code>new_elements<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u89e3\u5305\u64cd\u4f5c\u5c06\u5176\u4e0e\u539f\u59cb\u5b57\u5178<code>my_dict<\/code>\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528<code>collections.defaultdict<\/code><\/p>\n<\/p>\n<p><p><code>defaultdict<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u5de5\u5382\u51fd\u6570\uff0c\u7528\u4e8e\u5904\u7406\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u7684\u952e\u3002\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u5d4c\u5957\u5b57\u5178\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6dfb\u52a0\u65b0\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2adefaultdict<\/strong><\/h2>\n<p>my_dict = defaultdict(lambda: &#39;Not Present&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u5143\u7d20<\/strong><\/h2>\n<p>my_dict[&#39;name&#39;] = &#39;Alice&#39;<\/p>\n<p>my_dict[&#39;age&#39;] = 25<\/p>\n<h2><strong>\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e<\/strong><\/h2>\n<p>print(my_dict[&#39;address&#39;])  # \u8f93\u51fa\uff1aNot Present<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;address&#39;] = &#39;123 Main St&#39;<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>defaultdict<\/code>\uff0c\u5e76\u4e3a\u5176\u8bbe\u7f6e\u4e86\u9ed8\u8ba4\u503c\u5de5\u5382\u51fd\u6570<code>lambda: &#39;Not Present&#39;<\/code>\u3002\u6dfb\u52a0\u65b0\u5143\u7d20\u540e\uff0c\u5c1d\u8bd5\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u5c06\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u65b9\u62ec\u53f7\u8bed\u6cd5\u3001<code>update()<\/code>\u65b9\u6cd5\u3001<code>setdefault()<\/code>\u65b9\u6cd5\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u548c<code>collections.defaultdict<\/code>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002<strong>\u4f7f\u7528\u65b9\u62ec\u53f7\u8bed\u6cd5\u662f\u6700\u5e38\u89c1\u7684\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9<\/strong>\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u548c\u793a\u4f8b\u5bf9\u60a8\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6709\u6240\u5e2e\u52a9\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\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u5b57\u5178\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528update()\u65b9\u6cd5\u540c\u65f6\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u3002\u53ea\u9700\u4f20\u9012\u4e00\u4e2a\u5305\u542b\u65b0\u5143\u7d20\u7684\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\u3002\u4f8b\u5982\uff1a<code>my_dict.update({&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;})<\/code>\uff0c\u8fd9\u5c06\u5411\u73b0\u6709\u5b57\u5178\u6dfb\u52a0\u4e24\u4e2a\u65b0\u5143\u7d20\u3002<\/p>\n<p><strong>Python\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u786e\u4fdd\u952e\u7684\u552f\u4e00\u6027\u3002\u5982\u679c\u60a8\u6dfb\u52a0\u7684\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u65b0\u7684\u503c\u5c06\u8986\u76d6\u65e7\u7684\u503c\u3002\u6b64\u5916\uff0c\u6ce8\u610f\u503c\u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u662f\u4efb\u4f55Python\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\uff0c\u786e\u4fdd\u6839\u636e\u60a8\u7684\u9700\u6c42\u4f7f\u7528\u5408\u9002\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u6210\u529f\u6dfb\u52a0\u4e86\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u68c0\u67e5\u4e00\u4e2a\u952e\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u3002\u5982\u679c\u8fd4\u56deTrue\uff0c\u5219\u8868\u793a\u8be5\u952e\u5df2\u6210\u529f\u6dfb\u52a0\u3002\u4f8b\u5982\uff1a<code>&#39;new_key&#39; in my_dict<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u8be5\u952e\u662f\u5426\u5728\u5b57\u5178\u4e2d\u3002\u5982\u679c\u60a8\u60f3\u67e5\u770b\u5b57\u5178\u7684\u6240\u6709\u5143\u7d20\uff0c\u76f4\u63a5\u6253\u5370\u5b57\u5178\u4e5f\u53ef\u4ee5\u6709\u6548\u786e\u8ba4\u6dfb\u52a0\u7684\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u65b9\u62ec\u53f7\u8bed\u6cd5\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u4f7f\u7528setdefau [&hellip;]","protected":false},"author":3,"featured_media":1084896,"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\/1084891"}],"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=1084891"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084891\/revisions"}],"predecessor-version":[{"id":1084897,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084891\/revisions\/1084897"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1084896"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1084891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1084891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1084891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}