{"id":1156562,"date":"2025-01-13T18:17:38","date_gmt":"2025-01-13T10:17:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156562.html"},"modified":"2025-01-13T18:17:53","modified_gmt":"2025-01-13T10:17:53","slug":"python%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e5%b5%8c%e5%a5%97%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1156562.html","title":{"rendered":"python\u5b57\u5178\u5982\u4f55\u5d4c\u5957\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25195009\/47f69a89-e35d-4d01-9a03-d37f610061ae.webp\" alt=\"python\u5b57\u5178\u5982\u4f55\u5d4c\u5957\u5b57\u5178\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e00\u4e2a\u5b57\u5178\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5b57\u5178\u7684\u503c\u6765\u5d4c\u5957\u5b57\u5178<\/strong>\u3001<strong>\u53ef\u4ee5\u901a\u8fc7\u591a\u5c42\u5b57\u5178\u7684\u65b9\u5f0f\u7ec4\u7ec7\u548c\u8bbf\u95ee\u6570\u636e<\/strong>\u3001<strong>\u901a\u8fc7\u4f7f\u7528\u952e\u6765\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u7684\u503c<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u5b57\u5178\u7684\u5d4c\u5957\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u5d4c\u5957\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u5178\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5b57\u5178\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_dict = {<\/p>\n<p>    &quot;key1&quot;: {<\/p>\n<p>        &quot;subkey1&quot;: &quot;value1&quot;,<\/p>\n<p>        &quot;subkey2&quot;: &quot;value2&quot;<\/p>\n<p>    },<\/p>\n<p>    &quot;key2&quot;: {<\/p>\n<p>        &quot;subkey3&quot;: &quot;value3&quot;,<\/p>\n<p>        &quot;subkey4&quot;: &quot;value4&quot;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>nested_dict<\/code>\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u503c\u90fd\u662f\u53e6\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u7684\u503c<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value1 = nested_dict[&quot;key1&quot;][&quot;subkey1&quot;]<\/p>\n<p>print(value1)  # \u8f93\u51fa &quot;value1&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7<code>key1<\/code>\u8bbf\u95ee\u5230\u7b2c\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178\uff0c\u7136\u540e\u901a\u8fc7<code>subkey1<\/code>\u8bbf\u95ee\u5230\u8be5\u5b57\u5178\u4e2d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4fee\u6539\u5d4c\u5957\u5b57\u5178\u7684\u503c<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u4fee\u6539\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_dict[&quot;key1&quot;][&quot;subkey1&quot;] = &quot;new_value1&quot;<\/p>\n<p>print(nested_dict[&quot;key1&quot;][&quot;subkey1&quot;])  # \u8f93\u51fa &quot;new_value1&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06<code>subkey1<\/code>\u7684\u503c\u4ece<code>value1<\/code>\u4fee\u6539\u4e3a<code>new_value1<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6dfb\u52a0\u548c\u5220\u9664\u5d4c\u5957\u5b57\u5178\u7684\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u50cf\u64cd\u4f5c\u666e\u901a\u5b57\u5178\u4e00\u6837\uff0c\u6dfb\u52a0\u6216\u5220\u9664\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u5143\u7d20<\/p>\n<p>nested_dict[&quot;key3&quot;] = {&quot;subkey5&quot;: &quot;value5&quot;}<\/p>\n<p>print(nested_dict)<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>del nested_dict[&quot;key2&quot;][&quot;subkey4&quot;]<\/p>\n<p>print(nested_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5b57\u5178\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u5d4c\u5957\u5b57\u5178\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ec4\u7ec7\u548c\u8bbf\u95ee\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_data = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;user1&quot;: {<\/p>\n<p>        &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>        &quot;age&quot;: 30,<\/p>\n<p>        &quot;address&quot;: {<\/p>\n<p>            &quot;city&quot;: &quot;New York&quot;,<\/p>\n<p>            &quot;zipcode&quot;: &quot;10001&quot;<\/p>\n<p>        }<\/p>\n<p>    },<\/p>\n<p>    &quot;user2&quot;: {<\/p>\n<p>        &quot;name&quot;: &quot;Jane&quot;,<\/p>\n<p>        &quot;age&quot;: 25,<\/p>\n<p>        &quot;address&quot;: {<\/p>\n<p>            &quot;city&quot;: &quot;San Francisco&quot;,<\/p>\n<p>            &quot;zipcode&quot;: &quot;94101&quot;<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<h2><strong>\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>data = json.loads(json_data)<\/p>\n<h2><strong>\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>print(data[&quot;user1&quot;][&quot;address&quot;][&quot;city&quot;])  # \u8f93\u51fa &quot;New York&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u5e76\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u904d\u5386\u5d4c\u5957\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u9700\u8981\u904d\u5386\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u548c\u503c\u3002\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_nested_dict(d, indent=0):<\/p>\n<p>    for key, value in d.items():<\/p>\n<p>        print(&quot;  &quot; * indent + str(key))<\/p>\n<p>        if isinstance(value, dict):<\/p>\n<p>            print_nested_dict(value, indent + 1)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;  &quot; * (indent + 1) + str(value))<\/p>\n<p>print_nested_dict(nested_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570<code>print_nested_dict<\/code>\u6765\u904d\u5386\u5d4c\u5957\u5b57\u5178\uff0c\u5e76\u4ee5\u5c42\u6b21\u7ed3\u6784\u7684\u5f62\u5f0f\u6253\u5370\u952e\u548c\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5b57\u5178\u662fPython\u4e2d\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u4e00\u79cd\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7\u5d4c\u5957\u5b57\u5178\uff0c\u4f60\u53ef\u4ee5\u7ec4\u7ec7\u548c\u8bbf\u95ee\u591a\u5c42\u6b21\u7684\u6570\u636e\u7ed3\u6784\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u80fd\u591f\u638c\u63e1\u521b\u5efa\u3001\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u6dfb\u52a0\u3001\u5220\u9664\u548c\u904d\u5386\u5d4c\u5957\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\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\u5d4c\u5957\u53e6\u4e00\u4e2a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5d4c\u5957\u5b57\u5178\u610f\u5473\u7740\u5728\u4e00\u4e2a\u5b57\u5178\u7684\u503c\u4e2d\u5305\u542b\u53e6\u4e00\u4e2a\u5b57\u5178\u3002\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u5e76\u5c06\u53e6\u4e00\u4e2a\u5b57\u5178\u4f5c\u4e3a\u5176\u952e\u7684\u503c\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">outer_dict = {\n    &#39;key1&#39;: {&#39;inner_key1&#39;: &#39;value1&#39;, &#39;inner_key2&#39;: &#39;value2&#39;},\n    &#39;key2&#39;: {&#39;inner_key3&#39;: &#39;value3&#39;}\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>outer_dict<\/code>\u5305\u542b\u4e24\u4e2a\u952e\uff0c<code>key1<\/code>\u548c<code>key2<\/code>\uff0c\u5b83\u4eec\u7684\u503c\u5206\u522b\u662f\u53e6\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<p><strong>\u5d4c\u5957\u5b57\u5178\u7684\u8bbf\u95ee\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u7684\u503c\u65f6\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u591a\u4e2a\u952e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8bbf\u95ee<code>inner_key1<\/code>\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">value = outer_dict[&#39;key1&#39;][&#39;inner_key1&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de<code>&#39;value1&#39;<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u9010\u5c42\u6df1\u5165\u5b57\u5178\u7ed3\u6784\uff0c\u83b7\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u5d4c\u5957\u5b57\u5178\u4e2d\u5982\u4f55\u6dfb\u52a0\u6216\u4fee\u6539\u503c\uff1f<\/strong><br \/>\u8981\u5728\u5d4c\u5957\u5b57\u5178\u4e2d\u6dfb\u52a0\u6216\u4fee\u6539\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u952e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u5230<code>key1<\/code>\u7684\u5b57\u5178\u4e2d\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">outer_dict[&#39;key1&#39;][&#39;inner_key4&#39;] = &#39;value4&#39;\n<\/code><\/pre>\n<p>\u5982\u679c\u60a8\u60f3\u4fee\u6539<code>inner_key2<\/code>\u7684\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\uff1a<\/p>\n<pre><code class=\"language-python\">outer_dict[&#39;key1&#39;][&#39;inner_key2&#39;] = &#39;new_value2&#39;\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u66f4\u65b0\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e00\u4e2a\u5b57\u5178\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5b57\u5178\u7684\u503c\u6765\u5d4c\u5957\u5b57\u5178\u3001\u53ef\u4ee5\u901a\u8fc7\u591a\u5c42\u5b57\u5178\u7684\u65b9\u5f0f\u7ec4\u7ec7\u548c\u8bbf\u95ee\u6570\u636e\u3001\u901a\u8fc7\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1156579,"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\/1156562"}],"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=1156562"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156562\/revisions"}],"predecessor-version":[{"id":1156583,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156562\/revisions\/1156583"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156579"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}