{"id":973451,"date":"2024-12-27T05:57:01","date_gmt":"2024-12-26T21:57:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/973451.html"},"modified":"2024-12-27T05:57:03","modified_gmt":"2024-12-26T21:57:03","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e9%94%ae%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/973451.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8bbe\u7f6e\u952e\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195624\/ec2ba413-767e-48e4-9108-b94d41958230.webp\" alt=\"python\u4e2d\u5982\u4f55\u8bbe\u7f6e\u952e\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u7f6e\u952e\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528<code>dict<\/code>\u51fd\u6570\u3001\u901a\u8fc7<code>setdefault<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002<\/strong> \u4f7f\u7528\u5b57\u5178\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u952e\u76f4\u63a5\u8bbe\u7f6e\u5bf9\u5e94\u7684\u503c\u3002<code>dict<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u5b57\u5178\u5e76\u521d\u59cb\u5316\u952e\u503c\u5bf9\u3002<code>setdefault<\/code>\u65b9\u6cd5\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u5e76\u8fd4\u56de\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8bbe\u7f6e\u952e\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u8bbe\u7f6e\u952e\u503c<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u5178\uff08dictionary\uff09\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u3001\u65e0\u5e8f\u7684\u952e\u503c\u5bf9\u96c6\u5408\u3002\u5b57\u5178\u4e2d\u6bcf\u4e2a\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u901a\u5e38\u4f7f\u7528\u5b57\u7b26\u4e32\u6216\u6570\u5b57\u4f5c\u4e3a\u952e\u3002\u8981\u5728\u5b57\u5178\u4e2d\u8bbe\u7f6e\u952e\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u952e\u6765\u8d4b\u503c\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>\u8bbe\u7f6e\u952e\u503c<\/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>\u6253\u5370\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u5b57\u5178<code>my_dict<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>my_dict[&#39;name&#39;] = &#39;Alice&#39;<\/code>\u548c<code>my_dict[&#39;age&#39;] = 25<\/code>\u8bbe\u7f6e\u4e86\u4e24\u4e2a\u952e\u503c\u5bf9\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u975e\u5e38\u9002\u5408\u521d\u59cb\u5316\u6216\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u67d0\u4e9b\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>dict<\/code>\u51fd\u6570\u521b\u5efa\u548c\u8bbe\u7f6e\u952e\u503c<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528\u5b57\u5178\u4e4b\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>dict<\/code>\u51fd\u6570\u6765\u521b\u5efa\u5b57\u5178\u3002<code>dict<\/code>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u7cfb\u5217\u7684\u952e\u503c\u5bf9\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528dict\u51fd\u6570\u521b\u5efa\u5b57\u5178<\/p>\n<p>person = dict(name=&#39;Bob&#39;, age=30, city=&#39;New York&#39;)<\/p>\n<h2><strong>\u6253\u5370\u5b57\u5178<\/strong><\/h2>\n<p>print(person)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>dict<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521d\u59cb\u5316\u5b57\u5178\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u521b\u5efa\u5305\u542b\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u5178\u65f6\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u66f4\u7b80\u6d01\u548c\u53ef\u8bfb\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>setdefault<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u9ed8\u8ba4\u503c<\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u7684<code>setdefault<\/code>\u65b9\u6cd5\u5141\u8bb8\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u4e3a\u5176\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u5e76\u8fd4\u56de\u8be5\u503c\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u5176\u5bf9\u5e94\u7684\u503c\u800c\u4e0d\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 28}<\/p>\n<h2><strong>\u4f7f\u7528setdefault\u8bbe\u7f6e\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>default_city = my_dict.setdefault(&#39;city&#39;, &#39;Unknown&#39;)<\/p>\n<h2><strong>\u6253\u5370\u5b57\u5178\u548c\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p>print(default_city)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>setdefault<\/code>\u65b9\u6cd5\u4e3a\u952e<code>&#39;city&#39;<\/code>\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c<code>&#39;Unknown&#39;<\/code>\uff0c\u56e0\u4e3a<code>&#39;city&#39;<\/code>\u5728\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u5728\u907f\u514d\u91cd\u590d\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u7684\u540c\u65f6\uff0c\u786e\u4fdd\u5b57\u5178\u4e2d\u59cb\u7ec8\u6709\u5408\u7406\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u952e\u503c<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u5e38\u5e38\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5d4c\u5957\u5b57\u5178\u6216\u5217\u8868\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bbe\u7f6e\u952e\u503c\u7684\u64cd\u4f5c\u53ef\u80fd\u4f1a\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5d4c\u5957\u5b57\u5178\u7684\u952e\u503c\u8bbe\u7f6e<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u5d4c\u5957\u5b57\u5178\uff0c\u8bbe\u7f6e\u952e\u503c\u65f6\u9700\u8981\u9010\u5c42\u8bbf\u95ee\u5230\u76ee\u6807\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5d4c\u5957\u5b57\u5178<\/p>\n<p>user_data = {<\/p>\n<p>    &#39;user1&#39;: {&#39;name&#39;: &#39;David&#39;, &#39;age&#39;: 35},<\/p>\n<p>    &#39;user2&#39;: {&#39;name&#39;: &#39;Eva&#39;, &#39;age&#39;: 29}<\/p>\n<p>}<\/p>\n<h2><strong>\u8bbe\u7f6e\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u952e\u503c<\/strong><\/h2>\n<p>user_data[&#39;user1&#39;][&#39;city&#39;] = &#39;Los Angeles&#39;<\/p>\n<h2><strong>\u6253\u5370\u5d4c\u5957\u5b57\u5178<\/strong><\/h2>\n<p>print(user_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u901a\u8fc7<code>user_data[&#39;user1&#39;][&#39;city&#39;] = &#39;Los Angeles&#39;<\/code>\u4e3a\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u952e<code>&#39;city&#39;<\/code>\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u65b0\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u786e\u4fdd\u6bcf\u4e00\u5c42\u5b57\u5178\u90fd\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4<code>KeyError<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5217\u8868\u4e2d\u7684\u5b57\u5178<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u5b57\u5178\u662f\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5217\u8868\u6765\u8bbe\u7f6e\u5b57\u5178\u4e2d\u7684\u952e\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868\u4e2d\u7684\u5b57\u5178<\/p>\n<p>people = [<\/p>\n<p>    {&#39;name&#39;: &#39;Frank&#39;, &#39;age&#39;: 40},<\/p>\n<p>    {&#39;name&#39;: &#39;Grace&#39;, &#39;age&#39;: 22}<\/p>\n<p>]<\/p>\n<h2><strong>\u8bbe\u7f6e\u5217\u8868\u4e2d\u5b57\u5178\u7684\u952e\u503c<\/strong><\/h2>\n<p>for person in people:<\/p>\n<p>    person[&#39;status&#39;] = &#39;active&#39;<\/p>\n<h2><strong>\u6253\u5370\u5217\u8868<\/strong><\/h2>\n<p>print(people)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u5217\u8868<code>people<\/code>\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5b57\u5178\u4e2d\u7684\u952e<code>&#39;status&#39;<\/code>\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u65b0\u503c<code>&#39;active&#39;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6279\u91cf\u66f4\u65b0\u6216\u521d\u59cb\u5316\u5217\u8868\u4e2d\u6240\u6709\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5229\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u8bbe\u7f6e\u952e\u503c<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u548c\u64cd\u4f5c\u5b57\u5178\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u6279\u91cf\u8bbe\u7f6e\u952e\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u8bbe\u7f6e\u952e\u503c<\/p>\n<p>squares = {x: x2 for x in range(1, 6)}<\/p>\n<h2><strong>\u6253\u5370\u5b57\u5178<\/strong><\/h2>\n<p>print(squares)<\/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\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178<code>squares<\/code>\uff0c\u5176\u4e2d\u7684\u952e\u662f\u6570\u5b57\uff0c\u503c\u662f\u8be5\u6570\u5b57\u7684\u5e73\u65b9\u3002\u5b57\u5178\u63a8\u5bfc\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u5b57\u5178\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5bf9\u6bcf\u4e2a\u952e\u503c\u5bf9\u8fdb\u884c\u76f8\u540c\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u952e\u503c\u64cd\u4f5c\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>collections.defaultdict<\/code><\/strong>\uff1a\u5f53\u5b57\u5178\u7684\u503c\u662f\u5217\u8868\u3001\u96c6\u5408\u7b49\u53ef\u53d8\u5bf9\u8c61\u65f6\uff0c<code>defaultdict<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u5904\u7406\u952e\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u4f7f\u7528defaultdict<\/strong><\/h2>\n<p>grouped_data = defaultdict(list)<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>grouped_data[&#39;fruits&#39;].append(&#39;apple&#39;)<\/p>\n<p>grouped_data[&#39;fruits&#39;].append(&#39;banana&#39;)<\/p>\n<h2><strong>\u6253\u5370defaultdict<\/strong><\/h2>\n<p>print(grouped_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\n<p><strong>\u907f\u514d\u4f7f\u7528\u53ef\u53d8\u5bf9\u8c61\u4f5c\u4e3a\u5b57\u5178\u952e<\/strong>\uff1a\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u53ef\u54c8\u5e0c\u7684\uff0c\u56e0\u6b64\u50cf\u5217\u8868\u8fd9\u6837\u7684\u53ef\u53d8\u5bf9\u8c61\u4e0d\u80fd\u7528\u4f5c\u5b57\u5178\u952e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8003\u8651\u4f7f\u7528\u4e0d\u53ef\u53d8\u5bf9\u8c61\u4f5c\u4e3a\u5b57\u5178\u952e<\/strong>\uff1a\u5143\u7ec4\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c\u5f53\u9700\u8981\u4f7f\u7528\u591a\u4e2a\u503c\u4f5c\u4e3a\u952e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u542b\u591a\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7406\u89e3\u5b57\u5178\u7684\u6027\u80fd<\/strong>\uff1a\u5b57\u5178\u7684\u67e5\u627e\u548c\u8bbe\u7f6e\u64cd\u4f5c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u4f46\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u5b57\u5178\u662f\u603b\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c\u5728Python\u4e2d\u8bbe\u7f6e\u952e\u503c\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u7ba1\u7406\u548c\u7ec4\u7ec7\u6570\u636e\u3002\u5728\u9009\u62e9\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u5f0f\u65f6\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u7ed3\u6784\u6765\u51b3\u5b9a\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684\u952e\u503c\u8bbe\u7f6e\u65b9\u6cd5\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\u6765\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u5230\u5b57\u5178\u4e2d\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\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\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict[&#39;name&#39;] = &#39;Alice&#39;<\/code>\u6765\u5b9e\u73b0\u3002\u8fd9\u6837\uff0c\u5b57\u5178<code>my_dict<\/code>\u5c31\u4f1a\u66f4\u65b0\u4e3a<code>{&#39;name&#39;: &#39;Alice&#39;}<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u4fee\u6539\u5df2\u6709\u952e\u7684\u503c\uff1f<\/strong><br \/>\u8981\u4fee\u6539\u5b57\u5178\u4e2d\u5df2\u6709\u952e\u7684\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u5c06\u952e\u4e3a<code>&#39;name&#39;<\/code>\u7684\u503c\u4ece<code>&#39;Alice&#39;<\/code>\u6539\u4e3a<code>&#39;Bob&#39;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict[&#39;name&#39;] = &#39;Bob&#39;<\/code>\u3002\u8fd9\u4f1a\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u66f4\u65b0\u4e3a<code>{&#39;name&#39;: &#39;Bob&#39;}<\/code>\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u6dfb\u52a0\u952e\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>if &#39;name&#39; in my_dict:<\/code>\u53ef\u4ee5\u5224\u65ad\u5b57\u5178<code>my_dict<\/code>\u4e2d\u662f\u5426\u5305\u542b\u952e<code>&#39;name&#39;<\/code>\u3002\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u53ef\u4ee5\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u6253\u5370\u51fa\u5bf9\u5e94\u7684\u503c\u6216\u8005\u8fdb\u884c\u5176\u4ed6\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6e\u952e\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528dict\u51fd\u6570\u3001\u901a\u8fc7setdefault\u65b9\u6cd5\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002 \u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":973457,"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\/973451"}],"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=973451"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973451\/revisions"}],"predecessor-version":[{"id":973458,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973451\/revisions\/973458"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/973457"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=973451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=973451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=973451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}