{"id":1121968,"date":"2025-01-08T19:15:37","date_gmt":"2025-01-08T11:15:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1121968.html"},"modified":"2025-01-08T19:15:39","modified_gmt":"2025-01-08T11:15:39","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e8%a1%a8%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1121968.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084016\/1596931b-77a1-4e26-9aea-ef80e21ecf9a.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001<code>zip<\/code>\u51fd\u6570\u3001<code>enumerate<\/code>\u51fd\u6570\u4ee5\u53ca\u901a\u8fc7\u5faa\u73af\u624b\u52a8\u6784\u5efa\u5b57\u5178\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7<code>zip<\/code>\u51fd\u6570\u548c\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>zip<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>zip<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u6210\u4e00\u4e2a\u5143\u7ec4\u5bf9\u7684\u5217\u8868\uff0c\u7136\u540e\u901a\u8fc7\u5c06\u8fd9\u4e2a\u5143\u7ec4\u5bf9\u7684\u5217\u8868\u4f20\u9012\u7ed9<code>dict<\/code>\u6784\u9020\u51fd\u6570\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\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 25, &#39;Female&#39;]<\/p>\n<p>dictionary = dict(zip(keys, values))<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>zip<\/code>\u51fd\u6570\u5c06<code>keys<\/code>\u548c<code>values<\/code>\u5217\u8868\u5408\u5e76\u6210\u4e00\u4e2a\u5143\u7ec4\u5bf9\u7684\u5217\u8868\uff0c\u7136\u540e<code>dict<\/code>\u6784\u9020\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u8f6c\u6362\u5217\u8868\u4e3a\u5b57\u5178\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\u3002\u7279\u522b\u9002\u7528\u4e8e\u4e24\u4e2a\u5217\u8868\u957f\u5ea6\u76f8\u540c\u7684\u60c5\u51b5\u3002\u5982\u679c\u4e24\u4e2a\u5217\u8868\u957f\u5ea6\u4e0d\u4e00\u81f4\uff0c<code>zip<\/code>\u51fd\u6570\u4f1a\u81ea\u52a8\u622a\u65ad\u8f83\u957f\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4e24\u4e2a\u5217\u8868\u957f\u5ea6\u4e0d\u4e00\u81f4\uff0c<code>zip<\/code>\u51fd\u6570\u4f1a\u81ea\u52a8\u622a\u65ad\u8f83\u957f\u7684\u5217\u8868\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u7ed3\u5408<code>itertools.zip_longest<\/code>\u51fd\u6570\u6765\u786e\u4fdd\u4e0d\u4f1a\u4e22\u5931\u6570\u636e\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\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u6784\u5efa\u5b57\u5178\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u4ece\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\u4e2d\u521b\u5efa\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_of_tuples = [(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 25), (&#39;gender&#39;, &#39;Female&#39;)]<\/p>\n<p>dictionary = {k: v for k, v in list_of_tuples}<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5b57\u5178\u63a8\u5bfc\u5f0f<code>{k: v for k, v in list_of_tuples}<\/code>\u4ece\u5305\u542b\u952e\u503c\u5bf9\u7684\u5143\u7ec4\u5217\u8868\u4e2d\u521b\u5efa\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u5728\u6784\u5efa\u5b57\u5178\u65f6\u5e94\u7528\u6761\u4ef6\u6216\u8f6c\u6362\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u4ece\u590d\u6742\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u4fe1\u606f\u5e76\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u60c5\u51b5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u7684\u7075\u6d3b\u6027\u4e5f\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u590d\u6742\u6027\u589e\u52a0\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5e94\u7528\u591a\u4e2a\u6761\u4ef6\u6216\u8f6c\u6362\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>enumerate<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0e\u5176\u7d22\u5f15\u914d\u5bf9\uff0c\u7528\u4e8e\u751f\u6210\u952e\u503c\u5bf9\uff0c\u7136\u540e\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u6216<code>dict<\/code>\u6784\u9020\u51fd\u6570\u751f\u6210\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">values = [&#39;Alice&#39;, 25, &#39;Female&#39;]<\/p>\n<p>dictionary = {i: v for i, v in enumerate(values)}<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>enumerate<\/code>\u51fd\u6570\u5c06<code>values<\/code>\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0e\u5176\u7d22\u5f15\u914d\u5bf9\uff0c\u7136\u540e\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<p><p><code>enumerate<\/code>\u51fd\u6570\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u5e76\u4f7f\u7528\u7d22\u5f15\u4f5c\u4e3a\u952e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u751f\u6210\u7684\u5b57\u5178\u952e\u662f\u7d22\u5f15\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4e0d\u7b26\u5408\u9700\u6c42\u3002\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u952e\uff0c\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5faa\u73af\u624b\u52a8\u6784\u5efa\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\u624b\u52a8\u6784\u5efa\u5b57\u5178\u662f\u4e00\u79cd\u6700\u57fa\u672c\u4f46\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u590d\u6742\u60c5\u51b5\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;, 25, &#39;Female&#39;]<\/p>\n<p>dictionary = {}<\/p>\n<p>for k, v in zip(keys, values):<\/p>\n<p>    dictionary[k] = v<\/p>\n<p>print(dictionary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5faa\u73af\u7ed3\u5408<code>zip<\/code>\u51fd\u6570\u9010\u6b65\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<p><p>\u624b\u52a8\u6784\u5efa\u5b57\u5178\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u590d\u6742\u60c5\u51b5\uff0c\u5982\u952e\u503c\u5bf9\u7684\u81ea\u5b9a\u4e49\u903b\u8f91\u3001\u6761\u4ef6\u8fc7\u6ee4\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u624b\u52a8\u6784\u5efa\u5b57\u5178\u7684\u4ee3\u7801\u76f8\u5bf9\u8f83\u957f\u4e14\u4e0d\u591f\u7b80\u6d01\u3002\u5728\u7b80\u5355\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u663e\u5f97\u5197\u4f59\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u9009\u62e9\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002<strong>\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001<code>enumerate<\/code>\u51fd\u6570\u4ee5\u53ca\u901a\u8fc7\u5faa\u73af\u624b\u52a8\u6784\u5efa\u5b57\u5178\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u6269\u5c55\u9605\u8bfb<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5217\u8868\u548c\u5b57\u5178\u4e4b\u95f4\u7684\u8f6c\u6362\u5e38\u5e38\u9700\u8981\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5d4c\u5957\u5217\u8868\u3001\u5d4c\u5957\u5b57\u5178\u3001\u5305\u542b\u4e0d\u89c4\u5219\u6570\u636e\u7684\u5217\u8868\u7b49\u3002\u4e86\u89e3\u548c\u638c\u63e1\u4e0a\u8ff0\u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u7075\u6d3b\u5e94\u7528\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u5b57\u5178\u3002\u9996\u5148\uff0c\u51c6\u5907\u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u53e6\u4e00\u4e2a\u4f5c\u4e3a\u503c\u3002\u4f7f\u7528<code>dict(zip(list1, list2))<\/code>\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>list1 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\uff0c<code>list2 = [1, 2, 3]<\/code>\uff0c\u5219<code>dict(zip(list1, list2))<\/code>\u5c06\u8fd4\u56de<code>{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\uff0c\u53ef\u4ee5\u9009\u62e9\u4e00\u4e2a\u56fa\u5b9a\u7684\u952e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5217\u8868<code>values = [10, 20, 30]<\/code>\uff0c\u5e76\u5e0c\u671b\u5c06\u8fd9\u4e9b\u503c\u653e\u5165\u5b57\u5178\u4e2d\uff0c\u4f7f\u7528<code>{&#39;key&#39;: value for value in values}<\/code>\u7684\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u3002\u8fd9\u6837\uff0c\u6240\u6709\u503c\u90fd\u5c06\u5bf9\u5e94\u540c\u4e00\u4e2a\u952e\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u6539\u952e\u540d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u4ee5\u521b\u5efa\u5b57\u5178\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u4e2d\u5305\u542b\u91cd\u590d\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6216\u8ba1\u6570\u7684\u65b9\u5f0f\u6765\u5904\u7406\u3002\u4f7f\u7528<code>collections.Counter<\/code>\u7c7b\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5217\u8868\u662f<code>[&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;]<\/code>\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>from collections import Counter<\/code>\u548c<code>Counter(list)<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u7ed3\u679c\u5c06\u662f<code>{&#39;apple&#39;: 2, &#39;banana&#39;: 1}<\/code>\uff0c\u5176\u4e2d\u952e\u662f\u5143\u7d20\uff0c\u503c\u662f\u5b83\u4eec\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001zip\u51fd\u6570\u3001enumerate\u51fd\u6570\u4ee5\u53ca\u901a\u8fc7\u5faa [&hellip;]","protected":false},"author":3,"featured_media":1121974,"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\/1121968"}],"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=1121968"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121968\/revisions"}],"predecessor-version":[{"id":1121977,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121968\/revisions\/1121977"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1121974"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1121968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1121968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1121968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}