{"id":284724,"date":"2021-01-16T22:08:19","date_gmt":"2021-01-16T14:08:19","guid":{"rendered":"http:\/\/4563.org\/?p=284724"},"modified":"2021-01-16T22:08:19","modified_gmt":"2021-01-16T14:08:19","slug":"%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e5%88%97%e8%a1%a8%e7%9a%84%e7%b4%af%e7%a7%af%e6%b1%87%e6%80%bb%e5%80%bc","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=284724","title":{"rendered":"\u5982\u4f55\u8fd4\u56de\u5217\u8868\u7684\u7d2f\u79ef\u6c47\u603b\u503c?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5982\u4f55\u8fd4\u56de\u5217\u8868\u7684\u7d2f\u79ef\u6c47\u603b\u503c?               <\/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\u8fd4\u56de\u5217\u8868\u7684\u7d2f\u79ef\u6c47\u603b\u503c?\u4eca\u5929\u756a\u8304\u52a0\u901f\u5c31\u6765\u8bb2\u4e00\u4e0b\u3002<\/p>\n<p>\u3000\u3000\u8fd4\u56de\u5217\u8868\u7684\u7d2f\u79ef\u6c47\u603b\u503c\uff0c\u539f\u578b\uff1a<\/p>\n<p>\u3000\u3000 accumulate(iterable[, func, *, initial=None])<\/p>\n<p>\u3000\u3000\u5e94\u7528\u5982\u4e0b\uff1a<\/p>\n<p>\u3000\u3000 In [36]: list(accumulate([1,2,3,4,5,6],lambda x,y: x*y))<\/p>\n<p>\u3000\u3000 Out[36]: [1, 2, 6, 24, 120, 720]<\/p>\n<p>\u3000\u3000 accumulate \u5927\u6982\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>\u3000\u3000 def accumulate(iterable, func=operator.add, *, initial=None):<\/p>\n<p>\u3000\u3000 it = iter(iterable)<\/p>\n<p>\u3000\u3000 total = initial<\/p>\n<p>\u3000\u3000 if initial is None:<\/p>\n<p>\u3000\u3000 try:<\/p>\n<p>\u3000\u3000 total = next(it)<\/p>\n<p>\u3000\u3000 except StopIteration:<\/p>\n<p>\u3000\u3000 return<\/p>\n<p>\u3000\u3000 yield total<\/p>\n<p>\u3000\u3000 for element in it:<\/p>\n<p>\u3000\u3000 total = func(total, element)<\/p>\n<p>\u3000\u3000 yield total<\/p>\n<p>\u3000\u3000\u4ee5\u4e0a\u4ee3\u7801\uff0c\u4f60\u8fd8\u597d\u5417?\u4e0e chain \u7b80\u5355\u7684 yield \u4e0d\u540c\uff0c\u6b64\u5904\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\uff0cyield \u6709\u70b9\u50cf return\uff0c\u6240\u4ee5 yield total \u90a3\u884c\u76f4\u63a5\u5c31\u8fd4\u56de\u4e00\u4e2a\u5143\u7d20\uff0c\u4e5f\u5c31\u662f iterable \u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u56e0\u4e3a\u4efb\u4f55\u65f6\u5019\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5c31\u662f\u5b83\u7684\u7b2c\u4e00\u4e2a\u3002\u53c8\u56e0\u4e3a yield \u8fd4\u56de\u7684\u662f\u4e00\u4e2a generator \u5bf9\u8c61\uff0c\u6bd4\u5982\u540d\u5b57 gen\uff0c\u6240\u4ee5 next(gen)\u65f6\uff0c\u4ee3\u7801\u5c06\u4f1a\u6267\u884c\u5230 for element in it:\u8fd9\u884c\uff0c\u800c\u6b64\u65f6\u7684\u8fed\u4ee3\u5668 it \u5df2\u7ecf\u6307\u5230 iterable \u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0cOK\uff0c\u76f8\u4fe1\u4f60\u61c2\u4e86!<\/p>\n<p>\u3000\u3000\u6f0f\u6597\u7b5b\u9009<\/p>\n<p>\u3000\u3000\u5b83\u662f compress \u51fd\u6570\uff0c\u529f\u80fd\u7c7b\u4f3c\u4e8e\u6f0f\u6597\u529f\u80fd\uff0c\u6240\u4ee5\u6211\u79f0\u5b83\u4e3a\u6f0f\u6597\u7b5b\u9009\uff0c\u539f\u578b\uff1a<\/p>\n<p>\u3000\u3000 compress(data, selectors)<\/p>\n<p>\u3000\u3000 In [38]: list(compress(&#8216;abcdefg&#8217;,[1,1,0,1]))<\/p>\n<p>\u3000\u3000 Out[38]: [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;d&#8217;]<\/p>\n<p>\u3000\u3000\u5bb9\u6613\u770b\u51fa\uff0ccompress \u8fd4\u56de\u7684\u5143\u7d20\u4e2a\u6570\u7b49\u4e8e\u4e24\u4e2a\u53c2\u6570\u4e2d\u8f83\u77ed\u7684\u5217\u8868\u957f\u5ea6\u3002<\/p>\n<p>\u3000\u3000\u5b83\u7684\u5927\u6982\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<p>\u3000\u3000 def compress(data, selectors):<\/p>\n<p>\u3000\u3000 return (d for d, s in zip(data, selectors) if s)<\/p>\n<p>\u3000\u3000\u8fd9\u4e2a\u51fd\u6570\u975e\u5e38\u597d\u7528      <\/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\u8fd4\u56de\u5217\u8868\u7684\u7d2f\u79ef\u6c47\u603b\u503c? \u8cc7\u6df1\u5927&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\/284724"}],"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=284724"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/284724\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=284724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=284724"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=284724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}