{"id":961282,"date":"2024-12-27T03:57:16","date_gmt":"2024-12-26T19:57:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/961282.html"},"modified":"2024-12-27T03:57:18","modified_gmt":"2024-12-26T19:57:18","slug":"python%e5%a6%82%e4%bd%95%e5%ad%97%e5%85%b8%e7%9a%84%e6%b7%bb%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/961282.html","title":{"rendered":"python\u5982\u4f55\u5b57\u5178\u7684\u6dfb\u52a0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103543\/b118713d-87ac-4f4a-ad01-41513b050521.webp\" alt=\"python\u5982\u4f55\u5b57\u5178\u7684\u6dfb\u52a0\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6dfb\u52a0\u5143\u7d20\u5230\u5b57\u5178\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u65b9\u62ec\u53f7\u8d4b\u503c\u3001\u4f7f\u7528update\u65b9\u6cd5\u3001\u4f7f\u7528setdefault\u65b9\u6cd5\u3002<\/strong> \u5728\u8fd9\u91cc\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u65b9\u62ec\u53f7\u8d4b\u503c\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u662f\u6700\u5e38\u7528\u4e14\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u65b9\u62ec\u53f7\u8d4b\u503c\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u6307\u5b9a\u952e\u548c\u503c\u7684\u65b9\u5f0f\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u5982\u679c\u6307\u5b9a\u7684\u952e\u5728\u5b57\u5178\u4e2d\u5df2\u5b58\u5728\uff0c\u5219\u66f4\u65b0\u5176\u5bf9\u5e94\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u65b0\u7684\u952e\u503c\u5bf9\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u5b57\u5178\u7684\u5404\u79cd\u6dfb\u52a0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u65b9\u62ec\u53f7\u8d4b\u503c<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u65b9\u62ec\u53f7\u8d4b\u503c\u662fPython\u4e2d\u6700\u57fa\u672c\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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 &#39;c&#39;: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u65b0\u952e\u503c\u5bf9<\/strong><\/li>\n<\/ol>\n<p><p>\u5f53\u6211\u4eec\u4f7f\u7528\u65b9\u62ec\u53f7\u8d4b\u503c\u65f6\uff0c\u5982\u679c\u6307\u5b9a\u7684\u952e\u5728\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\uff0cPython\u4f1a\u81ea\u52a8\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u8fd9\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u66f4\u65b0\u5df2\u6709\u952e\u7684\u503c<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u5b57\u5178\u4e2d\u5df2\u7ecf\u5b58\u5728\u8be5\u952e\uff0c\u4f7f\u7528\u65b9\u62ec\u53f7\u8d4b\u503c\u4f1a\u66f4\u65b0\u8be5\u952e\u7684\u503c\u3002\u8fd9\u79cd\u673a\u5236\u4f7f\u5f97\u5b57\u5178\u5728\u5904\u7406\u52a8\u6001\u6570\u636e\u65f6\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528update\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u4e5f\u662f\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u7684\u5e38\u7528\u65b9\u5f0f\u3002<code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u4e00\u4e2a\u952e\u503c\u5bf9\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u5f53\u524d\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5b57\u5178\u7ed9<code>update()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u5230\u76ee\u6807\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>my_dict.update({&#39;c&#39;: 3, &#39;d&#39;: 4})  # \u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u5b57\u5178\uff0c<code>update()<\/code>\u65b9\u6cd5\u8fd8\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u6bd4\u5982\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>my_dict.update([(&#39;c&#39;, 3), (&#39;d&#39;, 4)])  # \u4f7f\u7528\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528setdefault\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>setdefault()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u5178\u4e2d\u63d2\u5165\u952e\u503c\u5bf9\u3002\u5728\u6dfb\u52a0\u952e\u503c\u5bf9\u7684\u540c\u65f6\uff0c\u8be5\u65b9\u6cd5\u8fd8\u8fd4\u56de\u952e\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u4e0d\u5b58\u5728\u7684\u952e<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u6307\u5b9a\u7684\u952e\u4e0d\u5b58\u5728\uff0c<code>setdefault()<\/code>\u4f1a\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u8be5\u952e\uff0c\u5e76\u5c06\u5176\u503c\u8bbe\u4e3a\u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>my_dict.setdefault(&#39;c&#39;, 3)  # \u5982\u679c &#39;c&#39; \u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0 &#39;c&#39;: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u83b7\u53d6\u5df2\u6709\u952e\u7684\u503c<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u6307\u5b9a\u7684\u952e\u5df2\u7ecf\u5b58\u5728\uff0c<code>setdefault()<\/code>\u4f1a\u8fd4\u56de\u8be5\u952e\u7684\u503c\uff0c\u800c\u4e0d\u4f1a\u66f4\u65b0\u5b83\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b57\u5178\u7406\u89e3\u548c\u52a8\u6001\u6dfb\u52a0<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5b57\u5178\u7406\u89e3\u529f\u80fd\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u7528\u7b80\u6d01\u7684\u8bed\u6cd5\u52a8\u6001\u5730\u6784\u5efa\u548c\u6269\u5c55\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b57\u5178\u7406\u89e3<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u5178\u7406\u89e3\u662f\u4e00\u79cd\u751f\u6210\u5b57\u5178\u7684\u7b80\u6d01\u65b9\u5f0f\uff0c\u5141\u8bb8\u5728\u5b9a\u4e49\u5b57\u5178\u65f6\u52a8\u6001\u5730\u6dfb\u52a0\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = {x: x*x for x in range(6)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5<\/strong><\/li>\n<\/ol>\n<p><p>\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\uff0c\u5b57\u5178\u7406\u89e3\u53ef\u4ee5\u7528\u4e8e\u6709\u6761\u4ef6\u5730\u5411\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">even_squares = {x: x*x for x in range(10) if x % 2 == 0}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u8fd0\u7b97\u7b26\u5408\u5e76\u5b57\u5178<\/p>\n<\/p>\n<p><p>Python 3.9\u5f15\u5165\u4e86\u65b0\u7684\u5b57\u5178\u5408\u5e76\u8fd0\u7b97\u7b26<code>|<\/code>\uff0c\u5b83\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5408\u5e76\u5b57\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>|<\/code>\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u6765\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>dict2 = {&#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>merged_dict = dict1 | dict2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u66f4\u65b0\u5b57\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u60f3\u66f4\u65b0\u4e00\u4e2a\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528<code>|=<\/code>\u8fd0\u7b97\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 |= dict2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528collections\u6a21\u5757\u7684defaultdict<\/p>\n<\/p>\n<p><p><code>collections<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>defaultdict<\/code>\u7684\u7c7b\uff0c\u5b83\u662f<code>dict<\/code>\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\u6765\u4e3a\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u7684\u952e\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efadefaultdict<\/strong><\/li>\n<\/ol>\n<p><p><code>defaultdict<\/code>\u9700\u8981\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u51fd\u6570\u5728\u5c1d\u8bd5\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>dd = defaultdict(int)  # \u4f7f\u7528 int \u5de5\u5382\u51fd\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u81ea\u52a8\u6dfb\u52a0\u952e<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c<code>defaultdict<\/code>\u4f1a\u81ea\u52a8\u6dfb\u52a0\u8be5\u952e\uff0c\u5e76\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u5176\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dd[&#39;missing&#39;] += 1  # &#39;missing&#39; \u952e\u81ea\u52a8\u88ab\u6dfb\u52a0\uff0c\u503c\u4e3a 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528dict.fromkeys\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>dict.fromkeys()<\/code>\u662f\u4e00\u4e2a\u7c7b\u65b9\u6cd5\uff0c\u7528\u4e8e\u4ece\u4e00\u4e2a\u952e\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5e76\u4e3a\u6240\u6709\u952e\u8bbe\u7f6e\u76f8\u540c\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u65b0\u5b57\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>dict.fromkeys()<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>new_dict = dict.fromkeys(keys, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6269\u5c55\u5b57\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u867d\u7136<code>fromkeys()<\/code>\u901a\u5e38\u7528\u4e8e\u521b\u5efa\u65b0\u5b57\u5178\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6269\u5c55\u73b0\u6709\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u4f7f\u7528\u65b9\u62ec\u53f7\u8d4b\u503c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u7b80\u5355\u573a\u5408\u3002\u5bf9\u4e8e\u9700\u8981\u540c\u65f6\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u60c5\u51b5\uff0c<code>update()<\/code>\u65b9\u6cd5\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u800c<code>setdefault()<\/code>\u65b9\u6cd5\u5728\u9700\u8981\u540c\u65f6\u68c0\u67e5\u548c\u6dfb\u52a0\u952e\u65f6\u975e\u5e38\u6709\u7528\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5b57\u5178\u64cd\u4f5c\uff0c\u5b57\u5178\u7406\u89e3\u548c<code>defaultdict<\/code>\u63d0\u4f9b\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\u3002\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u4f18\u52bf\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u64cd\u4f5c\u5b57\u5178\uff0c\u63d0\u5347Python\u7f16\u7a0b\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\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\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u65b9\u5f0f\u5c06\u65b0\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict = {}<\/code>\uff0c\u8981\u6dfb\u52a0\u4e00\u4e2a\u952e\u4e3a<code>&#39;name&#39;<\/code>\uff0c\u503c\u4e3a<code>&#39;Alice&#39;<\/code>\u7684\u9879\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict[&#39;name&#39;] = &#39;Alice&#39;<\/code>\u3002\u8fd9\u6837\uff0c<code>my_dict<\/code>\u5c31\u4f1a\u53d8\u4e3a<code>{&#39;name&#39;: &#39;Alice&#39;}<\/code>\u3002<\/p>\n<p><strong>Python\u5b57\u5178\u53ef\u4ee5\u6dfb\u52a0\u91cd\u590d\u7684\u952e\u5417\uff1f<\/strong><br \/>\u5728Python\u5b57\u5178\u4e2d\uff0c\u952e\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u5c1d\u8bd5\u6dfb\u52a0\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u952e\uff0c\u65b0\u7684\u503c\u4f1a\u8986\u76d6\u539f\u6709\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5728\u4e0a\u9762\u7684\u5b57\u5178\u4e2d\u518d\u6b21\u6267\u884c<code>my_dict[&#39;name&#39;] = &#39;Bob&#39;<\/code>\uff0c\u5b57\u5178\u5c06\u66f4\u65b0\u4e3a<code>{&#39;name&#39;: &#39;Bob&#39;}<\/code>\uff0c\u539f\u6765\u7684\u503c<code>&#39;Alice&#39;<\/code>\u5c06\u88ab\u66ff\u6362\u3002<\/p>\n<p><strong>\u5982\u4f55\u6279\u91cf\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u5230\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u5c06\u591a\u4e2a\u952e\u503c\u5bf9\u6279\u91cf\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict = {&#39;name&#39;: &#39;Alice&#39;}<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict.update({&#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;})<\/code>\u5c06\u591a\u4e2a\u952e\u503c\u5bf9\u6dfb\u52a0\u8fdb\u53bb\u3002\u8fd9\u6837\uff0c<code>my_dict<\/code>\u5c06\u66f4\u65b0\u4e3a<code>{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/code>\u3002\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u6279\u91cf\u6dfb\u52a0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6dfb\u52a0\u5143\u7d20\u5230\u5b57\u5178\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u65b9\u62ec\u53f7\u8d4b\u503c\u3001\u4f7f\u7528update\u65b9\u6cd5\u3001\u4f7f\u7528setdefault\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":961289,"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\/961282"}],"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=961282"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961282\/revisions"}],"predecessor-version":[{"id":961293,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961282\/revisions\/961293"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/961289"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=961282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=961282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=961282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}