{"id":567,"date":"2019-02-14T18:32:02","date_gmt":"2019-02-14T10:32:02","guid":{"rendered":"https:\/\/www.sunjw.cn\/?p=567"},"modified":"2019-05-10T18:17:03","modified_gmt":"2019-05-10T10:17:03","slug":"pythonmap-filter-reduce","status":"publish","type":"post","link":"https:\/\/www.sunjw.cn\/index.php\/2019\/02\/14\/pythonmap-filter-reduce\/","title":{"rendered":"[PYTHON]map filter reduce \u4f7f\u7528\u7b80\u4ecb"},"content":{"rendered":"<p>Map,\u00a0filter,reduce\u00a0\u90fd\u662f\u9488\u5bf9\u53ef\u8fed\u4ee3\u7c7b\u578b(iterable)\u7684\u5faa\u73af\u64cd\u4f5c\uff0c\u8fd4\u56de\u60f3\u8981\u7684\u7ed3\u679c\u3002\u4e09\u8005\u4f7f\u7528\u4e0a\u6709\u76f8\u4f3c\u6027\uff0c\u529f\u80fd\u4e0d\u592a\u76f8\u540c\u3002<\/p>\n<p>\u4e09\u4e2a\u51fd\u6570\u90fd\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u51fd\u6570\u540d\uff0c\u662f\u6307\u8981\u9488\u5bf9\u6bcf\u4e2a\u5143\u7d20\u505a\u7684\u64cd\u4f5c\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u53ef\u8fed\u4ee3\u7684\u5e8f\u5217\u3002<\/p>\n<p>\u5148\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u4e00\u4e2alist\u00a0mylist=[1,2,3,4,5]\uff0c\u6211\u4eec\u8981\u628a\u6bcf\u4e2a\u5143\u7d20\u5206\u522b\u4e582\u8f93\u51fa\uff0c\u6b63\u5e38\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><em>mylist=[1,2,3,4,5]<\/em><br \/>\n<em>result=[]<\/em><br \/>\n<em>i=0<\/em><br \/>\n<em>while\u00a0i\u00a0&lt;\u00a0len(mylist):<\/em><br \/>\n<em>\u00a0\u00a0\u00a0\u00a0result.append(mylist[i]*2)<\/em><br \/>\n<em>\u00a0\u00a0\u00a0\u00a0i+=1<\/em><br \/>\n<em>print(result)<\/em><\/p>\n<p>\u6211\u4eec\u4f7f\u7528\u5faa\u73af\u5b8c\u6210\u4e86\u4e0a\u8ff0\u529f\u80fd\uff0c\u4f46\u662f\u4e3a\u4e86\u4f7f\u4ee3\u7801\u7b80\u6d01\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528map\uff0c\u800c\u4e0d\u662f\u7528\u5faa\u73af\u3002\u6211\u4eec\u53ea\u9700\u4f7f\u75283\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b8c\u6210\u4e0a\u8ff0\u529f\u80fd\u3002<\/p>\n<p><em>mylist=[1,2,3,4,5]<\/em><br \/>\n<em>out=list(map(lambda\u00a0x:x*2,\u00a0mylist))<\/em><br \/>\n<em>print(out)<\/em><\/p>\n<p>\u6240\u4ee5\uff0cmap\u5b8c\u6210\u7684\u662f\u5c06mylist\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5206\u522b\u5e26\u5165lambda\u51fd\u6570\u4e2d\u8fdb\u884c\u8ba1\u7b97\uff0c\u6700\u7ec8\u4ecd\u7136\u8fd4\u56delist\u7c7b\u578b\u7684\u7ed3\u679c\uff0c\u5373<strong>[2,\u00a04,\u00a06,\u00a08,\u00a010]<\/strong>\u3002<\/p>\n<p>\u7c7b\u4f3c\uff0cfilter\u4e5f\u662f\u4f9d\u6b21\u5c06\u6bcf\u4e2a\u5143\u7d20\u5e26\u5165\u8fdb\u884c\u8ba1\u7b97\uff0c\u53ea\u4e0d\u8fc7filter\u8fd4\u56de\u7684\u662f\u8fd0\u884c\u7ed3\u679c\u4e3a\u771f\u7684\u5143\u7d20\u3002\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u6253\u5370\u51falist\u4e2d\u5c0f\u4e8e4\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u3002\u6700\u7ec8\u53ea\u6253\u5370\u51fa\u4e861,2,3<\/p>\n<p><em>mylist=[1,2,3,4,5]<\/em><br \/>\n<em>out=list(filter(lambda\u00a0x:\u00a0x&lt;4,\u00a0mylist))<\/em><br \/>\n<em>print(out)<\/em><\/p>\n<p><strong>[1,\u00a02,\u00a03]<\/strong><\/p>\n<p>Reduce\u4e5f\u662f\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7reduce\u8fd4\u56de\u7684\u4e0d\u662f\u5143\u7d20\uff0c\u800c\u662f\u7d2f\u8ba1\u8ba1\u7b97\u5404\u4e2a\u5143\u7d20\u7684\u8fd0\u884c\u7ed3\u679c\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u8981\u8ba1\u7b97list\u4e2d\u5404\u4e2a\u5143\u7d20\u7684\u548c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a\u6700\u7ec8\u6253\u5370<strong>15<\/strong><\/p>\n<p><em>mylist=[1,2,3,4,5]<\/em><br \/>\n<em>out=reduce(lambda\u00a0x,y\u00a0:\u00a0x+y,\u00a0mylist)<\/em><br \/>\n<em>print(out)<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Map,\u00a0filter,reduce\u00a0\u90fd\u662f\u9488\u5bf9\u53ef\u8fed\u4ee3\u7c7b\u578b(iterable)\u7684\u5faa\u73af\u64cd\u4f5c\uff0c\u8fd4\u56de\u60f3\u8981\u7684\u7ed3\u679c\u3002\u4e09\u8005\u4f7f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts\/567"}],"collection":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/comments?post=567"}],"version-history":[{"count":1,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts\/567\/revisions"}],"predecessor-version":[{"id":568,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts\/567\/revisions\/568"}],"wp:attachment":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/media?parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/categories?post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/tags?post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}