1\u3001zip\u5c06\u5217\u8868\u3001\u5143\u7ec4\u6216\u5176\u4ed6\u5e8f\u5217\u7684\u5143\u7d20\u8fdb\u884c\u914d\u5bf9<\/strong><\/span><\/div>\n\u65b0\u5efa\u6210\u4e00\u4e2a\u5143\u7ec4\u6784\u6210\u7684\u5217\u8868\uff0c\u5b83\u751f\u6210\u5217\u8868\u957f\u5ea6\u7531\u6700\u77ed\u7684\u5e8f\u5217\u51b3\u5b9a\uff1a<\/p>\n
#zip\r\nseq1=['foo','bar','baz']\r\nseq2=['one','two','three']\r\nzipped=zip(seq1,seq2)\r\nlist(zipped)\r\n>>>\r\n>[('foo', 'one'), ('bar', 'two'), ('baz', 'three')]\r\n \r\nseq3=[True,False]\r\nzipped2=zip(seq1,seq2,seq3)\r\nlist(zipped2)\r\n>>>\r\n>\r\n[('foo', 'one', True), ('bar', 'two', False)]<\/pre>\n2\u3001zip\u7684\u5e38\u7528\u573a\u666f\u4e3a\u540c\u65f6\u904d\u5386\u591a\u4e2a\u5e8f\u5217<\/strong><\/span><\/div>\n\u6709\u65f6\u548cenumerate\u642d\u914d\u4f7f\u7528\uff1a<\/p>\n
mapping={}\r\nfor i,(a,b) in enumerate(zip(seq1,seq2)):\r\n print('{0},{1},{2}'.format(i,a,b))\r\n mapping[i]=(a,b)\r\nprint(mapping)\r\n>>>\r\n>0,foo,one\r\n1,bar,two\r\n2,baz,three\r\n{0: ('foo', 'one'), 1: ('bar', 'two'), 2: ('baz', 'three')}<\/pre>\n3\u3001\u5bf9\u4e8e\u5df2\u201c\u914d\u5bf9\u201d\u7684\u5e8f\u5217<\/strong><\/span><\/div>\n\u5229\u7528zip\u51fd\u6570\u8fdb\u884c\u62c6\u5206<\/p>\n
pitchers=[('a','1'),('b','2'),('c','3')]\r\nfirst_names,last_names=zip(*pitchers)\r\nprint(first_names,last_names)\r\n>>>\r\n>('a', 'b', 'c') ('1', '2', '3')<\/pre>\n\u8bf4\u8bf4zip\u51fd\u6570\u7684\u4e3b\u8981\u7528\u9014<\/strong><\/div>\n\u4e2a\u4eba\u7406\u89e3<\/strong><\/span><\/div>\nzip\u51fd\u6570\u4e3b\u8981\u7528\u6765\u5c06n\u4e2alist\u4e2d\u76f8\u540clocation\u7684\u5143\u7d20\u53d6\u51fa\u6765\uff0c\u7ec4\u6210\u65b0\u7684data<\/p>\n
years = [22,23,24]\r\nnames = ['hjw','hyr','lyq']\r\n \r\na = zip(names,years) \r\nprint(a) # \r\nprint(list(a)) # [('hjw', 22), ('hyr', 23), ('lyq', 24)]\r\n \r\ndata = []\r\nfor (year,name) in zip(years,names):\r\n data.append({'name':name,'year':year})\r\nprint(data)\r\n# [{'name': 'hjw', 'year': 22}, {'name': 'hyr', 'year': 23}, {'name': 'lyq', 'year': 24}]<\/zip><\/pre>\n","protected":false},"excerpt":{"rendered":"\u65b0\u5efa\u6210\u4e00\u4e2a\u5143\u7ec4\u6784\u6210\u7684\u5217\u8868\uff0c\u5b83\u751f\u6210\u5217\u8868\u957f\u5ea6\u7531\u6700\u77ed\u7684\u5e8f\u5217\u51b3\u5b9a\uff1a #zip seq1=[‘foo’,’bar’,’b […]<\/p>\n","protected":false},"author":1482,"featured_media":206651,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-265384","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\/265384","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\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=265384"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265384\/revisions"}],"predecessor-version":[{"id":265390,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265384\/revisions\/265390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/206651"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=265384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=265384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=265384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}