{"id":1132815,"date":"2025-01-08T21:00:45","date_gmt":"2025-01-08T13:00:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132815.html"},"modified":"2025-01-08T21:00:49","modified_gmt":"2025-01-08T13:00:49","slug":"python%e5%a6%82%e4%bd%95%e4%bb%8e%e4%b8%a4%e4%b8%aa%e5%88%97%e8%a1%a8%e5%90%8c%e6%97%b6%e5%8f%96%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132815.html","title":{"rendered":"python\u5982\u4f55\u4ece\u4e24\u4e2a\u5217\u8868\u540c\u65f6\u53d6\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102248\/32cf5902-1612-4377-bbea-f32a1790592b.webp\" alt=\"python\u5982\u4f55\u4ece\u4e24\u4e2a\u5217\u8868\u540c\u65f6\u53d6\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u8981\u4ece\u4e24\u4e2a\u5217\u8868\u4e2d\u540c\u65f6\u53d6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5982zip()\u51fd\u6570\u3001\u5217\u8868\u89e3\u6790\u3001itertools\u6a21\u5757\u7b49\u3002\u6700\u5e38\u7528\u7684\u662f<\/strong>zip()<strong>\u51fd\u6570<\/strong>\u3002\u4f7f\u7528<strong>zip()<\/strong>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u538b\u7f29\u6210\u4e00\u4e2a\u5143\u7ec4\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7ec4\u5305\u542b\u6765\u81ea\u4e24\u4e2a\u5217\u8868\u7684\u5bf9\u5e94\u5143\u7d20\u3002\u6211\u4eec\u4e0b\u9762\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528zip()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>zip()<\/strong>\u51fd\u6570\u662fPython\u4e2d\u975e\u5e38\u65b9\u4fbf\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u8fed\u4ee3\u5668\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u538b\u7f29\u5728\u4e00\u8d77\u3002<strong>zip()\u51fd\u6570\u4f1a\u5c06\u591a\u4e2a\u5217\u8868\u5e76\u884c\u8fed\u4ee3\uff0c\u751f\u6210\u4e00\u4e2a\u7531\u591a\u4e2a\u5143\u7ec4\u7ec4\u6210\u7684\u8fed\u4ee3\u5668\uff0c\u6bcf\u4e2a\u5143\u7ec4\u5305\u542b\u6765\u81ea\u6bcf\u4e2a\u5217\u8868\u7684\u4e00\u4e2a\u5143\u7d20<\/strong>\u3002\u8fd9\u4e00\u65b9\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<p>zipped = zip(list1, list2)<\/p>\n<p>print(list(zipped))  # \u8f93\u51fa: [(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;), (4, &#39;d&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<strong>zip()<\/strong>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u904d\u5386\u4e24\u4e2a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for item1, item2 in zip(list1, list2):<\/p>\n<p>    print(f&quot;Item from list1: {item1}, Item from list2: {item2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408<strong>zip()<\/strong>\u51fd\u6570\u548c\u5217\u8868\u89e3\u6790\u6765\u66f4\u6709\u6548\u5730\u4ece\u4e24\u4e2a\u5217\u8868\u4e2d\u63d0\u53d6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<p>combined = [(item1, item2) for item1, item2 in zip(list1, list2)]<\/p>\n<p>print(combined)  # \u8f93\u51fa: [(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;), (4, &#39;d&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528itertools\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684<strong>itertools<\/strong>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u5de5\u5177\u6765\u5904\u7406\u8fed\u4ee3\u5668\u3002<strong>itertools.izip_longest()<\/strong>\u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868\uff0c\u5e76\u7528\u586b\u5145\u503c\u586b\u5145\u8f83\u77ed\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>combined = list(itertools.zip_longest(list1, list2, fillvalue=None))<\/p>\n<p>print(combined)  # \u8f93\u51fa: [(1, &#39;a&#39;), (2, &#39;b&#39;), (3, None), (4, None)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528map()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>map()<\/strong>\u51fd\u6570\u53ef\u4ee5\u4e0e<strong>lambda<\/strong>\u8868\u8fbe\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u7c7b\u4f3c<strong>zip()<\/strong>\u7684\u529f\u80fd\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982<strong>zip()<\/strong>\u76f4\u89c2\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<p>combined = list(map(lambda x, y: (x, y), list1, list2))<\/p>\n<p>print(combined)  # \u8f93\u51fa: [(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;), (4, &#39;d&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u904d\u5386\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u624b\u52a8\u904d\u5386\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u5b8c\u5168\u63a7\u5236\u5982\u4f55\u4ece\u4e24\u4e2a\u5217\u8868\u4e2d\u63d0\u53d6\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u66f4\u590d\u6742\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<p>for i in range(min(len(list1), len(list2))):<\/p>\n<p>    print(f&quot;Item from list1: {list1[i]}, Item from list2: {list2[i]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u7ec4\u5408\u591a\u4e2a\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u591a\u4e2a\u5217\u8868\u4e2d\u63d0\u53d6\u5185\u5bb9\u3002<strong>zip()<\/strong>\u51fd\u6570\u4e5f\u53ef\u4ee5\u5904\u7406\u591a\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<p>list3 = [True, False, True, False]<\/p>\n<p>combined = list(zip(list1, list2, list3))<\/p>\n<p>print(combined)  # \u8f93\u51fa: [(1, &#39;a&#39;, True), (2, &#39;b&#39;, False), (3, &#39;c&#39;, True), (4, &#39;d&#39;, False)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5f53\u4e24\u4e2a\u5217\u8868\u957f\u5ea6\u4e0d\u540c\u65f6\uff0c<strong>zip()<\/strong>\u51fd\u6570\u5c06\u505c\u6b62\u5728\u8f83\u77ed\u7684\u5217\u8868\u7ed3\u675f\u65f6\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e00\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>itertools.zip_longest()<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>combined = list(itertools.zip_longest(list1, list2, fillvalue=&#39;Missing&#39;))<\/p>\n<p>print(combined)  # \u8f93\u51fa: [(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;Missing&#39;), (4, &#39;Missing&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u4ece\u4e24\u4e2a\u5217\u8868\u4e2d\u540c\u65f6\u53d6\u5185\u5bb9\u6709\u591a\u79cd\u65b9\u6cd5<\/strong>\uff0c\u6700\u5e38\u7528\u7684\u662f<strong>zip()<\/strong>\u51fd\u6570\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528<strong>\u5217\u8868\u89e3\u6790<\/strong>\u3001<strong>itertools<\/strong>\u6a21\u5757\u6216<strong>map()<\/strong>\u51fd\u6570\u7b49\u65b9\u6cd5\u3002<strong>\u5728\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528itertools.zip_longest()<\/strong>\u6765\u786e\u4fdd\u6240\u6709\u5143\u7d20\u90fd\u88ab\u5904\u7406\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u5904\u7406\u591a\u4e2a\u5217\u8868\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u3002<code>zip()<\/code>\u4f1a\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u914d\u5bf9\uff0c\u5f62\u6210\u4e00\u4e2a\u5143\u7ec4\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>for a, b in zip(list1, list2)<\/code>\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u8bbf\u95ee<code>list1<\/code>\u548c<code>list2<\/code>\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u6210\u4e00\u4e2a\u5b57\u5178\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u7ed3\u5408\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u6210\u4e00\u4e2a\u5b57\u5178\u3002\u5047\u8bbe\u6709\u4e24\u4e2a\u5217\u8868<code>keys<\/code>\u548c<code>values<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>{k: v for k, v in zip(keys, values)}<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d<code>keys<\/code>\u4e2d\u7684\u5143\u7d20\u6210\u4e3a\u5b57\u5178\u7684\u952e\uff0c<code>values<\/code>\u4e2d\u7684\u5143\u7d20\u6210\u4e3a\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u4e24\u4e2a\u5217\u8868\u957f\u5ea6\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5f53\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u904d\u5386\u4e24\u4e2a\u957f\u5ea6\u4e0d\u4e00\u81f4\u7684\u5217\u8868\u65f6\uff0c\u751f\u6210\u7684\u7ed3\u679c\u5c06\u4ee5\u8f83\u77ed\u7684\u5217\u8868\u4e3a\u51c6\u3002\u5982\u679c\u9700\u8981\u4fdd\u7559\u8f83\u957f\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u4f1a\u586b\u5145\u77ed\u5217\u8868\u7684\u7f3a\u5931\u503c\uff0c\u901a\u5e38\u4f7f\u7528<code>None<\/code>\u6216\u81ea\u5b9a\u4e49\u7684\u586b\u5145\u503c\u3002\u4f7f\u7528\u793a\u4f8b\uff1a<code>from itertools import zip_longest<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>zip_longest(list1, list2, fillvalue=None)<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4ece\u4e24\u4e2a\u5217\u8868\u4e2d\u540c\u65f6\u53d6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5982zip()\u51fd\u6570\u3001\u5217\u8868\u89e3\u6790\u3001itertools\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1132832,"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\/1132815"}],"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=1132815"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132815\/revisions"}],"predecessor-version":[{"id":1132835,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132815\/revisions\/1132835"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132832"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}