{"id":1058113,"date":"2024-12-31T15:13:35","date_gmt":"2024-12-31T07:13:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058113.html"},"modified":"2024-12-31T15:13:38","modified_gmt":"2024-12-31T07:13:38","slug":"python%e5%a6%82%e4%bd%95%e5%90%91%e5%ad%97%e5%85%b8%e9%87%8c%e6%b7%bb%e5%8a%a0%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1058113.html","title":{"rendered":"python\u5982\u4f55\u5411\u5b57\u5178\u91cc\u6dfb\u52a0\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4d441dbf-c56e-4b49-a9b5-be6c8b13459e.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5411\u5b57\u5178\u91cc\u6dfb\u52a0\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5411\u5b57\u5178\u91cc\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u952e\u540d\u8d4b\u503c\u8bed\u6cd5\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u901a\u8fc7setdefault()\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u952e\u540d\u8d4b\u503c\u8bed\u6cd5\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u952e\u540d\u8d4b\u503c\u8bed\u6cd5\u662f\u6307\u901a\u8fc7\u6307\u5b9a\u5b57\u5178\u4e2d\u7684\u952e\u540d\u548c\u76f8\u5e94\u7684\u503c\u6765\u6dfb\u52a0\u6216\u66f4\u65b0\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_dict[&#39;new_key&#39;] = &#39;new_value&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u952e\u540d\u8d4b\u503c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u952e\u540d\u8d4b\u503c\u8bed\u6cd5\u662f\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u6216\u66f4\u65b0\u5143\u7d20\u7684\u6700\u76f4\u63a5\u65b9\u5f0f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4e3a\u5b57\u5178\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u6216\u66f4\u65b0\u73b0\u6709\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;city&#39;] = &#39;New York&#39;<\/p>\n<h2><strong>\u66f4\u65b0\u73b0\u6709\u7684\u952e\u503c\u5bf9<\/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\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u5178<code>my_dict<\/code>\uff0c\u7136\u540e\u6211\u4eec\u901a\u8fc7\u952e\u540d\u8d4b\u503c\u8bed\u6cd5\u4e3a\u8be5\u5b57\u5178\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;city&#39;: &#39;New York&#39;<\/code>\uff0c\u5e76\u66f4\u65b0\u4e86\u952e<code>&#39;age&#39;<\/code>\u7684\u503c\u4e3a<code>26<\/code>\u3002\u6700\u540e\u6253\u5370\u5b57\u5178\uff0c\u663e\u793a\u66f4\u65b0\u540e\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528update()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u5141\u8bb8\u60a8\u4e00\u6b21\u6027\u6dfb\u52a0\u6216\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u540c\u65f6\u6dfb\u52a0\u6216\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\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\u548c\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict.update({&#39;city&#39;: &#39;New York&#39;, &#39;age&#39;: 26, &#39;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&#39;: &#39;alice@example.com&#39;})<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u4e3a\u5b57\u5178<code>my_dict<\/code>\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;city&#39;: &#39;New York&#39;<\/code>\u548c<code>&#39;email&#39;: &#39;alice@example.com&#39;<\/code>\uff0c\u5e76\u66f4\u65b0\u4e86\u952e<code>&#39;age&#39;<\/code>\u7684\u503c\u4e3a<code>26<\/code>\u3002\u6700\u540e\u6253\u5370\u5b57\u5178\uff0c\u663e\u793a\u66f4\u65b0\u540e\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7setdefault()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>setdefault()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\uff0c\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u8be5\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u4e0d\u4f1a\u5bf9\u5176\u8fdb\u884c\u66f4\u65b0\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u786e\u4fdd\u67d0\u4e2a\u952e\u5b58\u5728\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\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\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict.setdefault(&#39;city&#39;, &#39;New York&#39;)<\/p>\n<h2><strong>\u5c1d\u8bd5\u4f7f\u7528setdefault()\u65b9\u6cd5\u66f4\u65b0\u73b0\u6709\u7684\u952e\u503c\u5bf9<\/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\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5\u4e3a\u5b57\u5178<code>my_dict<\/code>\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;city&#39;: &#39;New York&#39;<\/code>\uff0c\u5e76\u5c1d\u8bd5\u66f4\u65b0\u952e<code>&#39;age&#39;<\/code>\u7684\u503c\u4e3a<code>26<\/code>\uff0c\u4f46\u7531\u4e8e\u952e<code>&#39;age&#39;<\/code>\u5df2\u7ecf\u5b58\u5728\uff0c\u5176\u503c\u4e0d\u4f1a\u88ab\u66f4\u65b0\u3002\u6700\u540e\u6253\u5370\u5b57\u5178\uff0c\u663e\u793a\u66f4\u65b0\u540e\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u5b57\u5178\u7684\u65b0\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u60a8\u4ece\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u6784\u5efa\u5b57\u5178\uff0c\u5e76\u53ef\u4ee5\u5728\u6784\u5efa\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e00\u4e9b\u952e\u503c\u5bf9<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 25, &#39;New York&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {k: v for k, v in zip(keys, values)}<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u4ece\u4e24\u4e2a\u5217\u8868<code>keys<\/code>\u548c<code>values<\/code>\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>my_dict<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u4ece\u5404\u79cd\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u6784\u5efa\u5b57\u5178\uff0c\u5e76\u5728\u6784\u5efa\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5408\u5e76\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728Python 3.9\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>|<\/code>\u8fd0\u7b97\u7b26\u6765\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u60a8\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u6240\u6709\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e24\u4e2a\u5b57\u5178<\/p>\n<p>dict1 = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>dict2 = {&#39;city&#39;: &#39;New York&#39;, &#39;email&#39;: &#39;alice@example.com&#39;}<\/p>\n<h2><strong>\u4f7f\u7528|\u8fd0\u7b97\u7b26\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>merged_dict = dict1 | dict2<\/p>\n<p>print(merged_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>|<\/code>\u8fd0\u7b97\u7b26\u5c06<code>dict2<\/code>\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\u6dfb\u52a0\u5230<code>dict1<\/code>\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>merged_dict<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528collections.defaultdict<\/h3>\n<\/p>\n<p><p><code>collections.defaultdict<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u5141\u8bb8\u60a8\u4e3a\u5b57\u5178\u4e2d\u7684\u952e\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u5904\u7406\u53ef\u80fd\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u4f7f\u7528defaultdict\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u9ed8\u8ba4\u503c\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>my_dict = defaultdict(list)<\/p>\n<h2><strong>\u6dfb\u52a0\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;name&#39;].append(&#39;Alice&#39;)<\/p>\n<p>my_dict[&#39;age&#39;].append(25)<\/p>\n<p>my_dict[&#39;city&#39;].append(&#39;New York&#39;)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>defaultdict<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u4e3a\u5217\u8868\u7684\u5b57\u5178<code>my_dict<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4e3a\u4e0d\u5b58\u5728\u7684\u952e\u6dfb\u52a0\u9ed8\u8ba4\u503c\uff0c\u5e76\u5728\u9700\u8981\u65f6\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u5b57\u5178\u7684\u952e\u89c6\u56fe<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u952e\u89c6\u56fe\u5141\u8bb8\u60a8\u52a8\u6001\u5730\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u952e\uff0c\u5e76\u5728\u9700\u8981\u65f6\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u52a8\u6001\u64cd\u4f5c\u5b57\u5178\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u52a8\u6001\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u952e<\/strong><\/h2>\n<p>keys_view = my_dict.keys()<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;city&#39;] = &#39;New York&#39;<\/p>\n<h2><strong>\u66f4\u65b0\u73b0\u6709\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;age&#39;] = 26<\/p>\n<p>print(my_dict)<\/p>\n<p>print(keys_view)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>keys()<\/code>\u65b9\u6cd5\u83b7\u53d6\u4e86\u5b57\u5178<code>my_dict<\/code>\u7684\u952e\u89c6\u56fe<code>keys_view<\/code>\uff0c\u7136\u540e\u52a8\u6001\u5730\u4e3a\u5b57\u5178\u6dfb\u52a0\u548c\u66f4\u65b0\u952e\u503c\u5bf9\u3002\u6700\u540e\u6253\u5370\u5b57\u5178\u548c\u952e\u89c6\u56fe\uff0c\u663e\u793a\u66f4\u65b0\u540e\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5b57\u5178\u7684\u503c\u89c6\u56fe\u548c\u9879\u89c6\u56fe<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u503c\u89c6\u56fe\u548c\u9879\u89c6\u56fe\u5141\u8bb8\u60a8\u52a8\u6001\u5730\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u548c\u952e\u503c\u5bf9\uff0c\u5e76\u5728\u9700\u8981\u65f6\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u52a8\u6001\u64cd\u4f5c\u5b57\u5178\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u52a8\u6001\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u548c\u9879<\/strong><\/h2>\n<p>values_view = my_dict.values()<\/p>\n<p>items_view = my_dict.items()<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;city&#39;] = &#39;New York&#39;<\/p>\n<h2><strong>\u66f4\u65b0\u73b0\u6709\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;age&#39;] = 26<\/p>\n<p>print(my_dict)<\/p>\n<p>print(values_view)<\/p>\n<p>print(items_view)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>values()<\/code>\u65b9\u6cd5\u548c<code>items()<\/code>\u65b9\u6cd5\u5206\u522b\u83b7\u53d6\u4e86\u5b57\u5178<code>my_dict<\/code>\u7684\u503c\u89c6\u56fe<code>values_view<\/code>\u548c\u9879\u89c6\u56fe<code>items_view<\/code>\uff0c\u7136\u540e\u52a8\u6001\u5730\u4e3a\u5b57\u5178\u6dfb\u52a0\u548c\u66f4\u65b0\u952e\u503c\u5bf9\u3002\u6700\u540e\u6253\u5370\u5b57\u5178\u3001\u503c\u89c6\u56fe\u548c\u9879\u89c6\u56fe\uff0c\u663e\u793a\u66f4\u65b0\u540e\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u5b57\u5178\u7684\u65b9\u6cd5\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u65f6\uff0c\u4e86\u89e3\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u952e\u540d\u8d4b\u503c\u8bed\u6cd5<\/strong>\uff1a\u6700\u7b80\u5355\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5355\u4e2a\u952e\u503c\u5bf9\u7684\u6dfb\u52a0\u548c\u66f4\u65b0\u3002<\/li>\n<li><strong>update()\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u540c\u65f6\u6dfb\u52a0\u6216\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\u3002<\/li>\n<li><strong>setdefault()\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u786e\u4fdd\u67d0\u4e2a\u952e\u5b58\u5728\u65f6\u4f7f\u7528\uff0c\u4f46\u4e0d\u4f1a\u66f4\u65b0\u73b0\u6709\u7684\u952e\u503c\u5bf9\u3002<\/li>\n<li><strong>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u9002\u7528\u4e8e\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u6784\u5efa\u5b57\u5178\uff0c\u5e76\u5728\u6784\u5efa\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u3002<\/li>\n<li><strong>\u5408\u5e76\u5b57\u5178<\/strong>\uff1a\u9002\u7528\u4e8e\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u6240\u6709\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002<\/li>\n<li><strong>collections.defaultdict<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u53ef\u80fd\u4e0d\u5b58\u5728\u7684\u952e\uff0c\u5e76\u4e3a\u952e\u63d0\u4f9b\u9ed8\u8ba4\u503c\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u5b57\u5178\u7684\u952e\u89c6\u56fe\u3001\u503c\u89c6\u56fe\u548c\u9879\u89c6\u56fe<\/strong>\uff1a\u9002\u7528\u4e8e\u52a8\u6001\u8bbf\u95ee\u548c\u64cd\u4f5c\u5b57\u5178\u4e2d\u7684\u952e\u3001\u503c\u548c\u952e\u503c\u5bf9\u3002<\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\u5c06\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u5b57\u5178\uff0c\u5e76\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u7075\u6d3b\u8fd0\u7528\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\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u65b9\u5f0f\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u3002\u53ea\u9700\u4f7f\u7528\u65b0\u7684\u952e\uff08key\uff09\u548c\u5bf9\u5e94\u7684\u503c\uff08value\uff09\uff0c\u4f8b\u5982\uff1a<code>my_dict[&#39;new_key&#39;] = &#39;new_value&#39;<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u8f7b\u677e\u6269\u5c55\u5b57\u5178\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u679c\u5b57\u5178\u4e2d\u5df2\u7ecf\u5b58\u5728\u76f8\u540c\u7684\u952e\uff0c\u6dfb\u52a0\u65b0\u5143\u7d20\u4f1a\u6709\u4ec0\u4e48\u6548\u679c\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u952e\uff0c\u65b0\u7684\u503c\u5c06\u4f1a\u66ff\u6362\u6389\u65e7\u7684\u503c\u3002\u4f8b\u5982\uff0c<code>my_dict[&#39;existing_key&#39;] = &#39;new_value&#39;<\/code>\u4f1a\u66f4\u65b0\u8fd9\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u800c\u4e0d\u4f1a\u589e\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528update\u65b9\u6cd5\u6279\u91cf\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\u5230\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u3002\u6bd4\u5982\uff0c<code>my_dict.update({&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;})<\/code>\u5c06\u4f1a\u628a<code>key1<\/code>\u548c<code>key2<\/code>\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u9700\u8981\u6279\u91cf\u66f4\u65b0\u5b57\u5178\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5411\u5b57\u5178\u91cc\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u952e\u540d\u8d4b\u503c\u8bed\u6cd5\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u901a\u8fc7setdefaul [&hellip;]","protected":false},"author":3,"featured_media":1058118,"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\/1058113"}],"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=1058113"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058113\/revisions"}],"predecessor-version":[{"id":1058119,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058113\/revisions\/1058119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1058118"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1058113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1058113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1058113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}