{"id":1122849,"date":"2025-01-08T19:26:04","date_gmt":"2025-01-08T11:26:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122849.html"},"modified":"2025-01-08T19:26:07","modified_gmt":"2025-01-08T11:26:07","slug":"python%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e9%81%8d%e5%8e%86%e4%b8%a4%e4%b8%aa%e5%88%97%e8%a1%a86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1122849.html","title":{"rendered":"python\u5982\u4f55\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u88686"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084527\/a765574a-88ef-4b18-b4f0-e4b018b358c1.webp\" alt=\"python\u5982\u4f55\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u88686\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u3001\u5229\u7528\u7d22\u5f15\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u7b49\u65b9\u6cd5\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\u3002<strong><code>zip()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u89c1\u548c\u6700\u7b80\u6d01\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u6309\u5143\u7d20\u914d\u5bf9\u5f62\u6210\u5143\u7ec4\uff0c\u5e76\u8fd4\u56de\u8fd9\u4e9b\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\u3002\u901a\u8fc7<code>zip()<\/code>\u904d\u5386\u5217\u8868\u4e0d\u4ec5\u53ef\u4ee5\u4fdd\u8bc1\u4ee3\u7801\u7684\u7b80\u6d01\u6027\uff0c\u540c\u65f6\u4e5f\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<strong>\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>1.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>zip()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u4f1a\u5c06\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e00\u4e00\u914d\u5bf9\uff0c\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for item1, item2 in zip(list1, list2):<\/p>\n<p>    print(item1, item2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>zip(list1, list2)<\/code>\u4f1a\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u4ea7\u751f\u7684\u5143\u7ec4\u5206\u522b\u662f <code>(1, &#39;a&#39;)<\/code>, <code>(2, &#39;b&#39;)<\/code>, <code>(3, &#39;c&#39;)<\/code>\u3002\u7136\u540e\u901a\u8fc7for\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e24\u4e2a\u5217\u8868\u7684\u957f\u5ea6\u4e0d\u540c\uff0c<code>zip()<\/code>\u51fd\u6570\u4f1a\u6309\u7167\u6700\u77ed\u7684\u90a3\u4e2a\u5217\u8868\u6765\u914d\u5bf9\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>for item1, item2 in zip(list1, list2):<\/p>\n<p>    print(item1, item2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8f93\u51fa\u4f1a\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>1 a<\/p>\n<p>2 b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7b2c\u4e09\u4e2a\u5143\u7d20<code>3<\/code>\u6ca1\u6709\u5bf9\u5e94\u7684\u5143\u7d20\uff0c\u6240\u4ee5\u88ab\u5ffd\u7565\u4e86\u3002<\/p>\n<\/p>\n<p><h3>1.3 \u4f7f\u7528<code>itertools.zip_longest()<\/code><\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u957f\u5ea6\u4e0d\u540c\u7684\u5217\u8868\u800c\u4e0d\u5ffd\u7565\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools<\/code>\u5e93\u7684<code>zip_longest()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import zip_longest<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>for item1, item2 in zip_longest(list1, list2, fillvalue=None):<\/p>\n<p>    print(item1, item2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>1 a<\/p>\n<p>2 b<\/p>\n<p>3 None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>zip_longest()<\/code>\u51fd\u6570\u4f1a\u7528<code>fillvalue<\/code>\u53c2\u6570\u586b\u5145\u8f83\u77ed\u5217\u8868\u4e2d\u7684\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5229\u7528\u7d22\u5f15\u904d\u5386<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u7d22\u5f15\u6765\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u540c\u65f6\u8bbf\u95ee\u7d22\u5f15\u548c\u5143\u7d20\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for i in range(len(list1)):<\/p>\n<p>    print(list1[i], list2[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>range(len(list1))<\/code>\u751f\u6210\u4e00\u4e2a\u7d22\u5f15\u5e8f\u5217\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15\u540c\u65f6\u8bbf\u95ee\u4e24\u4e2a\u5217\u8868\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5f53\u4e24\u4e2a\u5217\u8868\u957f\u5ea6\u4e0d\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>min()<\/code>\u51fd\u6570\u6765\u786e\u4fdd\u4e0d\u8d85\u51fa\u8f83\u77ed\u5217\u8868\u7684\u957f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>for i in range(min(len(list1), len(list2))):<\/p>\n<p>    print(list1[i], list2[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>1 a<\/p>\n<p>2 b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e0d\u4f1a\u51fa\u73b0\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>combined = [(item1, item2) for item1, item2 in zip(list1, list2)]<\/p>\n<p>print(combined)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>[(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>3.2 \u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest()<\/code>\u6765\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import zip_longest<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>combined = [(item1, item2) for item1, item2 in zip_longest(list1, list2, fillvalue=None)]<\/p>\n<p>print(combined)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>[(1, &#39;a&#39;), (2, &#39;b&#39;), (3, None)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u4fdd\u8bc1\u4e86\u8f83\u77ed\u7684\u5217\u8868\u4e0d\u4f1a\u5bfc\u81f4\u7d22\u5f15\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528<code>map()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u5e38\u89c1\uff0c\u4f46<code>map()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u6765\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\u3002<code>map()<\/code>\u51fd\u6570\u4f1a\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u5230\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for item1, item2 in map(lambda x, y: (x, y), list1, list2):<\/p>\n<p>    print(item1, item2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>map()<\/code>\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u7684\u5143\u7d20\u4e00\u4e00\u914d\u5bf9\uff0c\u7c7b\u4f3c\u4e8e<code>zip()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>4.2 \u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4e0e<code>zip()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u5982\u679c\u4e24\u4e2a\u5217\u8868\u957f\u5ea6\u4e0d\u540c\uff0c<code>map()<\/code>\u51fd\u6570\u4e5f\u4f1a\u6309\u7167\u6700\u77ed\u7684\u90a3\u4e2a\u5217\u8868\u6765\u914d\u5bf9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>for item1, item2 in map(lambda x, y: (x, y), list1, list2):<\/p>\n<p>    print(item1, item2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>1 a<\/p>\n<p>2 b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\u548c\u7d22\u5f15\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>enumerate()<\/code>\u548c<code>zip()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for index, (item1, item2) in enumerate(zip(list1, list2)):<\/p>\n<p>    print(index, item1, item2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>enumerate()<\/code>\u51fd\u6570\u4f1a\u751f\u6210\u7d22\u5f15\u548c\u5143\u7d20\u7684\u5143\u7ec4\uff0c<code>zip()<\/code>\u51fd\u6570\u8d1f\u8d23\u914d\u5bf9\u4e24\u4e2a\u5217\u8868\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>5.2 \u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest()<\/code>\u6765\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import zip_longest<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>for index, (item1, item2) in enumerate(zip_longest(list1, list2, fillvalue=None)):<\/p>\n<p>    print(index, item1, item2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>0 1 a<\/p>\n<p>1 2 b<\/p>\n<p>2 3 None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e0d\u4f1a\u56e0\u4e3a\u5217\u8868\u957f\u5ea6\u4e0d\u540c\u800c\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\u6709\u591a\u79cd\u65b9\u6cd5<\/strong>\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002<strong><code>zip()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u548c\u6700\u7b80\u6d01\u7684\u89e3\u51b3\u65b9\u6848<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<strong>\u5229\u7528\u7d22\u5f15\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u529f\u80fd<\/strong>\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u8bbf\u95ee\u7d22\u5f15\u6216\u751f\u6210\u65b0\u5217\u8868\u65f6\u3002<strong>\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u548c<code>enumerate()<\/code>\u51fd\u6570<\/strong>\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u529f\u80fd\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\uff0c\u8fd8\u80fd\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u7075\u6d3b\u6027\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\u7684\u6280\u5de7\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\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\uff09\u6253\u5305\u6210\u4e00\u4e2a\u4e2a\u5143\u7ec4\uff0c\u4fbf\u4e8e\u540c\u65f6\u8bbf\u95ee\u6bcf\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3]\nlist2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\nfor num, letter in zip(list1, list2):\n    print(num, letter)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u5355\u6613\u61c2\uff0c\u8fd8\u80fd\u4fdd\u6301\u5217\u8868\u4e2d\u5143\u7d20\u7684\u5bf9\u5e94\u5173\u7cfb\u3002<\/p>\n<p><strong>\u4f7f\u7528<code>enumerate()<\/code>\u53ef\u4ee5\u540c\u65f6\u904d\u5386\u5217\u8868\u53ca\u5176\u7d22\u5f15\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c<code>enumerate()<\/code>\u51fd\u6570\u53ef\u4ee5\u4e0e<code>zip()<\/code>\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u4fbf\u5728\u904d\u5386\u591a\u4e2a\u5217\u8868\u7684\u540c\u65f6\u83b7\u53d6\u7d22\u5f15\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [10, 20, 30]\nlist2 = [&#39;x&#39;, &#39;y&#39;, &#39;z&#39;]\nfor index, (num, letter) in enumerate(zip(list1, list2)):\n    print(index, num, letter)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u904d\u5386\u7684\u540c\u65f6\u83b7\u53d6\u5230\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<p><strong>\u5728\u904d\u5386\u65f6\u5982\u4f55\u5904\u7406\u4e24\u4e2a\u5217\u8868\u957f\u5ea6\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5f53\u4e24\u4e2a\u5217\u8868\u957f\u5ea6\u4e0d\u4e00\u81f4\u65f6\uff0c<code>zip()<\/code>\u4f1a\u4ee5\u8f83\u77ed\u7684\u5217\u8868\u4e3a\u51c6\uff0c\u53ea\u904d\u5386\u5230\u6700\u77ed\u5217\u8868\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002\u82e5\u9700\u8981\u904d\u5386\u5230\u6700\u957f\u5217\u8868\u7684\u7ed3\u675f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest()<\/code>\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from itertools import zip_longest\n\nlist1 = [1, 2]\nlist2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]\nfor num, letter in zip_longest(list1, list2, fillvalue=&#39;N\/A&#39;):\n    print(num, letter)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u786e\u4fdd\u6bcf\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u90fd\u88ab\u904d\u5386\uff0c\u540c\u65f6\u7528<code>fillvalue<\/code>\u586b\u5145\u8f83\u77ed\u5217\u8868\u7684\u7f3a\u5931\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u4f7f\u7528zip()\u51fd\u6570\u3001\u5229\u7528\u7d22\u5f15\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u5217\u8868\u3002zip() [&hellip;]","protected":false},"author":3,"featured_media":1122855,"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\/1122849"}],"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=1122849"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122849\/revisions"}],"predecessor-version":[{"id":1122858,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122849\/revisions\/1122858"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1122855"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1122849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1122849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1122849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}