{"id":947088,"date":"2024-12-26T23:48:27","date_gmt":"2024-12-26T15:48:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/947088.html"},"modified":"2024-12-26T23:48:28","modified_gmt":"2024-12-26T15:48:28","slug":"python%e5%a6%82%e4%bd%95%e5%a2%9e%e5%8a%a0dictionary","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/947088.html","title":{"rendered":"python\u5982\u4f55\u589e\u52a0dictionary"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083008\/447d8d09-9d95-44e5-83b2-fcfc22c67938.webp\" alt=\"python\u5982\u4f55\u589e\u52a0dictionary\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u589e\u52a0dictionary\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u5305\u62ec\uff1a\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u3001update()\u65b9\u6cd5\u3001\u5b57\u5178\u5408\u5e76\u8fd0\u7b97\u7b26\uff08Python 3.9\u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u8fdb\u884c\u589e\u52a0\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u6cd5\u6700\u7b80\u5355\u76f4\u89c2\u3002\u901a\u8fc7\u8d4b\u503c\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u4ee5\u4e0b\u662f\u5bf9\u8d4b\u503c\u8bed\u53e5\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e3a\u5b57\u5178\u6307\u5b9a\u4e00\u4e2a\u65b0\u7684\u952e\uff0c\u7136\u540e\u4e3a\u8fd9\u4e2a\u952e\u8d4b\u503c\u6765\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u66f4\u65b0\u5176\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u589e\u52a0\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u5730\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5411\u5b57\u5178\u4e2d\u589e\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u76f4\u63a5\u8d4b\u503c\u589e\u52a0<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4e3a\u5b57\u5178\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4f1a\u66f4\u65b0\u5176\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\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  # \u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<\/p>\n<p>print(my_dict)    # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c<code>my_dict[&#39;c&#39;] = 3<\/code>\u7684\u65b9\u5f0f\u589e\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;c&#39;: 3<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u589e\u52a0<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u589e\u52a0\u952e\u503c\u5bf9\u4e4b\u524d\u8fdb\u884c\u4e00\u4e9b\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u4ec5\u5728\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u67d0\u4e2a\u952e\u65f6\u624d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>if &#39;c&#39; not in my_dict:<\/p>\n<p>    my_dict[&#39;c&#39;] = 3<\/p>\n<p>print(my_dict)    # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>if &#39;c&#39; not in my_dict<\/code>\u6765\u5224\u65ad\u5b57\u5178\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u952e<code>&#39;c&#39;<\/code>\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u901a\u8fc7\u8d4b\u503c\u8bed\u53e5\u589e\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528update()\u65b9\u6cd5\u589e\u52a0\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5c06\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u952e\u503c\u5bf9\u66f4\u65b0\u5230\u5f53\u524d\u5b57\u5178\u4e2d\u3002\u5b83\u53ef\u4ee5\u6279\u91cf\u589e\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5408\u5e76\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5408\u5e76\u4e24\u4e2a\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\uff0c\u5c06\u53e6\u4e00\u4e2a\u5b57\u5178\u7684\u952e\u503c\u5bf9\u66f4\u65b0\u5230\u5f53\u524d\u5b57\u5178\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>another_dict = {&#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>my_dict.update(another_dict)<\/p>\n<p>print(my_dict)    # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>another_dict<\/code>\u7684\u952e\u503c\u5bf9\u88ab\u66f4\u65b0\u5230<code>my_dict<\/code>\u4e2d\uff0c\u7ed3\u679c\u662f<code>my_dict<\/code>\u5305\u542b\u4e86\u4e24\u4e2a\u5b57\u5178\u7684\u6240\u6709\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ece\u5217\u8868\u6216\u5143\u7ec4\u589e\u52a0<\/strong><\/p>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u6216\u5143\u7ec4\uff09\u4f5c\u4e3a\u53c2\u6570\u3002\u6bcf\u4e2a\u952e\u503c\u5bf9\u5fc5\u987b\u662f\u4e00\u4e2a\u4e8c\u5143\u7ec4\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.update([(&#39;c&#39;, 3), (&#39;d&#39;, 4)])<\/p>\n<p>print(my_dict)    # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868<code>[(&#39;c&#39;, 3), (&#39;d&#39;, 4)]<\/code>\u4e2d\u7684\u952e\u503c\u5bf9\u88ab\u66f4\u65b0\u5230<code>my_dict<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u5408\u5e76\u8fd0\u7b97\u7b26\uff08Python 3.9\u53ca\u4ee5\u4e0a\u7248\u672c\uff09<\/p>\n<\/p>\n<p><p>Python 3.9\u5f15\u5165\u4e86\u65b0\u7684\u5b57\u5178\u5408\u5e76\u8fd0\u7b97\u7b26<code>|<\/code>\uff0c\u7528\u4e8e\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002\u8be5\u8fd0\u7b97\u7b26\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u800c\u4e0d\u6539\u53d8\u539f\u6709\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5b57\u5178\u5408\u5e76\u8fd0\u7b97\u7b26<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u5408\u5e76\u8fd0\u7b97\u7b26<code>|<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u5408\u5e76\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u7684\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>another_dict = {&#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>combined_dict = my_dict | another_dict<\/p>\n<p>print(combined_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_dict<\/code>\u548c<code>another_dict<\/code>\u901a\u8fc7<code>|<\/code>\u8fd0\u7b97\u7b26\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>combined_dict<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u5178\u5408\u5e76\u66f4\u65b0\u8fd0\u7b97\u7b26<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5b57\u5178\u5408\u5e76\u8fd0\u7b97\u7b26\uff0cPython 3.9\u8fd8\u5f15\u5165\u4e86\u5b57\u5178\u5408\u5e76\u66f4\u65b0\u8fd0\u7b97\u7b26<code>|=<\/code>\uff0c\u7528\u4e8e\u5c31\u5730\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>another_dict = {&#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>my_dict |= another_dict<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_dict<\/code>\u88ab\u66f4\u65b0\u4e3a\u5305\u542b<code>another_dict<\/code>\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u589e\u52a0\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u5b57\u5178\u7684\u7b80\u6d01\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6216\u89c4\u5219\u751f\u6210\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u4e8e\u6761\u4ef6\u7684\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u4e8e\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u751f\u6210\u65b0\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u57fa\u4e8e\u73b0\u6709\u5b57\u5178\u7684\u952e\u503c\u5bf9\u751f\u6210\u65b0\u7684\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">old_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>new_dict = {k: v for k, v in old_dict.items() if v &gt; 1}<\/p>\n<p>print(new_dict)  # \u8f93\u51fa: {&#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>new_dict<\/code>\uff0c\u5176\u4e2d\u4ec5\u5305\u542b<code>old_dict<\/code>\u4e2d\u503c\u5927\u4e8e1\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u57fa\u4e8e\u5217\u8868\u751f\u6210\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u8fd8\u53ef\u4ee5\u7528\u4e8e\u4ece\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u751f\u6210\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>print(my_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u548c\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>my_dict<\/code>\uff0c\u5176\u952e\u548c\u503c\u5206\u522b\u6765\u81ea<code>keys<\/code>\u548c<code>values<\/code>\u5217\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5728Python\u4e2d\u589e\u52a0\u5b57\u5178\u7684\u952e\u503c\u5bf9\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u3001<code>update()<\/code>\u65b9\u6cd5\u3001\u5b57\u5178\u5408\u5e76\u8fd0\u7b97\u7b26\u8fd8\u662f\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u7f16\u7a0b\u9700\u6c42\u548cPython\u7248\u672c\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\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict[&#39;\u65b0\u952e&#39;] = &#39;\u65b0\u503c&#39;<\/code>\u6765\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002\u8fd9\u6837\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u521b\u5efa\uff1b\u5982\u679c\u952e\u5df2\u5b58\u5728\uff0c\u5219\u4f1a\u66f4\u65b0\u5176\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p><strong>Python\u5b57\u5178\u662f\u5426\u652f\u6301\u5d4c\u5957\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5b57\u5178\u652f\u6301\u5d4c\u5957\u3002\u53ef\u4ee5\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u53e6\u4e00\u4e2a\u5b57\u5178\u4f5c\u4e3a\u503c\u3002\u8fd9\u79cd\u7ed3\u6784\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u66f4\u590d\u6742\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<code>my_dict = {&#39;\u5916\u90e8&#39;: {&#39;\u5185\u90e8\u952e1&#39;: &#39;\u503c1&#39;, &#39;\u5185\u90e8\u952e2&#39;: &#39;\u503c2&#39;}}<\/code>\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u5d4c\u5957\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\uff1f<\/strong><br \/>\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002\u4f7f\u7528Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>{&lt;strong&gt;dict1, &lt;\/strong&gt;dict2}<\/code>\u7684\u8bed\u6cd5\u6765\u5feb\u901f\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>dict.update()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5b57\u5178\u7684\u5408\u5e76\u3002\u4f8b\u5982\uff0c<code>dict1.update(dict2)<\/code>\u5c06<code>dict2<\/code>\u4e2d\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230<code>dict1<\/code>\u4e2d\u3002\u5982\u679c\u6709\u76f8\u540c\u7684\u952e\uff0c<code>dict2<\/code>\u4e2d\u7684\u503c\u5c06\u8986\u76d6<code>dict1<\/code>\u4e2d\u7684\u5bf9\u5e94\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u589e\u52a0dictionary\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u5305\u62ec\uff1a\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u3001update()\u65b9\u6cd5\u3001\u5b57\u5178\u5408\u5e76 [&hellip;]","protected":false},"author":3,"featured_media":947091,"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\/947088"}],"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=947088"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947088\/revisions"}],"predecessor-version":[{"id":947093,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947088\/revisions\/947093"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/947091"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=947088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=947088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=947088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}