{"id":310211,"date":"2024-05-20T21:44:41","date_gmt":"2024-05-20T13:44:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/310211.html"},"modified":"2024-05-20T21:44:58","modified_gmt":"2024-05-20T13:44:58","slug":"python-%e5%ad%97%e5%85%b8%e4%b8%ad-zip-%e6%96%b9%e6%b3%95%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/310211.html","title":{"rendered":"python \u5b57\u5178\u4e2d zip \u65b9\u6cd5\u600e\u4e48\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26230720\/efdf791b-59c9-4c76-829d-60f9353b7c7f.webp\" alt=\"python \u5b57\u5178\u4e2d zip \u65b9\u6cd5\u600e\u4e48\u7528\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>zip<\/code>\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u5c06\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u5bf9\u8c61\u4e2d\u5bf9\u5e94\u7684\u5143\u7d20\u6253\u5305\u6210\u4e00\u4e2a\u4e2a\u5143\u7ec4\uff0c\u7136\u540e\u8fd4\u56de\u7531\u8fd9\u4e9b\u5143\u7ec4\u7ec4\u6210\u7684\u5217\u8868\u3002<strong>\u5f53\u7528\u5728\u5b57\u5178\u4e0a\u65f6\uff0c<code>zip<\/code>\u53ef\u4ee5\u7528\u4e8e\u540c\u65f6\u904d\u5386\u5b57\u5178\u7684\u952e\u548c\u503c\u3001\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3001\u6216\u5c06\u952e\u503c\u5bf9\u9006\u8f6c\uff0c\u4f7f\u5f97\u5b57\u5178\u7684\u952e\u53d8\u6210\u503c\uff0c\u503c\u53d8\u6210\u952e\u3002<\/strong> \u4f7f\u7528<code>zip<\/code>\u8fdb\u884c\u5b57\u5178\u904d\u5386\u662f\u5176\u6700\u5e38\u89c1\u7684\u5e94\u7528\u4e4b\u4e00\u3002\u901a\u8fc7\u5c06<code>dict.items()<\/code>\u4f5c\u4e3a<code>zip<\/code>\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u5230\u5b57\u5178\u7684\u952e\u548c\u503c\uff0c\u8fd9\u6837\u505a\u6bd4\u4f20\u7edf\u7684\u904d\u5386\u65b9\u6cd5\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u672c\u6587\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd<code>zip<\/code>\u5728\u5904\u7406Python\u5b57\u5178\u4e2d\u7684\u5e94\u7528\u53ca\u76f8\u5173\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b57\u5178\u7684\u540c\u65f6\u952e\u503c\u904d\u5386<\/h2>\n<\/p>\n<p><p>\u901a\u5e38\uff0c\u904d\u5386\u5b57\u5178\u65f6\u6211\u4eec\u53ef\u80fd\u53ea\u5173\u6ce8\u952e\u6216\u8005\u503c\u3002\u4f46\u5f53\u6211\u4eec\u9700\u8981\u540c\u65f6\u83b7\u53d6\u952e\u548c\u503c\u65f6\uff0c\u76f4\u63a5\u4f7f\u7528<code>zip<\/code>\u6765\u904d\u5386<code>dict.items()<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u8fd9\u91cc\uff0c<code>dict.items()<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u89c6\u56fe\uff0c\u6bcf\u4e2a\u5143\u7ec4\u5305\u542b\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u7136\u540e\u901a\u8fc7<code>zip<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u952e\u548c\u503c\u201c\u89e3\u538b\u201d\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#039;apple&#039;: 1, &#039;banana&#039;: 2, &#039;cherry&#039;: 3}<\/p>\n<p>for key, value in zip(my_dict.keys(), my_dict.values()):<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5e94\u7528\u89e3\u8bfb<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>my_dict.keys()<\/code>\u548c<code>my_dict.values()<\/code>\u5206\u522b\u8fd4\u56de\u5b57\u5178\u7684\u952e\u548c\u503c\u7684\u89c6\u56fe\u3002\u4f7f\u7528<code>zip<\/code>\u5c06\u5b83\u4eec\u5408\u5e76\u8d77\u6765\uff0c\u5c31\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u8bbf\u95ee\u952e\u548c\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u4e8e\u9700\u8981\u5bf9\u5b57\u5178\u7684\u952e\u548c\u503c\u8fdb\u884c\u914d\u5bf9\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5408\u5e76\u4e24\u4e2a\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u4f7f\u7528\u573a\u666f\u662f\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002\u5982\u679c\u6709\u4e24\u4e2a\u5b57\u5178\uff0c\u4e00\u4e2a\u5b58\u50a8\u7684\u662f\u7269\u54c1\u540d\u79f0\uff0c\u53e6\u4e00\u4e2a\u5b58\u50a8\u7684\u662f\u76f8\u5e94\u7684\u4ef7\u683c\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u60f3\u8981\u5c06\u5b83\u4eec\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;]<\/p>\n<p>values = [1, 2, 3]<\/p>\n<p>combined_dict = dict(zip(keys, values))<\/p>\n<p>print(combined_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5e94\u7528\u89e3\u8bfb<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>zip<\/code>\u65b9\u6cd5\u5c06<code>keys<\/code>\u5217\u8868\u548c<code>values<\/code>\u5217\u8868\u4e2d\u5bf9\u5e94\u4f4d\u7f6e\u7684\u5143\u7d20\u6253\u5305\u6210\u5143\u7ec4\uff0c\u7136\u540e<code>dict()<\/code>\u51fd\u6570\u5c06\u8fd9\u4e9b\u5143\u7ec4\u8f6c\u6362\u6210\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5408\u5e76\u4e24\u4e2a\u5177\u6709\u76f8\u5173\u6027\u7684\u5217\u8868\u53d8\u5f97\u5f02\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u952e\u503c\u5bf9\u7684\u9006\u8f6c<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u5178\u4e2d\u7684\u952e\u548c\u503c\u4e92\u6362\uff0c\u5373\u539f\u5148\u7684\u952e\u6210\u4e3a\u503c\uff0c\u539f\u5148\u7684\u503c\u6210\u4e3a\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">original_dict = {&#039;apple&#039;: 1, &#039;banana&#039;: 2, &#039;cherry&#039;: 3}<\/p>\n<p>inverted_dict = {value: key for key, value in original_dict.items()}<\/p>\n<p>print(inverted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5e94\u7528\u89e3\u8bfb<\/h3>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528\u4e86\u5b57\u5178\u63a8\u5bfc\u5f0f\u548c<code>dict.items()<\/code>\u65b9\u6cd5\u83b7\u53d6\u539f\u5b57\u5178\u7684\u952e\u503c\u5bf9\uff0c\u7136\u540e\u901a\u8fc7 <code>{value: key}<\/code> \u7684\u5f62\u5f0f\u5c06\u5b83\u4eec\u9006\u8f6c\u3002\u867d\u7136\u8fd9\u4e2a\u4f8b\u5b50\u6ca1\u6709\u76f4\u63a5\u4f7f\u7528<code>zip<\/code>\uff0c\u4f46\u5b83\u5c55\u793a\u4e86\u5904\u7406\u952e\u503c\u5bf9\u7684\u53e6\u4e00\u79cd\u601d\u8def\uff0c\u4e0e<code>zip<\/code>\u5728\u5904\u7406\u5b57\u5178\u65f6\u7684\u601d\u60f3\u662f\u76f8\u901a\u7684\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528zip\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784<\/h2>\n<\/p>\n<p><p><code>zip<\/code>\u65b9\u6cd5\u53ef\u4ee5\u548c\u5b57\u5178\u4e00\u8d77\u4f7f\u7528\uff0c\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u5178\u7684\u952e\u662f\u5143\u7ec4\uff0c\u8868\u793a\u591a\u4e2a\u7ef4\u5ea6\u7684\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>zip<\/code>\u6765\u89e3\u538b\u8fd9\u4e9b\u7ef4\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">complex_dict = {(&#039;apple&#039;, &#039;red&#039;): 1, (&#039;banana&#039;, &#039;yellow&#039;): 2}<\/p>\n<p>for (fruit, color), value in complex_dict.items():<\/p>\n<p>    print(f&quot;The {fruit} is {color}, and its value is {value}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5e94\u7528\u89e3\u8bfb<\/h3>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>zip<\/code>\u6765\u904d\u5386\u4e00\u4e2a\u952e\u662f\u590d\u5408\u5143\u7d20\uff08\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\u662f\u5143\u7ec4\uff09\u7684\u5b57\u5178\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u88ab\u5355\u72ec\u8bbf\u95ee\u548c\u5904\u7406\uff0c\u4f7f\u5f97\u4ee3\u7801\u65e2\u6e05\u6670\u53c8\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<code>zip<\/code>\u65b9\u6cd5\u5728\u5904\u7406Python\u5b57\u5178\u65f6\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u5904\u7406\u573a\u666f\u3002\u5b83\u662fPython\u7f16\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u503c\u5f97\u6bcf\u4e2aPython\u5f00\u53d1\u8005\u638c\u63e1\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528 Python \u5b57\u5178\u7684 zip \u65b9\u6cd5\uff1f<\/strong><\/p>\n<p>Python \u4e2d\u7684\u5b57\u5178\u5bf9\u8c61\u6709\u4e00\u4e2a\u540d\u4e3a &quot;zip&quot; \u7684\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e24\u4e2a\u5e8f\u5217\u76f8\u4e92\u914d\u5bf9\uff0c\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u5bf9\u8c61\u3002\u4e0b\u9762\u662f\u4f7f\u7528 zip \u65b9\u6cd5\u7684\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u5e8f\u5217\nkeys = [&#039;name&#039;, &#039;age&#039;, &#039;country&#039;]\nvalues = [&#039;John&#039;, 25, &#039;USA&#039;]\n\n# \u4f7f\u7528 zip \u65b9\u6cd5\u521b\u5efa\u5b57\u5178\ndictionary = dict(zip(keys, values))\nprint(dictionary)\n<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<code>{&#039;name&#039;: &#039;John&#039;, &#039;age&#039;: 25, &#039;country&#039;: &#039;USA&#039;}<\/code><\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5e8f\u5217\uff1a\u4e00\u4e2a\u5305\u542b\u952e\uff0c\u4e00\u4e2a\u5305\u542b\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>zip<\/code> \u65b9\u6cd5\u914d\u5bf9\u8fd9\u4e24\u4e2a\u5e8f\u5217\uff0c\u5e76\u4f7f\u7528 <code>dict<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u5bf9\u8c61\u3002<\/p>\n<p><strong>2. zip \u65b9\u6cd5\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p><code>zip<\/code> \u65b9\u6cd5\u7528\u4e8e\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5e8f\u5217\u6309\u987a\u5e8f\u914d\u5bf9\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u7ec4\u6210\u7684\u65b0\u5e8f\u5217\u3002\u8fd9\u4e2a\u65b0\u5e8f\u5217\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u5217\u8868\u3001\u5b57\u5178\u6216\u76f4\u63a5\u7528\u4e8e\u8fed\u4ee3\u3002\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u5e38\u7528\u7684\u7528\u9014\u662f\u5c06\u4e24\u4e2a\u5e8f\u5217\u76f8\u4e92\u914d\u5bf9\uff0c\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<p><strong>3. zip \u65b9\u6cd5\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\u5417\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 <code>zip<\/code> \u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u4f20\u5165\u7684\u5e8f\u5217\u957f\u5ea6\u76f8\u540c\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u914d\u5bf9\u4e0d\u51c6\u786e\u3002\u5982\u679c\u4f20\u5165\u7684\u5e8f\u5217\u4e2d\u6709\u4e00\u4e2a\u957f\u5ea6\u8f83\u77ed\uff0c\u90a3\u4e48 <code>zip<\/code> \u65b9\u6cd5\u4f1a\u505c\u6b62\u5728\u6700\u77ed\u7684\u5e8f\u5217\u7ed3\u675f\u5904\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528 <code>zip<\/code> \u65b9\u6cd5\u65f6\uff0c\u8bf7\u786e\u4fdd\u5e8f\u5217\u957f\u5ea6\u76f8\u7b26\uff0c\u4ee5\u514d\u51fa\u73b0\u9519\u8bef\u7684\u914d\u5bf9\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0czip\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u5c06\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u5bf9\u8c61\u4e2d\u5bf9\u5e94\u7684\u5143\u7d20\u6253\u5305\u6210\u4e00\u4e2a\u4e2a\u5143\u7ec4\uff0c\u7136\u540e\u8fd4\u56de\u7531\u8fd9\u4e9b [&hellip;]","protected":false},"author":3,"featured_media":310228,"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\/310211"}],"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=310211"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/310211\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/310228"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=310211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=310211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=310211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}