{"id":2808,"date":"2025-05-30T17:17:26","date_gmt":"2025-06-04T09:42:25","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2808"},"modified":"2025-06-04T17:17:26","modified_gmt":"2025-06-04T09:42:25","slug":"python%e4%b8%ad%e7%9a%84sum%e5%87%bd%e6%95%b8%ef%bc%9a%e8%a8%88%e7%ae%97%e5%ba%8f%e5%88%97%e4%b8%ad%e5%85%83%e7%b4%a0%e7%9a%84%e7%b8%bd%e5%92%8c","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84sum%e5%87%bd%e6%95%b8%ef%bc%9a%e8%a8%88%e7%ae%97%e5%ba%8f%e5%88%97%e4%b8%ad%e5%85%83%e7%b4%a0%e7%9a%84%e7%b8%bd%e5%92%8c\/","title":{"rendered":"2025 \u6700\u65b0 Python sum() \u51fd\u6578\u6559\u5b78\uff1a\u5feb\u901f\u8a08\u7b97\u5e8f\u5217\u7e3d\u548c\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, sum(), Python \u6559\u5b78, Python \u51fd\u6578\"><\/p>\n<p>\u5728 Python \u4e2d\uff0c`sum()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u7528\u65bc\u8a08\u7b97\u5e8f\u5217\u4e2d\u6240\u6709\u5143\u7d20\u7684\u7e3d\u548c\u3002\u4e0d\u8ad6\u662f\u8655\u7406\u5217\u8868\u3001\u5143\u7d44\u9084\u662f\u5b57\u5178\uff0c`sum()` \u51fd\u6578\u90fd\u80fd\u5920\u8f15\u9b06\u5b8c\u6210\u9019\u4e00\u4efb\u52d9\u3002\u672c\u6587\u5c07\u4ecb\u7d39 `sum()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u3001\u793a\u4f8b\u4ee5\u53ca\u4e00\u4e9b\u5be6\u7528\u6280\u5de7\uff0c\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1\u9019\u500b\u51fd\u6578\u3002<\/p>\n<h2>sum() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`sum()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nsum(iterable[, start])<br \/>\n&#8220;`<\/p>\n<p>&#8211; **iterable**\uff1a\u4e00\u500b\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\uff0c\u5982\u5217\u8868\u3001\u5143\u7d44\u3001\u5b57\u5178\u7b49\u3002<br \/>\n&#8211; **start**\uff1a\u53ef\u9078\u53c3\u6578\uff0c\u6307\u5b9a\u8a08\u7b97\u7684\u8d77\u59cb\u503c\uff0c\u9810\u8a2d\u70ba 0\u3002<\/p>\n<h2>sum() \u51fd\u6578\u7684\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5e7e\u500b\u4f7f\u7528 `sum()` \u51fd\u6578\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u8a08\u7b97\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7e3d\u548c<br \/>\nlist_a = [1, 2, 3, 4, 5]\nsum_a = sum(list_a)<br \/>\nprint(sum_a)  # \u8f38\u51fa\u7d50\u679c\u70ba15<\/p>\n<p># \u8a08\u7b97\u5143\u7d44\u4e2d\u5143\u7d20\u7684\u7e3d\u548c<br \/>\ntuple_b = (1, 2, 3, 4, 5)<br \/>\nsum_b = sum(tuple_b)<br \/>\nprint(sum_b)  # \u8f38\u51fa\u7d50\u679c\u70ba15<\/p>\n<p># \u8a08\u7b97\u5b57\u5178\u4e2d\u5143\u7d20\u7684\u7e3d\u548c<br \/>\ndict_c = {&#8216;a&#8217;: 1, &#8216;b&#8217;: 2, &#8216;c&#8217;: 3, &#8216;d&#8217;: 4, &#8216;e&#8217;: 5}<br \/>\nsum_c = sum(dict_c.values())<br \/>\nprint(sum_c)  # \u8f38\u51fa\u7d50\u679c\u70ba15<\/p>\n<p># \u8a08\u7b97\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7e3d\u548c\uff0c\u4e26\u6307\u5b9a\u8d77\u59cb\u503c\u70ba10<br \/>\nlist_d = [1, 2, 3, 4, 5]\nsum_d = sum(list_d, 10)<br \/>\nprint(sum_d)  # \u8f38\u51fa\u7d50\u679c\u70ba25<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u4e9b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `sum()` \u51fd\u6578\u8a08\u7b97\u4e0d\u540c\u985e\u578b\u5e8f\u5217\u7684\u7e3d\u548c\u3002\u9019\u4e9b\u7bc4\u4f8b\u4e0d\u50c5\u7c21\u55ae\u660e\u77ad\uff0c\u9084\u80fd\u5e6b\u52a9\u4f60\u638c\u63e1\u51fd\u6578\u7684\u4f7f\u7528\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `sum()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **TypeError**\uff1a\u5982\u679c `iterable` \u4e2d\u7684\u5143\u7d20\u4e0d\u662f\u6578\u5b57\uff0c\u5247\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u4f60\u7684\u5e8f\u5217\u4e2d\u53ea\u5305\u542b\u6578\u5b57\u985e\u578b\u3002<br \/>\n&#8211; **ValueError**\uff1a\u5982\u679c `start` \u53c3\u6578\u7684\u985e\u578b\u8207 `iterable` \u4e2d\u7684\u5143\u7d20\u985e\u578b\u4e0d\u4e00\u81f4\uff0c\u5c07\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u52a0\u7e3d\u529f\u80fd\u5916\uff0c`sum()` \u51fd\u6578\u9084\u53ef\u4ee5\u8207\u5176\u4ed6 Python \u7279\u6027\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\u5217\u8868\u63a8\u5c0e\u5f0f\u548c\u51fd\u6578\u5f0f\u7de8\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u9032\u968e\u793a\u4f8b\uff0c\u4f7f\u7528 `sum()` \u51fd\u6578\u8a08\u7b97\u7bc4\u570d\u5167\u5076\u6578\u7684\u7e3d\u548c\uff1a<\/p>\n<p>&#8220;`python<br \/>\neven_sum = sum(x for x in range(1, 101) if x % 2 == 0)<br \/>\nprint(even_sum)  # \u8f38\u51fa\u7d50\u679c\u70ba2550<br \/>\n&#8220;`<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`sum()` \u51fd\u6578\u5728 Python \u4e2d\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u4e14\u7c21\u55ae\u6613\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5feb\u901f\u8a08\u7b97\u5e8f\u5217\u4e2d\u5143\u7d20\u7684\u7e3d\u548c\u3002\u7121\u8ad6\u662f\u57fa\u672c\u7528\u6cd5\u9084\u662f\u9032\u968e\u61c9\u7528\uff0c\u638c\u63e1 `sum()` \u51fd\u6578\u90fd\u80fd\u5e6b\u52a9\u4f60\u5728\u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u63d0\u5347\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u5176\u4ed6\u51fd\u6578\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc) \u7372\u53d6\u66f4\u591a\u6559\u5b78\u8cc7\u6e90\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u6211\u53ef\u4ee5\u4f7f\u7528 `sum()` \u51fd\u6578\u4f86\u8a08\u7b97\u5b57\u7b26\u4e32\u7684\u7e3d\u548c\u55ce\uff1f**<br \/>\nA1: \u4e0d\u53ef\u4ee5\uff0c`sum()` \u51fd\u6578\u53ea\u9069\u7528\u65bc\u6578\u5b57\u985e\u578b\uff0c\u5c0d\u65bc\u5b57\u7b26\u4e32\u5c07\u6703\u5f15\u767c `TypeError`\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8a08\u7b97\u591a\u7dad\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u7e3d\u548c\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5c0e\u5f0f\u5c07\u591a\u7dad\u5217\u8868\u5c55\u5e73\uff0c\u7136\u5f8c\u518d\u4f7f\u7528 `sum()` \u51fd\u6578\u3002\u4f8b\u5982\uff1a`sum(x for sublist in multi_dimensional_list for x in sublist)`\u3002<\/p>\n<p>**Q3: `start` \u53c3\u6578\u7684\u7528\u9014\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA3: `start` \u53c3\u6578\u5141\u8a31\u4f60\u6307\u5b9a\u4e00\u500b\u8d77\u59cb\u503c\uff0c\u9019\u500b\u503c\u6703\u52a0\u5230\u8a08\u7b97\u7684\u7e3d\u548c\u4e2d\uff0c\u9019\u5728\u9700\u8981\u589e\u52a0\u521d\u59cb\u503c\u7684\u60c5\u6cc1\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684sum()\u51fd\u6578\u53ef\u4ee5\u8a08\u7b97\u5e8f\u5217\u4e2d\u5143\u7d20\u7684\u7e3d\u548c\uff0c\u53ef\u4ee5\u7528\u65bc\u8a08\u7b97\u6578\u5b57\u5e8f\u5217\u3001\u5b57\u7b26\u4e32\u5e8f\u5217\u6216\u5176\u4ed6\u5e8f\u5217\u7684\u7e3d\u548c\u3002\u672c\u6587\u5c07\u4ecb\u7d39sum()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u8a08\u7b97\u5e8f\u5217\u4e2d\u5143\u7d20\u7684\u7e3d\u548c\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-2808","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-Ji","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2808","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=2808"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2808\/revisions"}],"predecessor-version":[{"id":2809,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2808\/revisions\/2809"}],"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=2808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}