﻿{"id":1466,"date":"2020-06-10T12:30:52","date_gmt":"2020-06-10T04:30:52","guid":{"rendered":"https:\/\/www.tansoo.cn\/?p=1466"},"modified":"2020-06-10T12:30:52","modified_gmt":"2020-06-10T04:30:52","slug":"phpbb-36","status":"publish","type":"post","link":"https:\/\/www.tansoo.cn\/?p=1466","title":{"rendered":"python\u4e2d\u201creduce\u201d\u7528\u6cd5"},"content":{"rendered":"<p><span style=\"font-size:150%;line-height:116%\"><span style=\"color:#FF0000\">Reduce<\/span><\/span><br \/>\nreduce() \u51fd\u6570\u4f1a\u5bf9\u53c2\u6570\u5e8f\u5217\u4e2d\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u3002<\/p>\n<p>\u51fd\u6570\u5c06\u4e00\u4e2a\u6570\u636e\u96c6\u5408\uff08\u94fe\u8868\uff0c\u5143\u7ec4\u7b49\uff09\u4e2d\u7684\u6240\u6709\u6570\u636e\u8fdb\u884c\u4e0b\u5217\u64cd\u4f5c\uff1a\u7528\u4f20\u7ed9 reduce \u4e2d\u7684\u51fd\u6570 function\uff08\u6709\u4e24\u4e2a\u53c2\u6570\uff09\u5148\u5bf9\u96c6\u5408\u4e2d\u7684\u7b2c 1\u30012 \u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u518d\u4e0e\u7b2c\u4e09\u4e2a\u6570\u636e\u7528 function \u51fd\u6570\u8fd0\u7b97\uff0c\u6700\u540e\u5f97\u5230\u4e00\u4e2a\u7ed3\u679c\u3002<br \/>\n\u5f53\u9700\u8981\u5bf9\u4e00\u4e2a\u5217\u8868\u8fdb\u884c\u4e00\u4e9b\u8ba1\u7b97\u5e76\u8fd4\u56de\u7ed3\u679c\u65f6\uff0cReduce \u662f\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5f53\u4f60\u9700\u8981\u8ba1\u7b97\u4e00\u4e2a\u6574\u6570\u5217\u8868\u7684\u4e58\u79ef\u65f6\u3002<\/p>\n<p>\u901a\u5e38\u5728 python \u4e2d\u4f60\u53ef\u80fd\u4f1a\u4f7f\u7528\u57fa\u672c\u7684 for \u5faa\u73af\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u6765\u8bd5\u8bd5 reduce\uff1a<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">from<\/span> functools <span style=\"color: #ff7700;font-weight:bold;\">import<\/span> <span style=\"color: #008000;\">reduce<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">product <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">reduce<\/span><span style=\"color: black;\">&#40;<\/span> <span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff7700;font-weight:bold;\">lambda<\/span> x<span style=\"color: #66cc66;\">,<\/span> y: x * y<span style=\"color: black;\">&#41;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: black;\">&#91;<\/span><span style=\"color: #ff4500;\">1<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">3<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">4<\/span><span style=\"color: black;\">&#93;<\/span> <span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># Output: 24<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #66cc66;\">&gt;&gt;&gt;<\/span><span style=\"color: #ff7700;font-weight:bold;\">def<\/span> add<span style=\"color: black;\">&#40;<\/span>x<span style=\"color: #66cc66;\">,<\/span> y<span style=\"color: black;\">&#41;<\/span> : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style=\"color: #808080; font-style: italic;\"># \u4e24\u6570\u76f8\u52a0<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&#8230; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">return<\/span> x + y<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&#8230; <\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #66cc66;\">&gt;&gt;&gt;<\/span> <span style=\"color: #008000;\">reduce<\/span><span style=\"color: black;\">&#40;<\/span>add<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: black;\">&#91;<\/span><span style=\"color: #ff4500;\">1<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff4500;\">3<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff4500;\">4<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff4500;\">5<\/span><span style=\"color: black;\">&#93;<\/span><span style=\"color: black;\">&#41;<\/span> &nbsp; <span style=\"color: #808080; font-style: italic;\"># \u8ba1\u7b97\u5217\u8868\u548c\uff1a1+2+3+4+5<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff4500;\">15<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #66cc66;\">&gt;&gt;&gt;<\/span> <span style=\"color: #008000;\">reduce<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff7700;font-weight:bold;\">lambda<\/span> x<span style=\"color: #66cc66;\">,<\/span> y: x+y<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: black;\">&#91;<\/span><span style=\"color: #ff4500;\">1<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff4500;\">3<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff4500;\">4<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff4500;\">5<\/span><span style=\"color: black;\">&#93;<\/span><span style=\"color: black;\">&#41;<\/span> &nbsp;<span style=\"color: #808080; font-style: italic;\"># \u4f7f\u7528 lambda \u533f\u540d\u51fd\u6570<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff4500;\">15<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><small>\u539f\u6587\u4f5c\u8005: BG6RSH | <a href=\"https:\/\/bbs.tansoo.cn\/viewtopic.php?t=36\" target=\"_blank\" rel=\"noopener\">\u67e5\u770b\u539f\u5e16<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reduce reduce() \u51fd\u6570\u4f1a\u5bf9\u53c2\u6570\u5e8f\u5217\u4e2d\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u3002 \u51fd\u6570\u5c06\u4e00\u4e2a\u6570\u636e &hellip; <a href=\"https:\/\/www.tansoo.cn\/?p=1466\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-1466","post","type-post","status-publish","format-standard","hentry","category-105"],"_links":{"self":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1466"}],"version-history":[{"count":0,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1466\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}