{"id":1042195,"date":"2024-12-31T12:55:24","date_gmt":"2024-12-31T04:55:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1042195.html"},"modified":"2024-12-31T12:55:27","modified_gmt":"2024-12-31T04:55:27","slug":"python%e4%b8%a4%e4%b8%aa%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1042195.html","title":{"rendered":"python\u4e24\u4e2a\u5217\u8868\u5982\u4f55\u751f\u6210\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3a88a046-266b-4ac8-8c4f-dedf28777f64.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e24\u4e2a\u5217\u8868\u5982\u4f55\u751f\u6210\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u7ec4\u5408\u751f\u6210\u4e00\u4e2a\u5b57\u5178\u3002<\/strong> \u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>zip()<\/code>\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u4ee5\u53ca<code>dict()<\/code>\u51fd\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>zip()\u51fd\u6570\u751f\u6210\u5b57\u5178<\/strong>\uff1a<code>zip()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u8868\u793a\u952e\uff0c\u53e6\u4e00\u4e2a\u8868\u793a\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 zip() \u548c dict() \u751f\u6210\u5b57\u5178<\/strong><\/h2>\n<p>result_dict = dict(zip(keys, values))<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u7684\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;gender&#39;: &#39;Female&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>zip()<\/code>\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u914d\u5bf9\u6253\u5305\uff0c\u7136\u540e<code>dict()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u5b57\u5178<\/strong>\uff1a\u5b57\u5178\u63a8\u5bfc\u5f0f\u4e5f\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u5b57\u5178\u7684\u540c\u65f6\u5bf9\u952e\u503c\u5bf9\u8fdb\u884c\u4e00\u4e9b\u64cd\u4f5c\u6216\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u5b57\u5178<\/strong><\/h2>\n<p>result_dict = {k: v for k, v in zip(keys, values)}<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u7684\u8f93\u51fa\u5c06\u4e0e\u524d\u4e00\u4e2a\u793a\u4f8b\u76f8\u540c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;gender&#39;: &#39;Female&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5728\u521b\u5efa\u5b57\u5178\u7684\u540c\u65f6\u8fdb\u884c\u4e00\u4e9b\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u5982\u8fc7\u6ee4\u6216\u8f6c\u6362\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5faa\u73af\u751f\u6210\u5b57\u5178<\/strong>\uff1a\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u903b\u8f91\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u624b\u52a8\u751f\u6210\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 for \u5faa\u73af\u751f\u6210\u5b57\u5178<\/strong><\/h2>\n<p>result_dict = {}<\/p>\n<p>for i in range(len(keys)):<\/p>\n<p>    result_dict[keys[i]] = values[i]<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u540c\u6837\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;gender&#39;: &#39;Female&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u903b\u8f91\u66f4\u76f4\u89c2\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0\u66f4\u591a\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p><strong>\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/strong>\uff1a\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e24\u4e2a\u5217\u8868\u7684\u957f\u5ea6\u53ef\u80fd\u4e0d\u540c\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest()<\/code>\u65b9\u6cd5\uff0c\u5b83\u5c06\u8f83\u77ed\u5217\u8868\u7684\u7f3a\u5931\u503c\u8865\u5145\u4e3a<code>None<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import zip_longest<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30]<\/p>\n<h2><strong>\u4f7f\u7528 zip_longest \u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/strong><\/h2>\n<p>result_dict = dict(zip_longest(keys, values))<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u7684\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;gender&#39;: None}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u5b57\u5178\u5305\u542b\u6240\u6709\u7684\u952e\uff0c\u5373\u4f7f\u67d0\u4e9b\u952e\u6ca1\u6709\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528zip()\u51fd\u6570\u751f\u6210\u5b57\u5178<\/h3>\n<\/p>\n<p><p><code>zip()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u540c\u65f6\u8fed\u4ee3\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\u3002\u5f53\u6211\u4eec\u6709\u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u8868\u793a\u5b57\u5178\u7684\u952e\uff0c\u53e6\u4e00\u4e2a\u8868\u793a\u5b57\u5178\u7684\u503c\u65f6\uff0c\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 zip() \u548c dict() \u751f\u6210\u5b57\u5178<\/strong><\/h2>\n<p>result_dict = dict(zip(keys, values))<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>zip(keys, values)<\/code>\u4f1a\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6bcf\u4e2a\u5143\u7ec4\u5305\u542b\u4e00\u4e2a\u952e\u548c\u503c\u3002\u7136\u540e<code>dict()<\/code>\u51fd\u6570\u5c06\u8fd9\u4e9b\u5143\u7ec4\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u548c\u7075\u6d3b\u7684\u65b9\u6cd5\u6765\u751f\u6210\u5b57\u5178\u3002\u5728\u521b\u5efa\u5b57\u5178\u7684\u540c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u952e\u503c\u5bf9\u8fdb\u884c\u4e00\u4e9b\u64cd\u4f5c\u6216\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u5b57\u5178<\/strong><\/h2>\n<p>result_dict = {k: v for k, v in zip(keys, values)}<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5728\u751f\u6210\u5b57\u5178\u7684\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u4e00\u4e9b\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u5982\u8fc7\u6ee4\u6216\u8f6c\u6362\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528for\u5faa\u73af\u751f\u6210\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u66f4\u590d\u6742\u7684\u903b\u8f91\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u624b\u52a8\u751f\u6210\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u548c\u7075\u6d3b\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 for \u5faa\u73af\u751f\u6210\u5b57\u5178<\/strong><\/h2>\n<p>result_dict = {}<\/p>\n<p>for i in range(len(keys)):<\/p>\n<p>    result_dict[keys[i]] = values[i]<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u903b\u8f91\u66f4\u76f4\u89c2\uff0c\u9002\u5408\u5904\u7406\u4e00\u4e9b\u590d\u6742\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e24\u4e2a\u5217\u8868\u7684\u957f\u5ea6\u53ef\u80fd\u4e0d\u540c\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest()<\/code>\u65b9\u6cd5\uff0c\u5b83\u4f1a\u5c06\u8f83\u77ed\u5217\u8868\u7684\u7f3a\u5931\u503c\u8865\u5145\u4e3a<code>None<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import zip_longest<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30]<\/p>\n<h2><strong>\u4f7f\u7528 zip_longest \u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/strong><\/h2>\n<p>result_dict = dict(zip_longest(keys, values))<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>zip_longest(keys, values)<\/code>\u4f1a\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5f53\u4e00\u4e2a\u5217\u8868\u6bd4\u53e6\u4e00\u4e2a\u5217\u8868\u77ed\u65f6\uff0c\u4f1a\u7528<code>None<\/code>\u586b\u5145\u7f3a\u5931\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528pandas\u751f\u6210\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u5904\u7406\u66f4\u5927\u89c4\u6a21\u7684\u6570\u636e\u96c6\uff0c\u6216\u8005\u5df2\u7ecf\u5728\u4f7f\u7528pandas\u5e93\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u5229\u7528pandas\u6765\u751f\u6210\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 pandas \u751f\u6210\u5b57\u5178<\/strong><\/h2>\n<p>result_dict = pd.Series(values, index=keys).to_dict()<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2apandas Series\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>to_dict()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5d4c\u5957\u5b57\u5178\u7684\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u751f\u6210\u5d4c\u5957\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5217\u8868\u8868\u793a\u5916\u5c42\u5b57\u5178\u7684\u952e\uff0c\u53e6\u4e00\u4e2a\u5217\u8868\u8868\u793a\u5d4c\u5957\u5b57\u5178\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;person1&#39;, &#39;person2&#39;]<\/p>\n<p>nested_keys = [&#39;name&#39;, &#39;age&#39;]<\/p>\n<p>nested_values = [[&#39;Alice&#39;, 30], [&#39;Bob&#39;, 25]]<\/p>\n<h2><strong>\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u751f\u6210\u5b57\u5178<\/strong><\/h2>\n<p>result_dict = {k: dict(zip(nested_keys, v)) for k, v in zip(keys, nested_values)}<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u751f\u6210\u7684\u5b57\u5178\u5305\u542b\u5d4c\u5957\u5b57\u5178\uff0c\u6bcf\u4e2a\u5d4c\u5957\u5b57\u5178\u5305\u542b<code>name<\/code>\u548c<code>age<\/code>\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528defaultdict\u751f\u6210\u5b57\u5178<\/h3>\n<\/p>\n<p><p><code>collections.defaultdict<\/code>\u662f\u53e6\u4e00\u4e2a\u751f\u6210\u5b57\u5178\u7684\u6709\u7528\u5de5\u5177\uff0c\u7279\u522b\u662f\u5f53\u4f60\u9700\u8981\u5904\u7406\u9ed8\u8ba4\u503c\u6216\u52a8\u6001\u751f\u6210\u5d4c\u5957\u5b57\u5178\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 defaultdict \u751f\u6210\u5b57\u5178<\/strong><\/h2>\n<p>result_dict = defaultdict(lambda: &#39;Unknown&#39;)<\/p>\n<p>result_dict.update(zip(keys, values))<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>defaultdict<\/code>\u6765\u5904\u7406\u9ed8\u8ba4\u503c\uff0c\u786e\u4fdd\u5373\u4f7f\u952e\u4e0d\u5b58\u5728\u4e5f\u4e0d\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u91cd\u590d\u952e\u7684\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5f53\u4e24\u4e2a\u5217\u8868\u7684\u952e\u6709\u91cd\u590d\u65f6\uff0c\u6211\u4eec\u9700\u8981\u51b3\u5b9a\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u91cd\u590d\u7684\u952e\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u4fdd\u7559\u6700\u540e\u4e00\u4e2a\u503c\uff0c\u6216\u8005\u5c06\u6240\u6709\u503c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4fdd\u7559\u6700\u540e\u4e00\u4e2a\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;, &#39;age&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;, 25]<\/p>\n<h2><strong>\u4fdd\u7559\u6700\u540e\u4e00\u4e2a\u503c<\/strong><\/h2>\n<p>result_dict = dict(zip(keys, values))<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;gender&#39;: &#39;Female&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5c06\u6240\u6709\u503c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;, &#39;age&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 30, &#39;Female&#39;, 25]<\/p>\n<h2><strong>\u5c06\u6240\u6709\u503c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d<\/strong><\/h2>\n<p>result_dict = defaultdict(list)<\/p>\n<p>for k, v in zip(keys, values):<\/p>\n<p>    result_dict[k].append(v)<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;name&#39;: [&#39;Alice&#39;], &#39;age&#39;: [30, 25], &#39;gender&#39;: [&#39;Female&#39;]})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528enumerate\u5904\u7406\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u5217\u8868\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u751f\u6210\u7684\u5b57\u5178\u5305\u542b\u7d22\u5f15\u4f5c\u4e3a\u952e\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">values = [&#39;Alice&#39;, 30, &#39;Female&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 enumerate \u5904\u7406\u7d22\u5f15<\/strong><\/h2>\n<p>result_dict = {i: v for i, v in enumerate(values)}<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{0: &#39;Alice&#39;, 1: 30, 2: &#39;Female&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u751f\u6210\u5305\u542b\u7d22\u5f15\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u7684\u503c\u5217\u8868\u53ef\u80fd\u5305\u542b\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u5178\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u751f\u6210\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;det<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, {&#39;age&#39;: 30, &#39;gender&#39;: &#39;Female&#39;}]<\/p>\n<h2><strong>\u5904\u7406\u590d\u6742\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>result_dict = dict(zip(keys, values))<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;name&#39;: &#39;Alice&#39;, &#39;details&#39;: {&#39;age&#39;: 30, &#39;gender&#39;: &#39;Female&#39;}}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5404\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u5c06\u4e24\u4e2a\u5217\u8868\u751f\u6210\u5b57\u5178\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u5b57\u5178\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>zip()<\/code>\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u7ec4\u5408\u6210\u4e00\u4e2a\u5b57\u5178\u3002\u901a\u8fc7\u5c06\u4e24\u4e2a\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>dict()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u5217\u8868<code>keys<\/code>\u548c<code>values<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\nvalues = [1, 2, 3]\nresult_dict = dict(zip(keys, values))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>result_dict<\/code>\u5c06\u4f1a\u662f<code>{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/code>\u3002<\/p>\n<p><strong>\u5728\u751f\u6210\u5b57\u5178\u65f6\uff0c\u5217\u8868\u957f\u5ea6\u4e0d\u4e00\u81f4\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\uff1f<\/strong><br \/>\u5f53\u4e24\u4e2a\u5217\u8868\u7684\u957f\u5ea6\u4e0d\u4e00\u81f4\u65f6\uff0c<code>zip()<\/code>\u51fd\u6570\u4f1a\u81ea\u52a8\u622a\u53d6\u8f83\u77ed\u7684\u5217\u8868\u957f\u5ea6\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u591a\u4f59\u7684\u5143\u7d20\u4f1a\u88ab\u5ffd\u7565\u3002\u4f8b\u5982\uff0c\u82e5<code>keys<\/code>\u6709\u4e09\u4e2a\u5143\u7d20\u800c<code>values<\/code>\u53ea\u6709\u4e24\u4e2a\uff0c\u751f\u6210\u7684\u5b57\u5178\u53ea\u4f1a\u5305\u542b\u4e24\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5b57\u5178\u751f\u6210\u8fc7\u7a0b\u4e2d\u7684\u91cd\u590d\u952e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u5728\u4e24\u4e2a\u5217\u8868\u4e2d\u5b58\u5728\u91cd\u590d\u7684\u952e\uff0c\u5219\u540e\u9762\u7684\u503c\u4f1a\u8986\u76d6\u4e4b\u524d\u7684\u503c\u3002\u4f8b\u5982\uff0c\u82e5<code>keys<\/code>\u5217\u8868\u4e3a<code>[&#39;a&#39;, &#39;b&#39;, &#39;a&#39;]<\/code>\uff0c<code>values<\/code>\u5217\u8868\u4e3a<code>[1, 2, 3]<\/code>\uff0c\u751f\u6210\u7684\u5b57\u5178\u5c06\u4f1a\u662f<code>{&#39;a&#39;: 3, &#39;b&#39;: 2}<\/code>\uff0c\u5176\u4e2d<code>&#39;a&#39;<\/code>\u7684\u503c\u88ab\u66f4\u65b0\u4e3a3\u3002\u82e5\u9700\u8981\u4fdd\u7559\u6240\u6709\u503c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>collections.defaultdict<\/code>\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u7ec4\u5408\u751f\u6210\u4e00\u4e2a\u5b57\u5178\u3002 \u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1042206,"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\/1042195"}],"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=1042195"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042195\/revisions"}],"predecessor-version":[{"id":1042209,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042195\/revisions\/1042209"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1042206"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1042195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1042195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1042195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}