{"id":3092,"date":"2025-06-01T15:58:23","date_gmt":"2025-06-04T08:56:23","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3092"},"modified":"2025-06-04T15:58:23","modified_gmt":"2025-06-04T08:56:23","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84reduce_ex%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84reduce_ex%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `reduce()` \u51fd\u6578\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, reduce, reduce_ex, Python \u51fd\u6578\u6559\u5b78, Python \u5be6\u4f5c\u7bc4\u4f8b\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 `reduce()` \u51fd\u6578<\/h1>\n<p>Python \u7684 `reduce()` \u51fd\u6578\u662f Python \u4e2d\u4e00\u500b\u5f37\u5927\u7684\u5167\u5efa\u51fd\u6578\uff0c\u4f86\u81ea `functools` \u6a21\u7d44\u3002\u5b83\u53ef\u4ee5\u5c0d\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\u9032\u884c\u64cd\u4f5c\uff0c\u4e26\u5c07\u7d50\u679c\u8fd4\u56de\u7d66\u8b8a\u6578\u3002`reduce()` \u51fd\u6578\u5e38\u7528\u65bc\u6578\u64da\u7684\u7d2f\u7a4d\u8a08\u7b97\uff0c\u6bd4\u5982\u5c0d\u5217\u8868\u3001\u5b57\u5178\u548c\u5143\u7d44\u7b49\u53ef\u8fed\u4ee3\u5c0d\u8c61\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n<h2>Python `reduce()` \u51fd\u6578\u7684\u8a9e\u6cd5<\/h2>\n<p>Python `reduce()` \u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom functools import reduce<\/p>\n<p>reduce(function, iterable[, initializer])<br \/>\n&#8220;`<\/p>\n<p>\u53c3\u6578\u8aaa\u660e\uff1a<\/p>\n<ul>\n<li><strong>function<\/strong> &#8211; \u63a5\u53d7\u5169\u500b\u53c3\u6578\u7684\u51fd\u5f0f\uff0c\u7528\u65bc\u5c0d\u53ef\u8fed\u4ee3\u5c0d\u8c61\u9032\u884c\u64cd\u4f5c\u3002<\/li>\n<li><strong>iterable<\/strong> &#8211; \u53ef\u8fed\u4ee3\u5c0d\u8c61\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u3001\u5143\u7d44\u7b49\u3002<\/li>\n<li><strong>initializer<\/strong> &#8211; \u53ef\u9078\u53c3\u6578\uff0c\u7528\u65bc\u6307\u5b9a\u521d\u59cb\u503c\u3002<\/li>\n<\/ul>\n<h2>Python `reduce()` \u51fd\u6578\u7684\u7bc4\u4f8b<\/h2>\n<p>\u4e0b\u9762\u7684\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Python \u7684 `reduce()` \u51fd\u6578\u4f86\u8a08\u7b97\u5217\u8868\u4e2d\u6240\u6709\u6578\u5b57\u7684\u7e3d\u548c\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom functools import reduce<\/p>\n<p># \u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f\uff0c\u7528\u65bc\u5c0d\u53ef\u8fed\u4ee3\u5c0d\u8c61\u9032\u884c\u64cd\u4f5c<br \/>\ndef add(x, y):<br \/>\n    return x + y<\/p>\n<p># \u5b9a\u7fa9\u4e00\u500b\u5217\u8868<br \/>\nnums = [1, 2, 3, 4, 5]\n<p># \u4f7f\u7528 reduce() \u51fd\u5f0f\u5c0d\u5217\u8868\u9032\u884c\u64cd\u4f5c<br \/>\nresult = reduce(add, nums)<\/p>\n<p># \u8f38\u51fa\u7d50\u679c<br \/>\nprint(result)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4e0a\u9762\u7684\u7a0b\u5f0f\uff0c\u5f97\u5230\u4ee5\u4e0b\u7d50\u679c\uff1a<\/p>\n<p>&#8220;`python<br \/>\n15<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 Python \u7684 `reduce()` \u51fd\u6578\u5c0d\u5217\u8868\u9032\u884c\u7d2f\u52a0\u64cd\u4f5c\uff0c\u4e26\u5c07\u7d50\u679c\u8fd4\u56de\u7d66\u8b8a\u6578\u3002<\/p>\n<h2>Python `reduce()` \u51fd\u6578\u7684\u61c9\u7528<\/h2>\n<p>Python \u7684 `reduce()` \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u5c0d\u53ef\u8fed\u4ee3\u5c0d\u8c61\u9032\u884c\u5404\u7a2e\u64cd\u4f5c\uff0c\u4e26\u5c07\u7d50\u679c\u8fd4\u56de\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u61c9\u7528\u7bc4\u4f8b\uff1a<\/p>\n<p>1. **\u6c42\u548c**\uff1a\u6b63\u5982\u4e0a\u9762\u6240\u793a\u7684\u7bc4\u4f8b\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `reduce()` \u4f86\u8a08\u7b97\u4e00\u7d44\u6578\u5b57\u7684\u7e3d\u548c\u3002<br \/>\n2. **\u4e58\u7a4d**\uff1a\u53ef\u4ee5\u4fee\u6539 `add` \u51fd\u5f0f\u4f86\u8a08\u7b97\u5217\u8868\u4e2d\u6240\u6709\u6578\u5b57\u7684\u4e58\u7a4d\u3002<\/p>\n<p>&#8220;`python<br \/>\ndef multiply(x, y):<br \/>\n    return x * y<\/p>\n<p># \u4f7f\u7528 reduce() \u51fd\u5f0f\u8a08\u7b97\u4e58\u7a4d<br \/>\nproduct_result = reduce(multiply, nums)<\/p>\n<p>print(product_result)  # \u7d50\u679c\u70ba 120<br \/>\n&#8220;`<\/p>\n<p>3. **\u6700\u5927\u503c**\uff1a\u6211\u5011\u4e5f\u53ef\u4ee5\u4f7f\u7528 `reduce()` \u4f86\u627e\u5230\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u3002<\/p>\n<p>&#8220;`python<br \/>\ndef maximum(x, y):<br \/>\n    return x if x > y else y<\/p>\n<p>max_value = reduce(maximum, nums)<\/p>\n<p>print(max_value)  # \u7d50\u679c\u70ba 5<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `reduce()` \u51fd\u6578\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n&#8211; **TypeError**\uff1a\u5982\u679c\u50b3\u5165\u7684 `function` \u4e0d\u662f\u63a5\u53d7\u5169\u500b\u53c3\u6578\u7684\u51fd\u5f0f\uff0c\u5c07\u6703\u5f15\u767c\u932f\u8aa4\u3002<br \/>\n&#8211; **ValueError**\uff1a\u5982\u679c `iterable` \u70ba\u7a7a\u4e14\u672a\u63d0\u4f9b `initializer`\uff0c\u5c07\u6703\u5f15\u767c\u932f\u8aa4\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86 Python \u7684 `reduce()` \u51fd\u6578\u7684\u7528\u6cd5\u8207\u61c9\u7528\uff0c\u4e26\u63d0\u4f9b\u4e86\u5e7e\u500b\u5be6\u4f5c\u7bc4\u4f8b\u3002`reduce()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u7c21\u5316\u5c0d\u53ef\u8fed\u4ee3\u5c0d\u8c61\u7684\u64cd\u4f5c\u3002\u60f3\u4e86\u89e3\u66f4\u591a Python \u7684\u51fd\u6578\u4f7f\u7528\uff0c\u5efa\u8b70\u53c3\u8003\u9019\u7bc7 [Python \u51fd\u6578\u6559\u5b78](https:\/\/vocus.cc\/article\/60c5f9b8fd89780001e7fc64)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<ul>\n<li><strong>Q:<\/strong> `reduce()` \u548c `map()` \u7684\u5340\u5225\u662f\u4ec0\u9ebc\uff1f<br \/>\n        <br \/><strong>A:<\/strong> `reduce()` \u7528\u65bc\u5c07\u591a\u500b\u5143\u7d20\u5408\u4f75\u70ba\u4e00\u500b\u7d50\u679c\uff0c\u800c `map()` \u7528\u65bc\u5c07\u51fd\u5f0f\u61c9\u7528\u65bc\u53ef\u8fed\u4ee3\u5c0d\u8c61\u7684\u6bcf\u500b\u5143\u7d20\u3002<\/li>\n<li><strong>Q:<\/strong> \u5982\u4f55\u8655\u7406 `reduce()` \u51fd\u6578\u4e2d\u7684\u7a7a\u5217\u8868\uff1f<br \/>\n        <br \/><strong>A:<\/strong> \u7576\u4f7f\u7528\u7a7a\u5217\u8868\u6642\uff0c\u61c9\u63d0\u4f9b `initializer` \u4f86\u907f\u514d `ValueError`\u3002<\/li>\n<li><strong>Q:<\/strong> \u4f7f\u7528 `reduce()` \u6642\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u51fd\u5f0f\u55ce\uff1f<br \/>\n        <br \/><strong>A:<\/strong> \u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 `lambda` \u8868\u9054\u5f0f\u4f5c\u70ba `function`\uff0c\u4f8b\u5982\uff1a`reduce(lambda x, y: x + y, nums)`\u3002<\/li>\n<\/ul>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684reduce_ex()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5c07\u4e00\u500b\u5e8f\u5217\u7684\u5143\u7d20\u9032\u884c\u7d2f\u52a0\u6216\u7d2f\u4e58\uff0c\u4e26\u5c07\u7d50\u679c\u4f5c\u70ba\u55ae\u500b\u503c\u8fd4\u56de\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39reduce_ex()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3092","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-NS","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3092"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3092\/revisions"}],"predecessor-version":[{"id":12934,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3092\/revisions\/12934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}