{"id":1151401,"date":"2025-01-13T17:13:04","date_gmt":"2025-01-13T09:13:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1151401.html"},"modified":"2025-01-13T17:13:07","modified_gmt":"2025-01-13T09:13:07","slug":"python%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e5%ad%97%e5%85%b8%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1151401.html","title":{"rendered":"python\u5982\u4f55\u6dfb\u52a0\u5b57\u5178\u4e2d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181634\/b2c52f43-f316-4ee0-b613-33cfa6a9816f.webp\" alt=\"python\u5982\u4f55\u6dfb\u52a0\u5b57\u5178\u4e2d\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u6dfb\u52a0\u5b57\u5178\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<strong>\u8d4b\u503c\u65b9\u6cd5<\/strong>\u3001<strong><code>update()<\/code>\u65b9\u6cd5<\/strong>\u3001\u4ee5\u53ca<strong>\u5b57\u5178\u63a8\u5bfc<\/strong>\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5728\u8fd9\u5176\u4e2d\uff0c\u8d4b\u503c\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u548c\u7b80\u5355\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u6765\u66f4\u65b0\u5b57\u5178\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8d4b\u503c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8d4b\u503c\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4f1a\u66f4\u65b0\u8be5\u952e\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\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<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u66f4\u65b0\u5df2\u6709\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;age&#39;] = 30<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001<code>update()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u6dfb\u52a0\u6216\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u952e\u503c\u5bf9\u7684\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u8fd9\u4e9b\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u539f\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 = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528\u53e6\u4e00\u4e2a\u5b57\u5178\u66f4\u65b0<\/strong><\/h2>\n<p>new_data = {&#39;city&#39;: &#39;New York&#39;, &#39;age&#39;: 30}<\/p>\n<p>my_dict.update(new_data)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u4f7f\u7528\u952e\u503c\u5bf9\u7684\u8fed\u4ee3\u5bf9\u8c61\u66f4\u65b0<\/strong><\/h2>\n<p>my_dict.update([(&#39;hobby&#39;, &#39;p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nting&#39;)])<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;, &#39;hobby&#39;: &#39;painting&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5b57\u5178\u63a8\u5bfc<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u662f\u4e00\u79cd\u9ad8\u6548\u4e14\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u6216\u66f4\u65b0\u5b57\u5178\u3002\u5728\u5b57\u5178\u63a8\u5bfc\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u952e\u503c\u5bf9\u7684\u751f\u6210\u89c4\u5219\u6765\u521b\u5efa\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>new_dict = {my_dict, &#39;city&#39;: &#39;New York&#39;, &#39;age&#39;: 30}<\/p>\n<p>print(new_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u5408\u5e76\u4e24\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>another_dict = {&#39;hobby&#39;: &#39;painting&#39;, &#39;profession&#39;: &#39;artist&#39;}<\/p>\n<p>merged_dict = {&lt;strong&gt;my_dict, &lt;\/strong&gt;another_dict}<\/p>\n<p>print(merged_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;hobby&#39;: &#39;painting&#39;, &#39;profession&#39;: &#39;artist&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001<code>setdefault()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>setdefault()<\/code>\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u6307\u5b9a\u952e\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u6dfb\u52a0\u8be5\u952e\u5e76\u5c06\u5176\u503c\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5e38\u7528\u4e8e\u5728\u8bbf\u95ee\u5b57\u5178\u7684\u540c\u65f6\u521d\u59cb\u5316\u5b57\u5178\u4e2d\u7684\u67d0\u4e9b\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\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\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>city = my_dict.setdefault(&#39;city&#39;, &#39;New York&#39;)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>print(city)  # \u8f93\u51fa: New York<\/p>\n<h2><strong>\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4e0d\u4f1a\u66f4\u65b0<\/strong><\/h2>\n<p>age = my_dict.setdefault(&#39;age&#39;, 30)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>print(age)  # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u5faa\u73af\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u901a\u8fc7\u5faa\u73af\u6765\u52a8\u6001\u6dfb\u52a0\u6216\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5904\u7406\u4ece\u6587\u4ef6\u6216\u5916\u90e8\u6570\u636e\u6e90\u8bfb\u53d6\u7684\u6570\u636e\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\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>\u6a21\u62df\u4ece\u6570\u636e\u6e90\u8bfb\u53d6\u7684\u6570\u636e<\/strong><\/h2>\n<p>data = [(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 25), (&#39;city&#39;, &#39;New York&#39;)]<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u6dfb\u52a0\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in data:<\/p>\n<p>    my_dict[key] = value<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>collections.defaultdict<\/code><\/h3>\n<\/p>\n<p><p><code>collections.defaultdict<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u5b57\u5178\u7c7b\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u4e3a\u4e0d\u5b58\u5728\u7684\u952e\u63d0\u4f9b\u9ed8\u8ba4\u503c\u6765\u7b80\u5316\u5b57\u5178\u64cd\u4f5c\u3002\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u9ed8\u8ba4\u5de5\u5382\u51fd\u6570\uff0c<code>defaultdict<\/code>\u4f1a\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u81ea\u52a8\u521b\u5efa\u65b0\u7684\u952e\u503c\u5bf9\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\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;names&#39;].append(&#39;Alice&#39;)<\/p>\n<p>my_dict[&#39;names&#39;].append(&#39;Bob&#39;)<\/p>\n<p>my_dict[&#39;ages&#39;].append(25)<\/p>\n<p>my_dict[&#39;ages&#39;].append(30)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;names&#39;: [&#39;Alice&#39;, &#39;Bob&#39;], &#39;ages&#39;: [25, 30]})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4ece\u7528\u6237\u8f93\u5165\u83b7\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4ece\u7528\u6237\u8f93\u5165\u83b7\u53d6\u6570\u636e\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u9700\u6c42\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>\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    key = input(&quot;Enter key (or &#39;stop&#39; to end): &quot;)<\/p>\n<p>    if key == &#39;stop&#39;:<\/p>\n<p>        break<\/p>\n<p>    value = input(&quot;Enter value: &quot;)<\/p>\n<p>    my_dict[key] = value<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528<code>dict<\/code>\u6784\u9020\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>dict<\/code>\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u952e\u503c\u5bf9\u7684\u8fed\u4ee3\u5bf9\u8c61\u6765\u521b\u5efa\u6216\u66f4\u65b0\u5b57\u5178\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u6765\u7b80\u6d01\u5730\u6784\u9020\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = dict(name=&#39;Alice&#39;, age=25)<\/p>\n<h2><strong>\u66f4\u65b0\u5b57\u5178<\/strong><\/h2>\n<p>new_data = dict(city=&#39;New York&#39;, age=30)<\/p>\n<p>my_dict.update(new_data)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b57\u5178\u7684\u5d4c\u5957\u66f4\u65b0<\/h3>\n<\/p>\n<p><p>\u5728\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5b57\u5178\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6\u5b57\u5178\u4e2d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u7684\u65b9\u6cd5\u6765\u66f4\u65b0\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178<\/p>\n<p>my_dict = {&#39;person&#39;: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}}<\/p>\n<h2><strong>\u66f4\u65b0\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;person&#39;][&#39;city&#39;] = &#39;New York&#39;<\/p>\n<p>my_dict[&#39;person&#39;][&#39;age&#39;] = 30<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;person&#39;: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5b57\u5178\u5408\u5e76\u7684\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u524d\u9762\u63d0\u5230\u7684\u65b9\u6cd5\uff0cPython 3.9\u53ca\u4ee5\u4e0a\u7248\u672c\u8fd8\u5f15\u5165\u4e86\u65b0\u7684\u5408\u5e76\u5b57\u5178\u7684\u8bed\u6cd5\uff0c\u5373\u4f7f\u7528<code>|<\/code>\u8fd0\u7b97\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\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;age&#39;: 30}<\/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)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6dfb\u52a0\u5b57\u5178\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u8d4b\u503c\u65b9\u6cd5<\/strong>\u3001<strong><code>update()<\/code>\u65b9\u6cd5<\/strong>\u3001<strong>\u5b57\u5178\u63a8\u5bfc<\/strong>\u3001<strong><code>setdefault()<\/code>\u65b9\u6cd5<\/strong>\u3001<strong>\u7ed3\u5408\u5faa\u73af\u8bed\u53e5<\/strong>\u3001<strong>\u4f7f\u7528<code>collections.defaultdict<\/code><\/strong>\u3001<strong>\u4ece\u7528\u6237\u8f93\u5165\u83b7\u53d6\u6570\u636e<\/strong>\u3001<strong>\u4f7f\u7528<code>dict<\/code>\u6784\u9020\u51fd\u6570<\/strong>\u3001<strong>\u5b57\u5178\u7684\u5d4c\u5957\u66f4\u65b0<\/strong>\u3001\u4ee5\u53ca<strong>\u5b57\u5178\u5408\u5e76\u7684\u5176\u4ed6\u65b9\u6cd5<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5408\u7406\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u5927\u5927\u63d0\u9ad8\u4f60\u7684\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\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\u7684\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u8bed\u53e5\u5411\u5b57\u5178\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/code>\uff0c\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;city&#39;: &#39;New York&#39;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict[&#39;city&#39;] = &#39;New York&#39;<\/code>\u3002\u8fd9\u6837\uff0c<code>my_dict<\/code>\u5c06\u66f4\u65b0\u4e3a<code>{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/code>\u3002<\/p>\n<p><strong>\u5982\u679c\u6dfb\u52a0\u7684\u952e\u5df2\u7ecf\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5f53\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u952e\u65f6\uff0c\u539f\u6709\u7684\u503c\u5c06\u88ab\u65b0\u7684\u503c\u8986\u76d6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6267\u884c<code>my_dict[&#39;age&#39;] = 30<\/code>\uff0c\u5b57\u5178\u4e2d\u7684<code>&#39;age&#39;<\/code>\u952e\u7684\u503c\u5c06\u4ece25\u53d8\u4e3a30\uff0c\u7ed3\u679c\u4e3a<code>{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/code>\u3002\u56e0\u6b64\uff0c\u5728\u6dfb\u52a0\u65b0\u503c\u65f6\uff0c\u8981\u6ce8\u610f\u952e\u7684\u552f\u4e00\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u6279\u91cf\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u5230\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u6279\u91cf\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u3002\u6bd4\u5982\uff0c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u5e76\u4e14\u60f3\u6dfb\u52a0<code>{&#39;gender&#39;: &#39;female&#39;, &#39;country&#39;: &#39;USA&#39;}<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict.update({&#39;gender&#39;: &#39;female&#39;, &#39;country&#39;: &#39;USA&#39;})<\/code>\u3002\u6267\u884c\u540e\uff0c\u5b57\u5178\u5c06\u66f4\u65b0\u4e3a<code>{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;, &#39;gender&#39;: &#39;female&#39;, &#39;country&#39;: &#39;USA&#39;}<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u9879\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6dfb\u52a0\u5b57\u5178\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u65b9\u6cd5\u3001update()\u65b9\u6cd5\u3001\u4ee5\u53ca\u5b57\u5178\u63a8\u5bfc\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5728\u8fd9\u5176 [&hellip;]","protected":false},"author":3,"featured_media":1151404,"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\/1151401"}],"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=1151401"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151401\/revisions"}],"predecessor-version":[{"id":1151406,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151401\/revisions\/1151406"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1151404"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1151401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1151401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1151401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}