{"id":1148080,"date":"2025-01-13T16:32:50","date_gmt":"2025-01-13T08:32:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148080.html"},"modified":"2025-01-13T16:32:53","modified_gmt":"2025-01-13T08:32:53","slug":"python%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1148080.html","title":{"rendered":"python\u5b57\u5178\u5982\u4f55\u6dfb\u52a0\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25170244\/7046b51f-1e22-40a8-ad33-7a1e9a2b07ed.webp\" alt=\"python\u5b57\u5178\u5982\u4f55\u6dfb\u52a0\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8d4b\u503c\u3001<code>update()<\/code>\u65b9\u6cd5\u3001<code>setdefault()<\/code>\u65b9\u6cd5\u7b49\u65b9\u5f0f\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002<\/strong>\u4f8b\u5982\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u5217\u8868\u8d4b\u503c\u7ed9\u5b57\u5178\u7684\u67d0\u4e2a\u952e\uff0c\u6216\u8005\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u6279\u91cf\u6dfb\u52a0\u952e\u503c\u5bf9\u3002\u8be6\u7ec6\u63cf\u8ff0\u4e00\u4e0b\u8d4b\u503c\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u4e00\u4e2a\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/p>\n<p>my_dict = {}<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict[&#39;my_key&#39;] = my_list<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u5b57\u5178<code>my_dict<\/code>\u548c\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868<code>my_list<\/code>\u3002\u7136\u540e\uff0c\u901a\u8fc7\u5c06\u5217\u8868\u8d4b\u503c\u7ed9\u5b57\u5178\u7684\u952e<code>&#39;my_key&#39;<\/code>\uff0c\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u6700\u7ec8\uff0c\u6253\u5370\u51fa\u7684\u5b57\u5178\u5305\u542b\u4e86\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u5185\u5bb9\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u65b9\u5f0f\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u4ecb\u7ecd\u76f8\u5173\u6ce8\u610f\u4e8b\u9879\u548c\u5b9e\u8df5\u7ecf\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u8d4b\u503c\u65b9\u5f0f\u6dfb\u52a0\u5217\u8868<\/strong><\/h2>\n<p><p><strong>\u901a\u8fc7\u8d4b\u503c\u64cd\u4f5c\u53ef\u4ee5\u5c06\u5217\u8868\u76f4\u63a5\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u8fd9\u662f\u6700\u5e38\u7528\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {}<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict[&#39;my_key&#39;] = my_list<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u5c06\u5217\u8868\u8d4b\u503c\u7ed9\u5b57\u5178\u7684\u952e\u540d<code>&#39;my_key&#39;<\/code>\uff0c\u4ece\u800c\u5c06\u5217\u8868\u6210\u529f\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u64cd\u4f5c\u7b80\u5355\u3001\u6613\u4e8e\u7406\u89e3\u4e14\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u952e\u540d\u552f\u4e00\u6027<\/strong>\uff1a\u5b57\u5178\u4e2d\u7684\u952e\u540d\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u8d4b\u503c\u64cd\u4f5c\u4e2d\u4f7f\u7528\u4e86\u5df2\u7ecf\u5b58\u5728\u7684\u952e\u540d\uff0c\u5219\u4f1a\u8986\u76d6\u539f\u6709\u7684\u503c\u3002<\/li>\n<li><strong>\u5217\u8868\u53ef\u53d8\u6027<\/strong>\uff1a\u5217\u8868\u662f\u53ef\u53d8\u7c7b\u578b\uff0c\u610f\u5473\u7740\u53ef\u4ee5\u5728\u6dfb\u52a0\u5230\u5b57\u5178\u540e\u5bf9\u5217\u8868\u8fdb\u884c\u4fee\u6539\u3002\u5982\u5728\u5b57\u5178\u4e2d\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u539f\u5217\u8868\u4e5f\u4f1a\u968f\u4e4b\u53d8\u5316\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {}<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict[&#39;my_key&#39;] = my_list<\/p>\n<p>my_dict[&#39;my_key&#39;][0] = 100<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;my_key&#39;: [100, 2, 3]}<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [100, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5<\/strong><\/h2>\n<p><p><strong>\u901a\u8fc7<code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6279\u91cf\u6dfb\u52a0\u952e\u503c\u5bf9\uff0c\u5305\u62ec\u5217\u8868\u3002<\/strong><\/p>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u952e\u503c\u5bf9\u66f4\u65b0\u6216\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002\u8fd9\u5bf9\u6211\u4eec\u6279\u91cf\u6dfb\u52a0\u5185\u5bb9\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {}<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict.update({&#39;my_key&#39;: my_list})<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u5c06\u5305\u542b\u5217\u8868\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u952e\u540d\u51b2\u7a81<\/strong>\uff1a<code>update()<\/code>\u65b9\u6cd5\u4f1a\u8986\u76d6\u5177\u6709\u76f8\u540c\u952e\u540d\u7684\u539f\u6709\u503c\u3002<\/li>\n<li><strong>\u53c2\u6570\u7c7b\u578b<\/strong>\uff1a<code>update()<\/code>\u65b9\u6cd5\u63a5\u53d7\u53e6\u4e00\u4e2a\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u63a5\u53d7\u952e\u503c\u5bf9\u5f62\u5f0f\u7684\u53c2\u6570\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {&#39;existing_key&#39;: 10}<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict.update({&#39;my_key&#39;: my_list, &#39;existing_key&#39;: 100})<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;existing_key&#39;: 100, &#39;my_key&#39;: [1, 2, 3]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5<\/strong><\/h2>\n<p><p><strong><code>setdefault()<\/code>\u65b9\u6cd5\u5728\u5b57\u5178\u4e2d\u8bbe\u7f6e\u952e\u503c\u5bf9\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u952e\u548c\u503c\uff0c\u5982\u679c\u952e\u5df2\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u73b0\u6709\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {}<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict.setdefault(&#39;my_key&#39;, my_list)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c<code>setdefault()<\/code>\u65b9\u6cd5\u5728\u5b57\u5178\u4e2d\u8bbe\u7f6e\u952e<code>&#39;my_key&#39;<\/code>\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u5217\u8868\u3002\u5982\u679c\u952e\u5df2\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u73b0\u6709\u503c\uff0c\u800c\u4e0d\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u952e\u540d\u5b58\u5728\u6027<\/strong>\uff1a\u8be5\u65b9\u6cd5\u5728\u952e\u540d\u4e0d\u5b58\u5728\u65f6\u624d\u4f1a\u6dfb\u52a0\u952e\u503c\u5bf9\uff0c\u82e5\u952e\u540d\u5df2\u5b58\u5728\u5219\u4e0d\u4f1a\u8fdb\u884c\u4fee\u6539\u3002<\/li>\n<li><strong>\u65b9\u6cd5\u8fd4\u56de\u503c<\/strong>\uff1a\u8be5\u65b9\u6cd5\u8fd4\u56de\u73b0\u6709\u503c\u6216\u65b0\u6dfb\u52a0\u7684\u503c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {&#39;existing_key&#39;: 10}<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>result = my_dict.setdefault(&#39;existing_key&#39;, 100)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;existing_key&#39;: 10}<\/p>\n<p>print(result)  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528<code>collections.defaultdict<\/code><\/strong><\/h2>\n<p><p><strong><code>defaultdict<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u5b57\u5178\u7c7b\u578b\uff0c\u63d0\u4f9b\u4e86\u4e3a\u4e0d\u5b58\u5728\u7684\u952e\u81ea\u52a8\u751f\u6210\u9ed8\u8ba4\u503c\u7684\u529f\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>my_dict = defaultdict(list)<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict[&#39;my_key&#39;].extend(my_list)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>defaultdict<\/code>\u7684\u9ed8\u8ba4\u503c\u4e3a\u5217\u8868\u7c7b\u578b\uff0c\u56e0\u6b64\u5728\u8bbf\u95ee\u672a\u5b58\u5728\u7684\u952e\u65f6\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u5e76\u53ef\u4ee5\u4f7f\u7528<code>extend()<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u81ea\u52a8\u521d\u59cb\u5316<\/strong>\uff1a<code>defaultdict<\/code>\u4f1a\u81ea\u52a8\u4e3a\u4e0d\u5b58\u5728\u7684\u952e\u521d\u59cb\u5316\u9ed8\u8ba4\u503c\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/li>\n<li><strong>\u7c7b\u578b\u9650\u5236<\/strong>\uff1a\u9700\u8981\u6307\u5b9a\u9ed8\u8ba4\u503c\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528<code>list<\/code>\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u7c7b\u578b\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>my_dict = defaultdict(list)<\/p>\n<p>my_dict[&#39;new_key&#39;].append(4)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;new_key&#39;: [4]})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528<code>dict.setdefault<\/code>\u65b9\u6cd5\u4e0e<code>list.append<\/code>\u65b9\u6cd5\u7ec4\u5408<\/strong><\/h2>\n<p><p><strong>\u901a\u8fc7\u5c06<code>dict.setdefault<\/code>\u65b9\u6cd5\u4e0e<code>list.append<\/code>\u65b9\u6cd5\u7ec4\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5728\u4e0d\u5b58\u5728\u952e\u65f6\u6dfb\u52a0\u65b0\u5217\u8868\uff0c\u5e76\u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {}<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict.setdefault(&#39;my_key&#39;, []).extend(my_list)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>setdefault<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u952e\u503c\u5bf9\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\u5219\u6dfb\u52a0\u4e00\u4e2a\u7a7a\u5217\u8868\u4f5c\u4e3a\u503c\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>extend<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u7684\u503c\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u952e\u503c\u5bf9\u521d\u59cb\u5316<\/strong>\uff1a\u5728\u952e\u4e0d\u5b58\u5728\u65f6\uff0c<code>setdefault<\/code>\u65b9\u6cd5\u4f1a\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868\u4f5c\u4e3a\u503c\u3002<\/li>\n<li><strong>\u7ed3\u5408\u4f7f\u7528<\/strong>\uff1a<code>setdefault<\/code>\u65b9\u6cd5\u548c<code>append<\/code>\u6216<code>extend<\/code>\u65b9\u6cd5\u7ed3\u5408\u4f7f\u7528\uff0c\u80fd\u591f\u7075\u6d3b\u5730\u5904\u7406\u5b57\u5178\u4e2d\u7684\u5217\u8868\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {}<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>for item in my_list:<\/p>\n<p>    my_dict.setdefault(&#39;my_key&#39;, []).append(item)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;my_key&#39;: [1, 2, 3]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528<code>dict<\/code>\u521d\u59cb\u5316\u65f6\u6dfb\u52a0\u5217\u8868<\/strong><\/h2>\n<p><p><strong>\u53ef\u4ee5\u5728\u521b\u5efa\u5b57\u5178\u65f6\u76f4\u63a5\u4e3a\u5176\u6dfb\u52a0\u5305\u542b\u5217\u8868\u7684\u952e\u503c\u5bf9\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict = {&#39;my_key&#39;: my_list}<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u521b\u5efa\u5b57\u5178\u65f6\u76f4\u63a5\u5c06\u952e\u503c\u5bf9\u8bbe\u7f6e\u4e3a\u5305\u542b\u5217\u8868\u7684\u5f62\u5f0f\uff0c\u4ece\u800c\u5728\u5b57\u5178\u521d\u59cb\u5316\u65f6\u5373\u5305\u542b\u6240\u9700\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b57\u5178\u521d\u59cb\u5316<\/strong>\uff1a\u5728\u521b\u5efa\u5b57\u5178\u65f6\u76f4\u63a5\u6dfb\u52a0\u6240\u9700\u7684\u952e\u503c\u5bf9\uff0c\u7b80\u6d01\u660e\u4e86\u3002<\/li>\n<li><strong>\u4e00\u6b21\u6027\u521d\u59cb\u5316<\/strong>\uff1a\u9002\u7528\u4e8e\u5728\u5b57\u5178\u521d\u59cb\u5316\u65f6\u9700\u8981\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list1 = [1, 2, 3]<\/p>\n<p>my_list2 = [4, 5, 6]<\/p>\n<p>my_dict = {&#39;key1&#39;: my_list1, &#39;key2&#39;: my_list2}<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;key1&#39;: [1, 2, 3], &#39;key2&#39;: [4, 5, 6]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u4f7f\u7528<code>dict.update<\/code>\u4e0e<code>dict<\/code>\u5408\u5e76<\/strong><\/h2>\n<p><p><strong>\u53ef\u4ee5\u901a\u8fc7<code>dict.update<\/code>\u65b9\u6cd5\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\uff0c\u5176\u4e2d\u4e00\u4e2a\u5b57\u5178\u5305\u542b\u5217\u8868\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {&#39;existing_key&#39;: 10}<\/p>\n<p>additional_dict = {&#39;my_key&#39;: [1, 2, 3]}<\/p>\n<p>my_dict.update(additional_dict)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u5305\u542b\u5217\u8868\u7684\u5b57\u5178\uff0c\u7136\u540e\u4f7f\u7528<code>update<\/code>\u65b9\u6cd5\u5c06\u5176\u5408\u5e76\u5230\u5df2\u6709\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b57\u5178\u5408\u5e76<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u952e\u540d\u51b2\u7a81<\/strong>\uff1a\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u5177\u6709\u76f8\u540c\u952e\u540d\u7684\u503c\u4f1a\u88ab\u8986\u76d6\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {&#39;existing_key&#39;: 10}<\/p>\n<p>additional_dict1 = {&#39;my_key&#39;: [1, 2, 3]}<\/p>\n<p>additional_dict2 = {&#39;new_key&#39;: [4, 5, 6]}<\/p>\n<p>my_dict.update(additional_dict1)<\/p>\n<p>my_dict.update(additional_dict2)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;existing_key&#39;: 10, &#39;my_key&#39;: [1, 2, 3], &#39;new_key&#39;: [4, 5, 6]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001\u4f7f\u7528\u5faa\u73af\u6dfb\u52a0\u591a\u4e2a\u5217\u8868<\/strong><\/h2>\n<p><p><strong>\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u5c06\u591a\u4e2a\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u6bcf\u4e2a\u5217\u8868\u5bf9\u5e94\u4e0d\u540c\u7684\u952e\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {}<\/p>\n<p>keys = [&#39;key1&#39;, &#39;key2&#39;, &#39;key3&#39;]<\/p>\n<p>lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>for key, lst in zip(keys, lists):<\/p>\n<p>    my_dict[key] = lst<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u952e\u548c\u5217\u8868\uff0c\u5c06\u6bcf\u4e2a\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u5bf9\u5e94\u7684\u952e\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5faa\u73af\u904d\u5386<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u6279\u91cf\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u952e\u5217\u8868\u548c\u503c\u5217\u8868<\/strong>\uff1a\u786e\u4fdd\u952e\u5217\u8868\u548c\u503c\u5217\u8868\u957f\u5ea6\u4e00\u81f4\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_dict = {}<\/p>\n<p>keys = [&#39;key1&#39;, &#39;key2&#39;, &#39;key3&#39;]<\/p>\n<p>lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>for key, lst in zip(keys, lists):<\/p>\n<p>    my_dict[key] = lst<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;key1&#39;: [1, 2, 3], &#39;key2&#39;: [4, 5, 6], &#39;key3&#39;: [7, 8, 9]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e5d\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p><p><strong>\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u5305\u542b\u5217\u8868\u7684\u5b57\u5178\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>keys = [&#39;key1&#39;, &#39;key2&#39;, &#39;key3&#39;]<\/p>\n<p>lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>my_dict = {key: lst for key, lst in zip(keys, lists)}<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u5b57\u5178\uff0c\u5c06\u6bcf\u4e2a\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u5bf9\u5e94\u7684\u952e\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u7b80\u6d01\u4e14\u9ad8\u6548\u5730\u521b\u5efa\u5305\u542b\u5217\u8868\u7684\u5b57\u5178\u3002<\/li>\n<li><strong>\u952e\u5217\u8868\u548c\u503c\u5217\u8868<\/strong>\uff1a\u786e\u4fdd\u952e\u5217\u8868\u548c\u503c\u5217\u8868\u957f\u5ea6\u4e00\u81f4\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>keys = [&#39;key1&#39;, &#39;key2&#39;, &#39;key3&#39;]<\/p>\n<p>lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>my_dict = {key: lst for key, lst in zip(keys, lists)}<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;key1&#39;: [1, 2, 3], &#39;key2&#39;: [4, 5, 6], &#39;key3&#39;: [7, 8, 9]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u3001\u4f7f\u7528<code>dict.fromkeys<\/code>\u65b9\u6cd5<\/strong><\/h2>\n<p><p><strong>\u53ef\u4ee5\u4f7f\u7528<code>dict.fromkeys<\/code>\u65b9\u6cd5\u521b\u5efa\u5b57\u5178\uff0c\u5e76\u5c06\u6240\u6709\u952e\u7684\u9ed8\u8ba4\u503c\u8bbe\u7f6e\u4e3a\u76f8\u540c\u7684\u5217\u8868\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>keys = [&#39;key1&#39;, &#39;key2&#39;, &#39;key3&#39;]<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict = dict.fromkeys(keys, my_list)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528<code>dict.fromkeys<\/code>\u65b9\u6cd5\u521b\u5efa\u5b57\u5178\uff0c\u5e76\u5c06\u6240\u6709\u952e\u7684\u9ed8\u8ba4\u503c\u8bbe\u7f6e\u4e3a\u76f8\u540c\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5171\u4eab\u5217\u8868<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u5c06\u6240\u6709\u952e\u7684\u503c\u8bbe\u7f6e\u4e3a\u540c\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61\uff0c\u4fee\u6539\u5176\u4e2d\u4e00\u4e2a\u952e\u7684\u503c\u4f1a\u5f71\u54cd\u5176\u4ed6\u952e\u7684\u503c\u3002<\/li>\n<li><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u6240\u6709\u952e\u5171\u4eab\u540c\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61\u7684\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>keys = [&#39;key1&#39;, &#39;key2&#39;, &#39;key3&#39;]<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<p>my_dict = dict.fromkeys(keys, my_list)<\/p>\n<p>my_dict[&#39;key1&#39;][0] = 100<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;key1&#39;: [100, 2, 3], &#39;key2&#39;: [100, 2, 3], &#39;key3&#39;: [100, 2, 3]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5b57\u5178\u548c\u5217\u8868\u7684\u64cd\u4f5c\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\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u5b57\u5178\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u952e\u6765\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u5e76\u5411\u5176\u6dfb\u52a0\u4e00\u4e2a\u5217\u8868\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {}\nmy_dict[&#39;my_list&#39;] = [1, 2, 3]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u5178<code>my_dict<\/code>\u5c31\u5305\u542b\u4e00\u4e2a\u952e\u4e3a<code>&#39;my_list&#39;<\/code>\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5728\u5df2\u6709\u7684\u5b57\u5178\u4e2d\u5982\u4f55\u5411\u5217\u8868\u6dfb\u52a0\u65b0\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u5b57\u5178\u4e2d\u5df2\u7ecf\u6709\u4e00\u4e2a\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u8be5\u5217\u8868\u4e2d\u3002\u5047\u8bbe\u5b57\u5178\u5df2\u5b58\u5728\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;my_list&#39;: [1, 2, 3]}\nmy_dict[&#39;my_list&#39;].append(4)\n<\/code><\/pre>\n<p>\u6267\u884c\u540e\uff0c<code>my_dict<\/code>\u4e2d\u7684<code>&#39;my_list&#39;<\/code>\u5c06\u53d8\u4e3a<code>[1, 2, 3, 4]<\/code>\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5728\u5b57\u5178\u7684\u5217\u8868\u4e2d\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5b57\u5178\u7684\u5217\u8868\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002\u60a8\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u5217\u8868\u4e2d\u5b58\u50a8\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u751a\u81f3\u5176\u4ed6\u5b57\u5178\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;mixed_list&#39;: [1, &#39;two&#39;, 3.0, {&#39;key&#39;: &#39;value&#39;}]}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97Python\u5b57\u5178\u975e\u5e38\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8d4b\u503c\u3001update()\u65b9\u6cd5\u3001setdefault()\u65b9\u6cd5\u7b49\u65b9\u5f0f\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u4f8b [&hellip;]","protected":false},"author":3,"featured_media":1148087,"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\/1148080"}],"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=1148080"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148080\/revisions"}],"predecessor-version":[{"id":1148090,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148080\/revisions\/1148090"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1148087"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1148080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1148080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1148080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}