{"id":260284,"date":"2023-01-20T08:36:49","date_gmt":"2023-01-20T00:36:49","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=260284"},"modified":"2022-12-27T21:37:41","modified_gmt":"2022-12-27T13:37:41","slug":"python-using-zip","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-using-zip.html","title":{"rendered":"python\u4e2d\u4f7f\u7528zip\u51fd\u6570\u7684\u4e03\u91cd\u5883\u754c"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPython\u4e2d\u6709\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u975e\u5e38\u4f18\u96c5\u3002\u200b\u200bzip\u200b\u200b \u51fd\u6570\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\uff0c\u4f46\u662fzip \u51fd\u6570\u7684\u4f7f\u7528\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\u4e0d\u662f\u5f88\u76f4\u89c2\uff0c\u6709\u65f6\u5bb9\u6613\u51fa\u9519\u3002\u56e0\u6b64\u672c\u6587\u5c06\u4ece7\u4e2a\u5c42\u6b21\u6765\u7531\u6d45\u5165\u6df1\u5730\u6765\u63a2\u8ba8\u5f3a\u5927\u7684\u200b\u200bzip \u200b\u200b\u51fd\u6570\u7684\u6982\u5ff5\u3001\u7528\u6cd5\u548c\u6280\u5de7\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e86\u89e3zip\u51fd\u6570\u57fa\u7840\u8bed\u6cd5<\/strong><\/div>\n

\u200b\u200bzip\u200b\u200b \u51fd\u6570\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\u200b\u200biterator\u200b\u200b\u3002
\n\u4e3e\u4f8b\u5982\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4ee5\u5982\u4e0b\u65b9\u5f0f\u6765\u7ec4\u5408\u4e24\u4e2a\u5217\u8868\uff0c\u6837\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nid = [1, 2, 3, 4]\r\nleaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']\r\nrecord = zip(id, leaders)\r\n\r\nprint(record)\r\n# \r\n\r\nprint(list(record))\r\n# [(1, 'Elon Mask'), (2, 'Tim Cook'), (3, 'Bill Gates'), (4, 'Bai Li')]\r\n<\/zip><\/pre>\n

\u5982\u4e0a\u9762\u7684\u793a\u4f8b\u6240\u793a\uff0c\u200b\u200bzip\u200b\u200b \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u8fed\u4ee3\u5668\uff0c\u5176\u4e2d\u7b2ci \u4e2a\u5143\u7ec4\u5305\u542b\u6bcf\u4e2a\u5217\u8868\u4e2d\u7684\u7b2c\u200b\u200bi \u200b\u200b\u4e2a\u5143\u7d20\u3002<\/p>\n

zip\u51fd\u6570\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5bf9\u8c61<\/strong><\/div>\n

\u4e8b\u5b9e\u4e0a\uff0cPython\u4e2d\u7684\u200b\u200bzip \u200b\u200b\u51fd\u6570\u5177\u6709\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u5b83\u53ef\u4ee5\u4e00\u6b21\u5904\u7406\u4efb\u610f\u6570\u91cf\u7684\u53ef\u8fed\u4ee3\u9879\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u4e24\u4e2a\u3002<\/p>\n

\u9996\u5148\uff0c\u6211\u4eec\u6765\u770b\u5982\u679c\u6211\u4eec\u5c06\u4e00\u4e2alist \u4f20\u9012\u7ed9\u200b\u200b zip\u200b\u200b \u51fd\u6570\uff0c\u6837\u4f8b\u5982\u4e0b\uff1a<\/p>\n

\r\nid = [1, 2, 3, 4]\r\nrecord = zip(id)\r\nprint(list(record))\r\n# [(1,), (2,), (3,), (4,)]\r\n<\/pre>\n

\u5982\u679c\u6211\u4eec\u540c\u65f6\u4f20\u9012\u4e09\u4e2a\u200b\u200blist \u200b\u200b\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\r\nid = [1, 2, 3, 4]\r\nleaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']\r\nsex = ['m', 'm', 'm', 'm']\r\nrecord = zip(id, leaders, sex)\r\n\r\nprint(list(record))\r\n# [(1, 'Elon Mask', 'm'), (2, 'Tim Cook', 'm'), (3, 'Bill Gates', 'm'), (4, 'Bai Li', 'm')]\r\n<\/pre>\n

\u5982\u4e0a\u6240\u8ff0\uff0c\u65e0\u8bba\u6211\u4eec\u4f20\u9012\u7ed9\u200b\u200bzip\u200b\u200b\u51fd\u6570\u591a\u5c11\u4e2a\u53ef\u8fed\u4ee3\u9879\uff0c\u5b83\u90fd\u80fd\u6309\u7167\u6211\u4eec\u7684\u9884\u671f\u6765\u6b63\u5e38\u5de5\u4f5c\u3002
\n\u987a\u4fbf\u8bf4\u4e00\u4e0b\uff0c\u5982\u679c\u6ca1\u6709\u53c2\u6570\uff0c\u200b\u200bzip\u200b\u200b \u51fd\u6570\u53ea\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n

zip\u51fd\u6570\u5904\u7406\u957f\u5ea6\u4e0d\u7b49\u7684\u53c2\u6570<\/strong><\/div>\n

\u771f\u5b9e\u6570\u636e\u5e76\u4e0d\u603b\u662f\u5e72\u51c0\u548c\u5b8c\u6574\u7684\uff0c\u6709\u65f6\u6211\u4eec\u5fc5\u987b\u5904\u7406\u4e0d\u7b49\u957f\u7684\u53ef\u8fed\u4ee3\u6570\u636e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u200b\u200bzip\u200b\u200b\u51fd\u6570\u7684\u7ed3\u679c\u57fa\u4e8e\u6700\u77ed\u7684\u53ef\u8fed\u4ee3\u9879\u3002
\n\u4e3e\u4f8b\u5982\u4e0b\uff1a<\/p>\n

\r\nid = [1, 2]\r\nleaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']\r\nrecord = zip(id, leaders)\r\n\r\nprint(list(record))\r\n# [(1, 'Elon Mask'), (2, 'Tim Cook')]\r\n<\/pre>\n

\u5982\u4e0a\u9762\u7684\u4ee3\u7801\u6240\u793a\uff0c\u6700\u77ed\u7684\u5217\u8868\u662f\u200b\u200bid \u200b\u200b\uff0c\u56e0\u6b64\u200b\u200brecord\u200b\u200b \u53ea\u5305\u542b\u4e24\u4e2a\u5143\u7ec4\uff0c\u5e76\u4e14\u5ffd\u7565\u4e86\u5217\u8868\u200b\u200bleaders \u200b\u200b\u4e2d\u7684\u6700\u540e\u4e24\u4e2a\u5143\u7d20\u3002
\n\u5982\u679c\u6700\u540e\u4e24\u4f4d\u200b\u200bleader\u200b\u200b \u56e0\u88ab\u5ffd\u89c6\u800c\u4e0d\u9ad8\u5174\uff0c\u6211\u4eec\u8be5\u600e\u4e48\u529e\uff1f
\nPython\u5c06\u518d\u6b21\u5e2e\u52a9\u6211\u4eec\u3002\u200b\u200bitertools \u200b\u200b\u6a21\u5757\u4e2d\u8fd8\u6709\u4e00\u4e2a\u540d\u4e3a\u200b\u200bzip_langest \u200b\u200b\u7684\u51fd\u6570\u3002\u987e\u540d\u601d\u4e49\uff0c\u5b83\u662f\u200b\u200bzip\u200b\u200b\u51fd\u6570\u7684\u5144\u5f1f\uff0c\u5176\u7ed3\u679c\u57fa\u4e8e\u6700\u957f\u7684\u53c2\u6570\u3002
\n\u6211\u4eec\u4e0d\u59a8\u4f7f\u7528\u200b\u200bzip_langest \u200b\u200b\u51fd\u6570\u6765\u751f\u6210\u4e0a\u8ff0\u200b\u200brecord\u200b\u200b\u5217\u8868\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\r\nfrom itertools import zip_longest\r\nid = [1, 2]\r\nleaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']\r\n\r\nlong_record = zip_longest(id, leaders)\r\nprint(list(long_record))\r\n# [(1, 'Elon Mask'), (2, 'Tim Cook'), (None, 'Bill Gates'), (None, 'Bai Li')]\r\n\r\nlong_record_2 = zip_longest(id, leaders, fillvalue='Top')\r\nprint(list(long_record_2))\r\n# [(1, 'Elon Mask'), (2, 'Tim Cook'), ('Top', 'Bill Gates'), ('Top', 'Bai Li')]\r\n<\/pre>\n

\u5982\u4e0a\u6240\u8ff0\uff0c\u200b\u200bzip_langest\u200b\u200b \u51fd\u6570\u57fa\u4e8e\u5176\u6700\u957f\u53c2\u6570\u6765\u8fd4\u56de\u7ed3\u679c\u3002\u53ef\u9009\u7684\u200b\u200bfillvalue\u200b\u200b \u53c2\u6570\uff08\u9ed8\u8ba4\u503c\u4e3a\u200b\u200bNone\u200b\u200b \uff09\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u586b\u5145\u7f3a\u5931\u7684\u503c\u3002<\/p>\n

\u638c\u63e1unzip\u64cd\u4f5c<\/strong><\/div>\n

\u5728\u4e0a\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u9996\u5148\u83b7\u5f97\u5217\u8868\u200b\u200brecord\u200b\u200b \uff0c\u90a3\u4e48\u6211\u4eec\u5982\u4f55\u5c06\u5176\u200b\u200bunzip\u200b\u200b \u89e3\u538b\u7f29\u4e3a\u5355\u72ec\u7684\u53ef\u8fed\u4ee3\u9879\uff1f
\n\u4e0d\u5e78\u7684\u662f\uff0cPython\u5e76\u6ca1\u6709\u76f4\u63a5\u7684\u89e3\u538b\u7f29\u200b\u200bunzip \u200b\u200b\u51fd\u6570\u3002\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u719f\u6089\u661f\u53f7\u200b\u200b* \u200b\u200b\u7684\u6280\u5de7\uff0c\u89e3\u538b\u7f29\u5c06\u662f\u4e00\u9879\u975e\u5e38\u7b80\u5355\u7684\u4efb\u52a1\u3002<\/p>\n

\r\nrecord = [(1, 'Elon Mask'), (2, 'Tim Cook'), (3, 'Bill Gates'), (4, 'Bai Li')]\r\nid, leaders = zip(*record)\r\nprint(id)\r\n# (1, 2, 3, 4)\r\nprint(leaders)\r\n# ('Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li')\r\n<\/pre>\n

\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u661f\u53f7\u6267\u884c\u4e86\u62c6\u5305\u64cd\u4f5c\uff0c\u5373\u4ece\u8bb0\u5f55\u5217\u8868\u4e2d\u62c6\u5305\u6240\u6709\u56db\u4e2a\u5143\u7ec4\u3002<\/p>\n

\u901a\u8fc7zip\u51fd\u6570\u521b\u5efa\u548c\u66f4\u65b0dict<\/strong><\/div>\n

\u53d7\u76ca\u4e8e\u529f\u80fd\u5f3a\u5927\u7684\u200b\u200bzip\u200b\u200b \u51fd\u6570\uff0c\u57fa\u4e8e\u4e00\u4e9b\u72ec\u7acb\u7684\u5217\u8868\u6765\u521b\u5efa\u548c\u66f4\u65b0dict \u5c06\u975e\u5e38\u65b9\u4fbf\u3002
\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u200b\u200bone-line\u200b\u200b\u7684\u65b9\u6848\uff1a
\n\u25cf \u4f7f\u7528\u5b57\u5178\u751f\u6210\u5f0f\u548czip\u51fd\u6570
\n\u25cf \u4f7f\u7528dict\u548czip\u51fd\u6570
\n\u6837\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nid = [1, 2, 3, 4]\r\nleaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']\r\n\r\n# create dict by dict comprehension\r\nleader_dict = {i: name for i, name in zip(id, leaders)}\r\nprint(leader_dict)\r\n# {1: 'Elon Mask', 2: 'Tim Cook', 3: 'Bill Gates', 4:'Bai Li'}\r\n\r\n# create dict by dict function\r\nleader_dict_2 = dict(zip(id, leaders))\r\nprint(leader_dict_2)\r\n# {1: 'Elon Mask', 2: 'Tim Cook', 3: 'Bill Gates', 4: 'Bai Li'}\r\n\r\n# update\r\nother_id = [5, 6]\r\nother_leaders = ['Larry Page', 'Sergey Brin']\r\nleader_dict.update(zip(other_id, other_leaders))\r\nprint(leader_dict)\r\n# {1: 'Elon Mask', 2: 'Tim Cook', 3: 'Bill Gates', 4: ''Bai Li'', 5: 'Larry Page', 6: 'Sergey Brin'}\r\n<\/pre>\n

\u4e0a\u9762\u7684\u793a\u4f8b\u6839\u672c\u4e0d\u4f7f\u7528\u200b\u200bfor\u200b\u200b \u5faa\u73af , \u8fd9\u662f\u591a\u4e48\u7684\u4f18\u96c5\u548c\u200b\u200bPythonic\u200b\u200b\uff01<\/p>\n

\u5728for\u5faa\u73af\u4e2d\u4f7f\u7528zip\u51fd\u6570<\/strong><\/div>\n

\u540c\u65f6\u5904\u7406\u591a\u4e2a\u53ef\u8fed\u4ee3\u9879\u901a\u5e38\u662f\u5e38\u89c1\u7684\u573a\u666f\uff0c \u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u5728\u200b\u200bfor\u200b\u200b\u5faa\u73af\u4e2d\u914d\u5408\u4f7f\u7528\u51fd\u6570\u200b\u200bzip\u200b\u200b \uff0c\u8fd9\u4e5f\u662f\u6211\u6700\u559c\u6b22\u51fd\u6570\u200b\u200bzip \u200b\u200b\u7684\u7528\u6cd5\u4e4b\u4e00\u3002
\n\u4e3e\u4f8b\u5982\u4e0b\uff1a<\/p>\n

\r\nproducts = [\"cherry\", \"strawberry\", \"banana\"]\r\nprice = [2.5, 3, 5]\r\ncost = [1, 1.5, 2]\r\nfor prod, p, c in zip(products, price, cost):\r\n    print(f'The profit of a box of {prod} is \u00a3{p-c}!')\r\n# The profit of a box of cherry is \u00a31.5!\r\n# The profit of a box of strawberry is \u00a31.5!\r\n# The profit of a box of banana is \u00a33!\r\n<\/pre>\n
\u5b9e\u73b0\u77e9\u9635\u8f6c\u7f6e<\/strong><\/div>\n

\u6211\u4eec\u6765\u770b\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n

\u5982\u4f55\u4f18\u96c5\u5730\u5b9e\u73b0\u77e9\u9635\u7684\u8f6c\u7f6e\u64cd\u4f5c\uff1f<\/p>\n

Wow, \u9274\u4e8e\u5728\u4e0a\u6587\u4e2d\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u51fd\u6570\u200b\u200bzip\u200b\u200b \uff0c\u661f\u53f7\u200b\u200b*\u200b\u200b ,\u4ee5\u53ca\u5217\u8868\u751f\u6210\u5f0f\uff0c\u6240\u4ee5\u200b\u200bone-line\u200b\u200b \u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n

\r\nmatrix = [[1, 2, 3], [1, 2, 3]]\r\nmatrix_T = [list(i) for i in zip(*matrix)]\r\nprint(matrix_T)\r\n# [[1, 1], [2, 2], [3, 3]]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u200b\u200bzip\u200b\u200b \u51fd\u6570\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\u200b […]<\/p>\n","protected":false},"author":1903,"featured_media":260311,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-260284","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=260284"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260284\/revisions"}],"predecessor-version":[{"id":260316,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260284\/revisions\/260316"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/260311"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=260284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=260284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=260284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}