{"id":1043884,"date":"2024-12-31T13:09:57","date_gmt":"2024-12-31T05:09:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1043884.html"},"modified":"2024-12-31T13:10:00","modified_gmt":"2024-12-31T05:10:00","slug":"python%e5%a6%82%e4%bd%95%e5%90%91%e5%ad%97%e5%85%b8%e4%b8%ad%e6%b7%bb%e5%8a%a0%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1043884.html","title":{"rendered":"python\u5982\u4f55\u5411\u5b57\u5178\u4e2d\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\/27f497e7-7169-4daa-8e01-9be009bf9df0.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\" \/><\/p>\n<p><p> <strong>Python\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u5229\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49\u3002\u76f4\u63a5\u6dfb\u52a0\u952e\u503c\u5bf9\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u64cd\u4f5c\u7b80\u5355\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/strong> \u76f4\u63a5\u6dfb\u52a0\u952e\u503c\u5bf9\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u6307\u5b9a\u952e\u548c\u503c\u7684\u5f62\u5f0f\uff0c\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u6dfb\u52a0\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u6dfb\u52a0\u952e\u503c\u5bf9\u662f\u6700\u5e38\u89c1\u3001\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4f60\u53ea\u9700\u8981\u6307\u5b9a\u5b57\u5178\u7684\u952e\u548c\u503c\uff0c\u7136\u540e\u8d4b\u503c\u5373\u53ef\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4f1a\u66f4\u65b0\u5bf9\u5e94\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u65b0\u589e\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict = {}<\/p>\n<h2><strong>\u76f4\u63a5\u6dfb\u52a0\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;key1&#39;] = &#39;value1&#39;<\/p>\n<p>my_dict[&#39;key2&#39;] = &#39;value2&#39;<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528update()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>update()\u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u6027\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\uff0c\u751a\u81f3\u53ef\u4ee5\u5c06\u53e6\u4e00\u4e2a\u5b57\u5178\u7684\u5143\u7d20\u5408\u5e76\u5230\u5f53\u524d\u5b57\u5178\u4e2d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u6279\u91cf\u6dfb\u52a0\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;key1&#39;: &#39;value1&#39;}<\/p>\n<h2><strong>\u4f7f\u7528update()\u65b9\u6cd5\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict.update({&#39;key2&#39;: &#39;value2&#39;, &#39;key3&#39;: &#39;value3&#39;})<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;, &#39;key3&#39;: &#39;value3&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u4fee\u6539\u5b57\u5178\u3002\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e00\u5b9a\u7684\u89c4\u5219\u6279\u91cf\u6dfb\u52a0\u5143\u7d20\u5230\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {f&#39;key{i}&#39;: f&#39;value{i}&#39; for i in range(1, 4)}<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;, &#39;key3&#39;: &#39;value3&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528setdefault()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>setdefault()\u65b9\u6cd5\u53ef\u4ee5\u5728\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\u7684\u540c\u65f6\uff0c\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u952e\u503c\u5bf9\uff1b\u5982\u679c\u952e\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;key1&#39;: &#39;value1&#39;}<\/p>\n<h2><strong>\u4f7f\u7528setdefault()\u65b9\u6cd5\u6dfb\u52a0\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>value = my_dict.setdefault(&#39;key2&#39;, &#39;value2&#39;)<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/strong><\/h2>\n<p>print(value)<\/p>\n<h2><strong>\u8f93\u51fa: value2<\/strong><\/h2>\n<h2><strong>\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\uff0c\u4e0d\u4f1a\u66f4\u65b0\u5b57\u5178<\/strong><\/h2>\n<p>value = my_dict.setdefault(&#39;key1&#39;, &#39;new_value1&#39;)<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/strong><\/h2>\n<p>print(value)<\/p>\n<h2><strong>\u8f93\u51fa: value1<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684defaultdict<\/p>\n<\/p>\n<p><p>defaultdict\u662fcollections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\u6765\u4e3a\u5b57\u5178\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u662f\u5728\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u800c\u4e0d\u662f\u629b\u51faKeyError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2adefaultdict\uff0c\u9ed8\u8ba4\u503c\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>my_dict = defaultdict(list)<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>my_dict[&#39;key1&#39;].append(&#39;value1&#39;)<\/p>\n<p>my_dict[&#39;key2&#39;].append(&#39;value2&#39;)<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;key1&#39;: [&#39;value1&#39;], &#39;key2&#39;: [&#39;value2&#39;]})<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u5b57\u5178\u7684__setitem__()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u7684__setitem__()\u65b9\u6cd5\u662f\u5b57\u5178\u5bf9\u8c61\u5185\u90e8\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u6dfb\u52a0\u6216\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002\u867d\u7136\u4e0d\u5e38\u7528\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u80fd\u4f1a\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict = {}<\/p>\n<h2><strong>\u4f7f\u7528__setitem__()\u65b9\u6cd5\u6dfb\u52a0\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict.__setitem__(&#39;key1&#39;, &#39;value1&#39;)<\/p>\n<p>my_dict.__setitem__(&#39;key2&#39;, &#39;value2&#39;)<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528\u94fe\u5f0f\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u94fe\u5f0f\u64cd\u4f5c\u6765\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\u3002\u867d\u7136Python\u4e0d\u76f4\u63a5\u652f\u6301\u94fe\u5f0f\u64cd\u4f5c\u6dfb\u52a0\u5143\u7d20\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u5c01\u88c5\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict = {}<\/p>\n<h2><strong>\u5c01\u88c5\u4e00\u4e2a\u6dfb\u52a0\u5143\u7d20\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def add_item(d, key, value):<\/p>\n<p>    d[key] = value<\/p>\n<p>    return d<\/p>\n<h2><strong>\u94fe\u5f0f\u64cd\u4f5c\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>my_dict = add_item(my_dict, &#39;key1&#39;, &#39;value1&#39;)<\/p>\n<p>my_dict = add_item(my_dict, &#39;key2&#39;, &#39;value2&#39;)<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528\u5b57\u5178\u7684fromkeys()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>fromkeys()\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u5b57\u5178\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e3a\u6240\u6709\u7684\u952e\u8bbe\u7f6e\u76f8\u540c\u7684\u503c\u3002\u867d\u7136\u8fd9\u4e2a\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u521b\u5efa\u5b57\u5178\uff0c\u4f46\u4e5f\u53ef\u4ee5\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u7528\u4e8e\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528fromkeys()\u65b9\u6cd5\u521b\u5efa\u5b57\u5178<\/p>\n<p>keys = [&#39;key1&#39;, &#39;key2&#39;, &#39;key3&#39;]<\/p>\n<p>value = &#39;common_value&#39;<\/p>\n<p>my_dict = dict.fromkeys(keys, value)<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: &#39;common_value&#39;, &#39;key2&#39;: &#39;common_value&#39;, &#39;key3&#39;: &#39;common_value&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u4f7f\u7528\u5b57\u5178\u7684merge\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u5728Python 3.9\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u652f\u6301\u4f7f\u7528|\u8fd0\u7b97\u7b26\u8fdb\u884c\u5408\u5e76\u3002\u8fd9\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5c06\u4e24\u4e2a\u5b57\u5178\u5408\u5e76\uff0c\u7c7b\u4f3c\u4e8eupdate()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u5b57\u5178<\/p>\n<p>dict1 = {&#39;key1&#39;: &#39;value1&#39;}<\/p>\n<p>dict2 = {&#39;key2&#39;: &#39;value2&#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<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u3002<strong>\u76f4\u63a5\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u5229\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528setdefault()\u65b9\u6cd5<\/strong>\u7b49\uff0c\u90fd\u662f\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u505a\u51fa\u66f4\u660e\u667a\u7684\u9009\u62e9\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u8ba9\u4f60\u5bf9Python\u5b57\u5178\u7684\u64cd\u4f5c\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff0c\u5e76\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6280\u5de7\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\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528update()\u65b9\u6cd5\u5411\u5b57\u5178\u4e2d\u6279\u91cf\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5c06\u4e00\u4e2a\u5b57\u5178\u5408\u5e76\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}\nnew_entries = {&#39;c&#39;: 3, &#39;d&#39;: 4}\nmy_dict.update(new_entries)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0cmy_dict\u5c06\u5305\u542b{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}\u3002<\/p>\n<p><strong>\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u5d4c\u5957\u5b57\u5178\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u5178\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5b57\u5178\u7684\u503c\uff0c\u4ece\u800c\u521b\u5efa\u5d4c\u5957\u5b57\u5178\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1}\nmy_dict[&#39;b&#39;] = {&#39;c&#39;: 3, &#39;d&#39;: 4}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cmy_dict\u7684\u7ed3\u6784\u5c06\u4e3a{&#39;a&#39;: 1, &#39;b&#39;: {&#39;c&#39;: 3, &#39;d&#39;: 4}}\u3002\u5d4c\u5957\u5b57\u5178\u53ef\u4ee5\u5e2e\u52a9\u7ec4\u7ec7\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u5b57\u5178\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u67d0\u4e2a\u952e\uff1f<\/strong><br \/>\u5728\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u4e4b\u524d\uff0c\u68c0\u67e5\u952e\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u53ef\u4ee5\u4f7f\u7528in\u5173\u952e\u5b57\u6765\u5224\u65ad\uff1a  <\/p>\n<pre><code class=\"language-python\">if &#39;key&#39; not in my_dict:\n    my_dict[&#39;key&#39;] = &#39;value&#39;\n<\/code><\/pre>\n<p>\u5982\u679c&#39;key&#39;\u4e0d\u5b58\u5728\u4e8emy_dict\u4e2d\uff0c\u60a8\u5c31\u53ef\u4ee5\u5b89\u5168\u5730\u6dfb\u52a0\u5b83\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u907f\u514d\u8986\u76d6\u5df2\u6709\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u5229\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49\u3002\u76f4\u63a5 [&hellip;]","protected":false},"author":3,"featured_media":1043902,"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\/1043884"}],"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=1043884"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043884\/revisions"}],"predecessor-version":[{"id":1043904,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043884\/revisions\/1043904"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1043902"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1043884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1043884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1043884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}