{"id":3090,"date":"2025-05-30T16:49:41","date_gmt":"2025-06-04T08:57:40","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3090"},"modified":"2025-06-04T16:49:41","modified_gmt":"2025-06-04T08:57:40","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84reduce%e5%87%bd%e6%95%b8-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84reduce%e5%87%bd%e6%95%b8-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 reduce() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, reduce()\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 reduce() \u51fd\u6578<\/h1>\n<p>Python \u7684 `reduce()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5c07\u4e00\u500b\u53ef\u8fed\u4ee3\u5c0d\u8c61\uff08\u5982 list\uff09\u4e2d\u7684\u5143\u7d20\u9032\u884c\u7d2f\u8a08\u8a08\u7b97\uff0c\u4e26\u5c07\u6700\u7d42\u7d50\u679c\u8fd4\u56de\u3002\u9019\u500b\u51fd\u6578\u901a\u5e38\u8207 `functools` \u6a21\u7d44\u4e00\u8d77\u4f7f\u7528\uff0c\u80fd\u5920\u5e6b\u52a9\u6211\u5011\u5728\u8a31\u591a\u60c5\u6cc1\u4e0b\u7c21\u5316\u4ee3\u78bc\u3002<\/p>\n<p>### \u57fa\u672c\u8a9e\u6cd5<\/p>\n<p>`reduce()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom functools import reduce<\/p>\n<p>reduce(function, sequence[, initial])<br \/>\n&#8220;`<\/p>\n<p>&#8211; **function**\uff1a\u4e00\u500b\u63a5\u53d7\u5169\u500b\u53c3\u6578\u7684\u51fd\u6578\u3002<br \/>\n&#8211; **sequence**\uff1a\u4e00\u500b\u53ef\u8fed\u4ee3\u5c0d\u8c61\uff08\u5982 list\uff09\u3002<br \/>\n&#8211; **initial**\uff08\u53ef\u9078\uff09\uff1a\u521d\u59cb\u503c\uff0c\u53ef\u4ee5\u662f\u4efb\u4f55\u985e\u578b\u7684\u8cc7\u6599\u3002<\/p>\n<p>### \u4f7f\u7528\u7bc4\u4f8b<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u5217\u8868\uff0c\u88e1\u9762\u5305\u542b\u4e86\u4e00\u4e9b\u6578\u5b57\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [1, 2, 3, 4, 5]\n&#8220;`<\/p>\n<p>#### \u8a08\u7b97\u7e3d\u548c<\/p>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `reduce()` \u51fd\u6578\u5c07\u9019\u500b\u5217\u8868\u4e2d\u7684\u5143\u7d20\u76f8\u52a0\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom functools import reduce<\/p>\n<p>total_sum = reduce(lambda x, y: x + y, my_list)<\/p>\n<p>print(total_sum)  # \u8f38\u51fa\uff1a15<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 lambda \u51fd\u6578\u4f86\u5c07\u5217\u8868\u4e2d\u7684\u5143\u7d20\u76f8\u52a0\uff0c\u6700\u7d42\u7d50\u679c\u5b58\u5132\u5728 `total_sum` \u8b8a\u6578\u4e2d\u3002<\/p>\n<p>#### \u8a08\u7b97\u4e58\u7a4d<\/p>\n<p>\u6211\u5011\u4e5f\u53ef\u4ee5\u4f7f\u7528 `reduce()` \u51fd\u6578\u5c07\u5217\u8868\u4e2d\u7684\u5143\u7d20\u76f8\u4e58\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom functools import reduce<\/p>\n<p>product = reduce(lambda x, y: x * y, my_list)<\/p>\n<p>print(product)  # \u8f38\u51fa\uff1a120<br \/>\n&#8220;`<\/p>\n<p>#### \u8907\u96dc\u8a08\u7b97<\/p>\n<p>`reduce()` \u51fd\u6578\u9084\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u66f4\u8907\u96dc\u7684\u8a08\u7b97\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom functools import reduce<\/p>\n<p>def my_func(x, y):<br \/>\n    return x + y**2<\/p>\n<p>result = reduce(my_func, my_list)<\/p>\n<p>print(result)  # \u8f38\u51fa\uff1a55<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u51fd\u6578 `my_func`\uff0c\u5b83\u63a5\u53d7\u5169\u500b\u53c3\u6578 `x` \u548c `y`\uff0c\u4e26\u8fd4\u56de `x` \u52a0\u4e0a `y` \u7684\u5e73\u65b9\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 `reduce()` \u51fd\u6578\u5c0d\u5217\u8868\u4e2d\u7684\u5143\u7d20\u9032\u884c\u8a08\u7b97\uff0c\u6700\u7d42\u7d50\u679c\u5b58\u5132\u5728 `result` \u8b8a\u6578\u4e2d\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `reduce()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **TypeError**\uff1a\u9019\u901a\u5e38\u662f\u56e0\u70ba\u63d0\u4f9b\u7684\u51fd\u6578\u4e0d\u63a5\u53d7\u6b63\u78ba\u6578\u91cf\u7684\u53c3\u6578\u3002<br \/>\n2. **ValueError**\uff1a\u5982\u679c\u5217\u8868\u662f\u7a7a\u7684\u800c\u4e14\u6c92\u6709\u63d0\u4f9b\u521d\u59cb\u503c\uff0c\u5247\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002<\/p>\n<p>\u70ba\u4e86\u907f\u514d\u9019\u4e9b\u932f\u8aa4\uff0c\u78ba\u4fdd\u63d0\u4f9b\u7684\u51fd\u6578\u6b63\u78ba\uff0c\u4e26\u8003\u616e\u5728\u4f7f\u7528\u7a7a\u5217\u8868\u6642\u63d0\u4f9b\u4e00\u500b\u5408\u9069\u7684\u521d\u59cb\u503c\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`reduce()` \u51fd\u6578\u5728\u6578\u64da\u8655\u7406\u3001\u6578\u5b78\u8a08\u7b97\u548c\u6578\u64da\u5206\u6790\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83\u4f86\u8a08\u7b97\u7d71\u8a08\u6578\u64da\u3001\u5408\u4f75\u5b57\u5178\u3001\u6216\u9032\u884c\u4efb\u4f55\u9700\u8981\u5728\u5217\u8868\u4e2d\u7d2f\u8a08\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5176\u4ed6\u9ad8\u968e\u51fd\u6578\uff0c\u5efa\u8b70\u67e5\u770b [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/645ae2f21e4b4f0013b80953) \u4ee5\u7372\u5f97\u66f4\u591a\u7684\u5be6\u7528\u6280\u5de7\u8207\u7bc4\u4f8b\u3002<\/p>\n<p>### \u7d50\u8ad6<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u7684 `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\u8a08\u7b97\u3002\u7121\u8ad6\u662f\u9032\u884c\u7c21\u55ae\u7684\u52a0\u6cd5\u6216\u4e58\u6cd5\uff0c\u9084\u662f\u66f4\u8907\u96dc\u7684\u8a08\u7b97\uff0c`reduce()` \u90fd\u80fd\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: reduce() \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8655\u7406\u54ea\u4e9b\u985e\u578b\u7684\u6578\u64da\uff1f**<br \/>\nA1: `reduce()` \u51fd\u6578\u53ef\u4ee5\u8655\u7406\u4efb\u4f55\u53ef\u8fed\u4ee3\u5c0d\u8c61\uff0c\u5982\u5217\u8868\u3001\u5143\u7d44\u548c\u96c6\u5408\u7b49\uff0c\u53ea\u8981\u63d0\u4f9b\u7684\u51fd\u6578\u80fd\u5920\u63a5\u53d7\u9019\u4e9b\u6578\u64da\u985e\u578b\u3002<\/p>\n<p>**Q2: \u4f7f\u7528 reduce() \u51fd\u6578\u6642\uff0c\u70ba\u4ec0\u9ebc\u6211\u7684\u4ee3\u78bc\u5831\u932f\uff1f**<br \/>\nA2: \u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\u50b3\u905e\u7d66 `reduce()` \u7684\u51fd\u6578\u4e0d\u63a5\u53d7\u6b63\u78ba\u6578\u91cf\u7684\u53c3\u6578\u6216\u5217\u8868\u662f\u7a7a\u7684\u4e14\u672a\u63d0\u4f9b\u521d\u59cb\u503c\u3002<\/p>\n<p>**Q3: reduce() \u51fd\u6578\u7684\u6027\u80fd\u5982\u4f55\uff1f**<br \/>\nA3: `reduce()` \u51fd\u6578\u5728\u8655\u7406\u5927\u578b\u6578\u64da\u96c6\u6642\u53ef\u80fd\u4e0d\u662f\u6700\u9ad8\u6548\u7684\u9078\u64c7\uff0c\u5c24\u5176\u662f\u7576\u9700\u8981\u591a\u6b21\u8a08\u7b97\u6642\u3002\u901a\u5e38\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528\u8ff4\u5708\u6216\u5176\u4ed6\u9ad8\u968e\u51fd\u6578\uff08\u5982 `map()` \u548c `filter()`\uff09\u53ef\u80fd\u6703\u66f4\u52a0\u9ad8\u6548\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684Reduce()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5c07\u4e00\u500b\u5217\u8868\u4e2d\u7684\u5143\u7d20\u9032\u884c\u7d2f\u52a0\u6216\u7d2f\u4e58\uff0c\u4e26\u5c07\u7d50\u679c\u8fd4\u56de\u70ba\u55ae\u500b\u503c\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Reduce()\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-3090","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-NQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3090","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=3090"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3090\/revisions"}],"predecessor-version":[{"id":3091,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3090\/revisions\/3091"}],"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=3090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}