{"id":277599,"date":"2021-01-14T12:20:24","date_gmt":"2021-01-14T04:20:24","guid":{"rendered":"http:\/\/4563.org\/?p=277599"},"modified":"2021-01-14T12:20:24","modified_gmt":"2021-01-14T04:20:24","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-enumerate-%e8%a3%85%e9%a5%b0","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=277599","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 enumerate \u88c5\u9970?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5982\u4f55\u4f7f\u7528 enumerate \u88c5\u9970?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fanqieipnet <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u5982\u4f55\u4f7f\u7528 enumerate \u88c5\u9970?\u4eca\u5929\u756a\u8304\u52a0\u901f\u5c31\u6765\u8bb2\u4e00\u4e0b\u3002<\/p>\n<p>\u3000\u3000 enumerate \u88c5\u9970<\/p>\n<p>\u3000\u3000 for i, it in enumerate(iterators):<\/p>\n<p>\u3000\u3000&#8230;<\/p>\n<p>\u3000\u3000 iterators[i] = repeat(fillvalue)<\/p>\n<p>\u3000\u3000 enumerate \u88c5\u9970\u540e\u4f1a\u5f97\u5230\u4e00\u4e2a\u7531 index \u548c\u5143\u7d20\u672c\u8eab\u7ec4\u6210\u7684 tuple\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<p>\u3000\u3000 In [1]: for i,ele in enumerate([9,4,2,5,8]):<\/p>\n<p>\u3000\u3000&#8230;: print((i,ele))<\/p>\n<p>\u3000\u3000&#8230;:<\/p>\n<p>\u3000\u3000(0, 9)<\/p>\n<p>\u3000\u3000(1, 4)<\/p>\n<p>\u3000\u3000(2, 2)<\/p>\n<p>\u3000\u3000(3, 5)<\/p>\n<p>\u3000\u3000(4, 8)<\/p>\n<p>\u3000\u3000\u6b64\u5904\u4f7f\u7528 enumerate\uff0c\u56e0\u540e\u9762\u5148\u904d\u5386\u5b8c\u7684 iter(a)\uff0c\u4e3a\u4fdd\u8bc1\u548c iter(b)\u4e2a\u6570\u5bf9\u9f50\uff0c\u9700\u8981\u586b\u5145 fillvalue \u503c\uff0c\u9700\u8981\u77e5\u9053 a \u7684 index\uff0c\u6b64\u5904\u4e3a 0.<\/p>\n<p>\u3000\u3000\u6355\u83b7\u8fed\u4ee3\u7ec8\u6b62\u5f02\u5e38<\/p>\n<p>\u3000\u3000 try:<\/p>\n<p>\u3000\u3000 value = next(it)<\/p>\n<p>\u3000\u3000 except StopIteration:<\/p>\n<p>\u3000\u3000&#8230;<\/p>\n<p>\u3000\u3000 try\u2026 except \u662f\u5f02\u5e38\u6355\u83b7\u7684\u6807\u51c6\u6a21\u677f\uff0cnext(it)\u7b2c\u4e00\u6b21\u6267\u884c\u8fd4\u56de it \u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0ca=[1,2,3]\uff0c\u6240\u4ee5 value \u503c\u4e3a 1.<\/p>\n<p>\u3000\u3000\u8ba1\u6570<\/p>\n<p>\u3000\u3000 num_active \u8868\u793a\u5f53\u524d\u5b58\u6d3b\u7684\u5217\u8868\u4e2a\u6570\uff0c\u4e00\u65e6\u4e00\u4e2a\u5217\u8868\u8fed\u4ee3\u5230\u7ec8\u70b9\uff0cnum_active \u7acb\u5373\u51cf 1<\/p>\n<p>\u3000\u3000 num_active -= 1<\/p>\n<p>\u3000\u3000 repeat<\/p>\n<p>\u3000\u3000 iterators[i] = repeat(fillvalue)<\/p>\n<p>\u3000\u3000 repeat \u662f itertools \u6a21\u5757\u5185\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5982\u679c times \u4e3a\u9ed8\u8ba4\u503c\uff0c\u5219\u8868\u793a\u91cd\u590d\u65e0\u9650\u6b21\uff0c\u6b64\u5904\u662f\u4e00\u4e2a repeat \u7684\u5178\u578b\u7528\u6cd5\u3002\u6b64\u5904\u7a0d\u52a0\u6ce8\u610f\uff0c\u4e0d\u8981\u4ee5\u4e3a times \u53c2\u6570\u9ed8\u8ba4\u503c\u4e3a 1\uff0c\u6216\u8005\u4e0d\u7406\u89e3\u4e3a\u4ec0\u4e48\u8981\u65e0\u9650\u6b21\u91cd\u590d\uff0c\u8bb0\u4f4f\u8fd9\u79cd\u7528\u6cd5\u3002<\/p>\n<p>\u3000\u3000\u6b64\u5904\uff0crepeat \u8fd4\u56de\u4e00\u4e2a\u65e0\u9650\u6b21\u91cd\u590d fillvalue \u503c\u7684\u8fed\u4ee3\u5668\uff0c\u5e76\u8d4b\u503c\u7ed9\u6b64\u65f6\u89e6\u53d1 StopIteration \u5f02\u5e38\u7684\u5217\u8868\u3002\u6b64\u5904\u5c31\u662f iter(a)\uff0c\u4ee4\u5176\u6307\u5411 repeat \u521b\u5efa\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<p>\u3000\u3000 yield \u8fd4\u56de\u503c<\/p>\n<p>\u3000\u3000 value = fillvalue<\/p>\n<p>\u3000\u3000 values.append(value)<\/p>\n<p>\u3000\u3000 yield tuple(values)<\/p>\n<p>\u3000\u3000\u7b2c\u4e00\u53e5\uff0c\u662f\u5728\u89e6\u53d1 StopIteration \u65f6\uff0c\u5c06\u9ed8\u8ba4\u503c fillvalue \u8d4b\u7ed9 value\uff0cvalues \u4fdd\u5b58\u4f4f\u5217\u8868\u5bf9\u9f50\u540e\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u7b2c\u4e00\u6b21 for \u9000\u51fa\u65f6\uff0cvalues \u5185\u503c\u4e3a[1,4]      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 enumerate \u88c5\u9970&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/277599"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=277599"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/277599\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=277599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=277599"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=277599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}