{"id":1057854,"date":"2024-12-31T15:11:29","date_gmt":"2024-12-31T07:11:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057854.html"},"modified":"2024-12-31T15:11:31","modified_gmt":"2024-12-31T07:11:31","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e5%88%97%e8%a1%a8%e6%b7%bb%e5%8a%a0%e5%88%b0%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057854.html","title":{"rendered":"python\u5982\u4f55\u628a\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5e725a7d-0fdc-4d53-be42-d8fe6d23dae7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u628a\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/strong>\u3002\u5e38\u89c1\u7684\u65b9\u5f0f\u6709\uff1a\u76f4\u63a5\u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53ca\u901a\u8fc7\u5faa\u73af\u5c06\u5217\u8868\u7684\u5143\u7d20\u4f9d\u6b21\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002<strong>\u76f4\u63a5\u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c<\/strong>\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5217\u8868\u548c\u5b57\u5178\u4e4b\u95f4\u5b58\u5728\u4e00\u5bf9\u4e00\u7684\u5bf9\u5e94\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u7684\uff0c\u53ea\u9700\u5c06\u5217\u8868\u8d4b\u503c\u7ed9\u5b57\u5178\u4e2d\u7684\u4e00\u4e2a\u952e\u5373\u53ef\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {}<\/p>\n<h2><strong>\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>my_dict[&#39;key1&#39;] = my_list<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: [1, 2, 3, 4, 5]}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u4e00\u4e2a\u503c\uff0c\u5e76\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u8be5\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u751f\u6210\u5b57\u5178\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>my_dict = {i: my_list[i] for i in range(len(my_list))}<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {0: &#39;a&#39;, 1: &#39;b&#39;, 2: &#39;c&#39;, 3: &#39;d&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u5904\u7406\u540e\u518d\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u5faa\u73af\u5c06\u5217\u8868\u7684\u5143\u7d20\u4f9d\u6b21\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5206\u522b\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u4f9d\u6b21\u5c06\u5217\u8868\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {}<\/p>\n<h2><strong>\u901a\u8fc7\u5faa\u73af\u5c06\u5217\u8868\u7684\u5143\u7d20\u4f9d\u6b21\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>for i, value in enumerate(my_list):<\/p>\n<p>    my_dict[f&#39;key{i+1}&#39;] = value<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: 10, &#39;key2&#39;: 20, &#39;key3&#39;: 30, &#39;key4&#39;: 40, &#39;key5&#39;: 50}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u4e3a\u6bcf\u4e2a\u5217\u8868\u5143\u7d20\u6307\u5b9a\u4e0d\u540c\u7684\u952e\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>zip<\/code> \u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5f53\u6211\u4eec\u6709\u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u53e6\u4e00\u4e2a\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>zip<\/code> \u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u5b57\u5178\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u5217\u8868<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 25, &#39;New York&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 zip \u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = dict(zip(keys, values))<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u6709\u4e24\u4e2a\u5bf9\u5e94\u5217\u8868\u7684\u60c5\u51b5\uff0c\u80fd\u591f\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>defaultdict<\/code> \u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c<\/h3>\n<\/p>\n<p><p><code>defaultdict<\/code> \u662f <code>collections<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u80fd\u591f\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u53ef\u4ee5\u4f7f\u7528 <code>defaultdict<\/code> \u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a defaultdict\uff0c\u9ed8\u8ba4\u503c\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>my_dict = defaultdict(list)<\/p>\n<h2><strong>\u5c06\u5217\u8868\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for value in my_list:<\/p>\n<p>    my_dict[&#39;key&#39;].append(value)<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;key&#39;: [1, 2, 3, 4, 5]})<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>defaultdict<\/code> \u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5b57\u5178\u4e2d\u7684\u7f3a\u5931\u952e\uff0c\u5e76\u81ea\u52a8\u521d\u59cb\u5316\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5c06\u5d4c\u5957\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u9700\u8981\u5c06\u5d4c\u5957\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5d4c\u5957\u5217\u8868\uff0c\u5c06\u5176\u5bf9\u5e94\u7684\u503c\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5d4c\u5957\u5217\u8868<\/p>\n<p>nested_list = [[&#39;a&#39;, 1], [&#39;b&#39;, 2], [&#39;c&#39;, 3]]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {}<\/p>\n<h2><strong>\u5c06\u5d4c\u5957\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>for sub_list in nested_list:<\/p>\n<p>    key, value = sub_list<\/p>\n<p>    my_dict[key] = value<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u503c\u6620\u5c04\u5230\u5b57\u5178\u4e2d\u7684\u76f8\u5e94\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528 <code>update<\/code> \u65b9\u6cd5\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/h3>\n<\/p>\n<p><p><code>update<\/code> \u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5c06\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u73b0\u6709\u5b57\u5178\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u5f62\u5f0f\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 <code>update<\/code> \u65b9\u6cd5\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [(&#39;x&#39;, 100), (&#39;y&#39;, 200), (&#39;z&#39;, 300)]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u5f62\u5f0f\u5e76\u6dfb\u52a0\u5230\u73b0\u6709\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>my_dict.update(dict(my_list))<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;x&#39;: 100, &#39;y&#39;: 200, &#39;z&#39;: 300}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>update<\/code> \u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5408\u5e76\u5b57\u5178\u548c\u952e\u503c\u5bf9\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528 <code>setdefault<\/code> \u65b9\u6cd5\u5c06\u5217\u8868\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/h3>\n<\/p>\n<p><p><code>setdefault<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5728\u5b57\u5178\u4e2d\u8bbe\u7f6e\u952e\u7684\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528 <code>setdefault<\/code> \u65b9\u6cd5\u5c06\u5217\u8868\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {}<\/p>\n<h2><strong>\u4f7f\u7528 setdefault \u65b9\u6cd5\u5c06\u5217\u8868\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    my_dict.setdefault(f&#39;key{index+1}&#39;, value)<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;key1&#39;: 1, &#39;key2&#39;: 2, &#39;key3&#39;: 3, &#39;key4&#39;: 4, &#39;key5&#39;: 5}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>setdefault<\/code> \u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u8bbe\u7f6e\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\uff0c\u907f\u514d\u952e\u4e0d\u5b58\u5728\u65f6\u7684\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528 <code>fromkeys<\/code> \u65b9\u6cd5\u5c06\u5217\u8868\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u952e<\/h3>\n<\/p>\n<p><p><code>fromkeys<\/code> \u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u751f\u6210\u4e00\u4e2a\u65b0\u5b57\u5178\uff0c\u5176\u952e\u6765\u81ea\u4e8e\u7ed9\u5b9a\u7684\u5e8f\u5217\uff0c\u503c\u4e3a\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002\u53ef\u4ee5\u4f7f\u7528 <code>fromkeys<\/code> \u65b9\u6cd5\u5c06\u5217\u8868\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 fromkeys \u65b9\u6cd5\u5c06\u5217\u8868\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u952e<\/strong><\/h2>\n<p>my_dict = dict.fromkeys(keys, 0)<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;a&#39;: 0, &#39;b&#39;: 0, &#39;c&#39;: 0, &#39;d&#39;: 0}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>fromkeys<\/code> \u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u901f\u7684\u65b9\u5f0f\u6765\u751f\u6210\u5177\u6709\u76f8\u540c\u9ed8\u8ba4\u503c\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5c06\u5217\u8868\u5143\u7d20\u6dfb\u52a0\u4e3a\u5b57\u5178\u7684\u5d4c\u5957\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5217\u8868\u5143\u7d20\u6dfb\u52a0\u4e3a\u5b57\u5178\u7684\u5d4c\u5957\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u548c\u5d4c\u5957\u5b57\u5178\u6765\u5b9e\u73b0\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5d4c\u5957\u5217\u8868<\/p>\n<p>nested_list = [[&#39;a&#39;, 1], [&#39;b&#39;, 2], [&#39;c&#39;, 3]]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {}<\/p>\n<h2><strong>\u5c06\u5d4c\u5957\u5217\u8868\u5143\u7d20\u6dfb\u52a0\u4e3a\u5b57\u5178\u7684\u5d4c\u5957\u7ed3\u6784<\/strong><\/h2>\n<p>for sub_list in nested_list:<\/p>\n<p>    key, value = sub_list<\/p>\n<p>    my_dict[key] = {&#39;inner_key&#39;: value}<\/p>\n<p>print(my_dict)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;a&#39;: {&#39;inner_key&#39;: 1}, &#39;b&#39;: {&#39;inner_key&#39;: 2}, &#39;c&#39;: {&#39;inner_key&#39;: 3}}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684\u5d4c\u5957\u5b57\u5178\u7ed3\u6784\uff0c\u4ee5\u4fbf\u66f4\u7075\u6d3b\u5730\u7ec4\u7ec7\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5305\u62ec\u76f4\u63a5\u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u901a\u8fc7\u5faa\u73af\u5c06\u5217\u8868\u7684\u5143\u7d20\u4f9d\u6b21\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3001\u4f7f\u7528 <code>zip<\/code> \u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u5b57\u5178\u3001\u4f7f\u7528 <code>defaultdict<\/code> \u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\u3001\u5c06\u5d4c\u5957\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3001\u4f7f\u7528 <code>update<\/code> \u65b9\u6cd5\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3001\u4f7f\u7528 <code>setdefault<\/code> \u65b9\u6cd5\u5c06\u5217\u8868\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3001\u4f7f\u7528 <code>fromkeys<\/code> \u65b9\u6cd5\u5c06\u5217\u8868\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u3001\u4ee5\u53ca\u5c06\u5217\u8868\u5143\u7d20\u6dfb\u52a0\u4e3a\u5b57\u5178\u7684\u5d4c\u5957\u7ed3\u6784\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u4e00\u4e2a\u5217\u8868\u7684\u5143\u7d20\u9010\u4e2a\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5217\u8868\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u6216\u503c\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5217\u8868\u548c\u4e00\u4e2a\u7a7a\u5b57\u5178\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u53ef\u4ee5\u9009\u62e9\u5c06\u5217\u8868\u5143\u7d20\u4f5c\u4e3a\u952e\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u952e\u5206\u914d\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u6216\u8005\u5c06\u5176\u4f5c\u4e3a\u503c\u5e76\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u4f5c\u4e3a\u952e\u3002<\/p>\n<p><strong>\u5728\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u7684<code>update()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u6216\u8005\u76f4\u63a5\u901a\u8fc7\u7d22\u5f15\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06\u5217\u8868\u7684\u7d22\u5f15\u4f5c\u4e3a\u952e\uff0c\u5217\u8868\u7684\u503c\u4f5c\u4e3a\u76f8\u5e94\u7684\u503c\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6dfb\u52a0\u91cd\u590d\u5217\u8868\u5143\u7d20\u5230\u5b57\u5178\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u6765\u81ea\u5217\u8868\u7684\u5143\u7d20\u65f6\uff0c\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u91cd\u590d\u7684\u5143\u7d20\uff0c\u540e\u6dfb\u52a0\u7684\u5143\u7d20\u5c06\u8986\u76d6\u4e4b\u524d\u7684\u5143\u7d20\uff0c\u56e0\u4e3a\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u7684\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\uff0c\u5c06\u76f8\u540c\u7684\u952e\u6620\u5c04\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u4ece\u800c\u4fdd\u7559\u6240\u6709\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u4e00\u4e2a\u5217\u8868\u5b8c\u6574\u5730\u4f5c\u4e3a\u4e00\u4e2a\u503c\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5c06\u6574\u4e2a\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u4e00\u4e2a\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u5217\u8868\u8d4b\u503c\u7ed9\u5b57\u5178\u7684\u67d0\u4e2a\u952e\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u5217\u8868\u548c\u4e00\u4e2a\u5b57\u5178\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5c06\u5217\u8868\u4f5c\u4e3a\u4e00\u4e2a\u503c\u6dfb\u52a0\uff1a<code>my_dict[&#39;my_key&#39;] = my_list<\/code>\u3002\u8fd9\u6837\uff0c\u6574\u4e2a\u5217\u8868\u5c06\u4f5c\u4e3a<code>my_key<\/code>\u7684\u503c\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8868\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002\u5e38\u89c1\u7684\u65b9\u5f0f\u6709\uff1a\u76f4\u63a5\u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":1057859,"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\/1057854"}],"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=1057854"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057854\/revisions"}],"predecessor-version":[{"id":1057860,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057854\/revisions\/1057860"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1057859"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1057854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1057854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1057854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}