{"id":1155551,"date":"2025-01-13T18:05:03","date_gmt":"2025-01-13T10:05:03","guid":{"rendered":""},"modified":"2025-01-13T18:05:06","modified_gmt":"2025-01-13T10:05:06","slug":"python%e5%a6%82%e4%bd%95%e8%87%aa%e5%8a%a8%e5%88%9b%e5%bb%ba%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1155551.html","title":{"rendered":"python\u5982\u4f55\u81ea\u52a8\u521b\u5efa\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194147\/efea93dc-142c-4a07-8bb6-b416c1d3400f.webp\" alt=\"python\u5982\u4f55\u81ea\u52a8\u521b\u5efa\u5b57\u5178\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u81ea\u52a8\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001<code>dict<\/code>\u51fd\u6570\u3001<code>fromkeys<\/code>\u65b9\u6cd5\u3001<code>defaultdict<\/code>\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\u53ef\u4ee5\u901a\u8fc7\u7b80\u6d01\u7684\u8bed\u6cd5\u6765\u5feb\u901f\u521b\u5efa\u548c\u521d\u59cb\u5316\u5b57\u5178\u3002<strong>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u9ad8\u6548\u548c\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u57fa\u4e8e\u73b0\u6709\u6570\u636e\u8fdb\u884c\u5b57\u5178\u521b\u5efa\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5feb\u901f\u751f\u6210\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">key_value_p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rs = [(&quot;name&quot;, &quot;Alice&quot;), (&quot;age&quot;, 25), (&quot;city&quot;, &quot;New York&quot;)]<\/p>\n<p>dictionary = {k: v for k, v in key_value_pairs}<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#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><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u4ec5\u662f\u521b\u5efa\u5b57\u5178\u7684\u7b80\u6d01\u65b9\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u4f8b\u5982\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u67d0\u79cd\u8f6c\u6362\u6216\u8fc7\u6ee4\u540e\u518d\u521b\u5efa\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u51e0\u79cd\u5e38\u89c1\u7684\u5b57\u5178\u521b\u5efa\u65b9\u5f0f\uff0c\u5e76\u4ecb\u7ecd\u5b83\u4eec\u7684\u5177\u4f53\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\uff08dictionary comprehension\uff09\u662fPython\u4e2d\u521b\u5efa\u5b57\u5178\u7684\u7b80\u6d01\u65b9\u5f0f\u3002\u5b83\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u7528\u4e8e\u521b\u5efa\u5b57\u5178\u3002\u5b57\u5178\u63a8\u5bfc\u5f0f\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{key: value for key, value in iterable}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>iterable<\/code>\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c<code>key<\/code>\u548c<code>value<\/code>\u662f\u4ece\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u63d0\u53d6\u7684\u952e\u548c\u503c\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u7b80\u5355\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">key_value_pairs = [(&quot;name&quot;, &quot;Alice&quot;), (&quot;age&quot;, 25), (&quot;city&quot;, &quot;New York&quot;)]<\/p>\n<p>dictionary = {k: v for k, v in key_value_pairs}<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#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><h4>2. \u901a\u8fc7\u8ba1\u7b97\u751f\u6210\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u8fd8\u53ef\u4ee5\u7528\u4e8e\u901a\u8fc7\u8ba1\u7b97\u751f\u6210\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u53ca\u5176\u5e73\u65b9\u7684\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = {x: x*x for x in range(1, 6)}<\/p>\n<p>print(squares)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8fc7\u6ee4\u751f\u6210\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u8fd8\u53ef\u4ee5\u4e0e\u6761\u4ef6\u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ece\u800c\u6839\u636e\u6761\u4ef6\u8fc7\u6ee4\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u53ea\u5305\u542b\u5076\u6570\u53ca\u5176\u5e73\u65b9\u7684\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">even_squares = {x: x*x for x in range(1, 11) if x % 2 == 0}<\/p>\n<p>print(even_squares)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{2: 4, 4: 16, 6: 36, 8: 64, 10: 100}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>dict<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>dict<\/code> \u51fd\u6570\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u5b57\u5178\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u5982\u952e\u503c\u5bf9\u7684\u5e8f\u5217\u3001\u5173\u952e\u5b57\u53c2\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u901a\u8fc7\u952e\u503c\u5bf9\u5e8f\u5217\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5305\u542b\u952e\u503c\u5bf9\u7684\u5e8f\u5217\uff08\u5982\u5217\u8868\u6216\u5143\u7ec4\uff09\u521b\u5efa\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">key_value_pairs = [(&quot;name&quot;, &quot;Alice&quot;), (&quot;age&quot;, 25), (&quot;city&quot;, &quot;New York&quot;)]<\/p>\n<p>dictionary = dict(key_value_pairs)<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#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><h4>2. \u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u521b\u5efa\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dictionary = dict(name=&quot;Alice&quot;, age=25, city=&quot;New York&quot;)<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#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><h4>3. \u901a\u8fc7\u6620\u5c04\u5bf9\u8c61\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u53e6\u4e00\u4e2a\u6620\u5c04\u5bf9\u8c61\uff08\u5982\u73b0\u6709\u5b57\u5178\uff09\u521b\u5efa\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">existing_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>new_dict = dict(existing_dict)<\/p>\n<p>print(new_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#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>\u4e09\u3001\u4f7f\u7528 <code>fromkeys<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>fromkeys<\/code> \u65b9\u6cd5\u662f\u5b57\u5178\u7c7b\u7684\u4e00\u4e2a\u7c7b\u65b9\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u5b57\u5178\uff0c\u5176\u4e2d\u6240\u6709\u952e\u90fd\u5177\u6709\u76f8\u540c\u7684\u503c\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict.fromkeys(keys, value=None)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>keys<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u952e\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c<code>value<\/code> \u662f\u6240\u6709\u952e\u7684\u9ed8\u8ba4\u503c\uff0c\u9ed8\u8ba4\u4e3a <code>None<\/code>\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5177\u6709\u76f8\u540c\u503c\u7684\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>fromkeys<\/code> \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6240\u6709\u952e\u90fd\u5177\u6709\u76f8\u540c\u503c\u7684\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>dictionary = dict.fromkeys(keys, &quot;unknown&quot;)<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;name&#39;: &#39;unknown&#39;, &#39;age&#39;: &#39;unknown&#39;, &#39;city&#39;: &#39;unknown&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u672a\u6307\u5b9a\u503c\uff0c\u6240\u6709\u952e\u7684\u503c\u9ed8\u8ba4\u4e3a <code>None<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>dictionary = dict.fromkeys(keys)<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;name&#39;: None, &#39;age&#39;: None, &#39;city&#39;: None}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>defaultdict<\/code><\/h3>\n<\/p>\n<p><p><code>defaultdict<\/code> \u662f <code>collections<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u5de5\u5382\u51fd\u6570\uff0c\u7528\u4e8e\u4e3a\u4e0d\u5b58\u5728\u7684\u952e\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5b57\u5178\u4e2d\u7684\u7f3a\u5931\u952e\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa <code>defaultdict<\/code><\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>defaultdict<\/code> \u521b\u5efa\u4e00\u4e2a\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>dictionary = defaultdict(int)  # \u9ed8\u8ba4\u503c\u4e3a0\u7684defaultdict<\/p>\n<p>dictionary[&#39;count&#39;] += 1<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">defaultdict(&lt;class &#39;int&#39;&gt;, {&#39;count&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>defaultdict<\/code> \u5904\u7406\u7f3a\u5931\u952e<\/h4>\n<\/p>\n<p><p><code>defaultdict<\/code> \u53ef\u81ea\u52a8\u5904\u7406\u7f3a\u5931\u952e\uff0c\u907f\u514d <code>KeyError<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>dictionary = defaultdict(list)  # \u9ed8\u8ba4\u503c\u4e3a\u7a7a\u5217\u8868\u7684defaultdict<\/p>\n<p>dictionary[&#39;fruits&#39;].append(&#39;apple&#39;)<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;fruits&#39;: [&#39;apple&#39;]})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u521b\u5efa\u548c\u521d\u59cb\u5316\u5b57\u5178\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u57fa\u4e8e\u73b0\u6709\u6570\u636e\u8fdb\u884c\u5b57\u5178\u521b\u5efa\u7684\u573a\u666f\uff0c\u8bed\u6cd5\u7b80\u6d01\u3002<\/li>\n<li><strong><code>dict<\/code> \u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u591a\u79cd\u8f93\u5165\u7c7b\u578b\uff0c\u7075\u6d3b\u6027\u5f3a\u3002<\/li>\n<li><strong><code>fromkeys<\/code> \u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u521b\u5efa\u5177\u6709\u76f8\u540c\u503c\u7684\u5b57\u5178\u3002<\/li>\n<li><strong><code>defaultdict<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u7f3a\u5931\u952e\u7684\u573a\u666f\uff0c\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ol>\n<p><p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f<\/strong>\u3002\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\u66f4\u9ad8\u6548\u3001\u7075\u6d3b\u5730\u5904\u7406\u5b57\u5178\u76f8\u5173\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u63a8\u5bfc\u5f0f\u5feb\u901f\u521b\u5efa\u5b57\u5178\uff1f<\/strong><br \/>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u5feb\u901f\u521b\u5efa\u5b57\u5178\u3002\u901a\u8fc7\u7b80\u5355\u7684\u8868\u8fbe\u5f0f\uff0c\u4f60\u53ef\u4ee5\u751f\u6210\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c<code>{x: x**2 for x in range(5)}<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f0\u52304\u7684\u6570\u5b57\uff0c\u503c\u662f\u5bf9\u5e94\u6570\u5b57\u7684\u5e73\u65b9\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u8fd8\u63d0\u9ad8\u4e86\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4ece\u5217\u8868\u521b\u5efa\u5b57\u5178\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528<code>zip()<\/code>\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u7ec4\u5408\u6210\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5217\u8868<code>keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\u548c\u4e00\u4e2a\u5217\u8868<code>values = [1, 2, 3]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>dict(zip(keys, values))<\/code>\u6765\u521b\u5efa\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5f53\u4f60\u6709\u5bf9\u5e94\u7684\u952e\u548c\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u80fd\u591f\u5feb\u901f\u751f\u6210\u5b57\u5178\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u5b57\u5178\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff1f<\/strong><br \/>\u4f7f\u7528<code>collections.defaultdict<\/code>\u53ef\u4ee5\u4e3a\u5b57\u5178\u6307\u5b9a\u9ed8\u8ba4\u503c\u7c7b\u578b\u3002\u8fd9\u6837\u5f53\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u5b57\u5178\u4f1a\u81ea\u52a8\u4e3a\u8be5\u952e\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff0c<code>from collections import defaultdict; d = defaultdict(int)<\/code>\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u4efb\u4f55\u4e0d\u5b58\u5728\u7684\u952e\u90fd\u5c06\u8fd4\u56de0\u3002\u8fd9\u5728\u5904\u7406\u9700\u8981\u7d2f\u52a0\u6216\u8ba1\u6570\u7684\u573a\u666f\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u81ea\u52a8\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001dict\u51fd\u6570\u3001fromkeys\u65b9\u6cd5\u3001defaultdict [&hellip;]","protected":false},"author":3,"featured_media":1155559,"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\/1155551"}],"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=1155551"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155551\/revisions"}],"predecessor-version":[{"id":1155561,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155551\/revisions\/1155561"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1155559"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1155551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1155551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1155551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}