{"id":1170689,"date":"2025-01-15T16:25:37","date_gmt":"2025-01-15T08:25:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170689.html"},"modified":"2025-01-15T16:25:40","modified_gmt":"2025-01-15T08:25:40","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e5%88%97%e8%a1%a8%e5%85%83%e7%b4%a0%e6%b1%82%e5%92%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170689.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u5217\u8868\u5143\u7d20\u6c42\u548c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26073250\/ea36ac25-5c3b-49f1-993d-f9d3de50a12c.webp\" alt=\"python\u5982\u4f55\u5bf9\u5217\u8868\u5143\u7d20\u6c42\u548c\" \/><\/p>\n<p><p> <strong>Python\u5bf9\u5217\u8868\u5143\u7d20\u6c42\u548c\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528<code>functools.reduce()<\/code>\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u65e2\u7b80\u6d01\u53c8\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p><strong>1. \u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\uff1a<\/strong> Python \u5185\u7f6e\u51fd\u6570 <code>sum()<\/code> \u53ef\u4ee5\u76f4\u63a5\u5bf9\u5217\u8868\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u5b83\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 <code>sum()<\/code> \u51fd\u6570\u5373\u53ef\u3002<code>sum()<\/code> \u51fd\u6570\u4e0d\u4ec5\u652f\u6301\u6574\u6570\u5217\u8868\uff0c\u4e5f\u652f\u6301\u6d6e\u70b9\u6570\u5217\u8868\uff0c\u751a\u81f3\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u4ece\u800c\u66f4\u52a0\u7075\u6d3b\u5730\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = sum(numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868\u662f\u4e00\u79cd\u6700\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u7d2f\u52a0\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528<code>for<\/code>\u5faa\u73af<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = 0<\/p>\n<p>for number in numbers:<\/p>\n<p>    total += number<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0<\/li>\n<li>\u9002\u5408\u521d\u5b66\u8005\u7ec3\u4e60\u57fa\u672c\u5faa\u73af\u548c\u7d2f\u52a0\u64cd\u4f5c<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u8f83\u4e3a\u5197\u957f<\/li>\n<li>\u76f8\u5bf9\u4e8e<code>sum()<\/code>\u51fd\u6570\uff0c\u6548\u7387\u7a0d\u4f4e<\/li>\n<\/ul>\n<p><h4>1.2 \u4f7f\u7528<code>while<\/code>\u5faa\u73af<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = 0<\/p>\n<p>index = 0<\/p>\n<p>while index &lt; len(numbers):<\/p>\n<p>    total += numbers[index]<\/p>\n<p>    index += 1<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u540c\u6837\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u5408\u521d\u5b66\u8005\u7ec3\u4e60<code>while<\/code>\u5faa\u73af<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u76f8\u5bf9\u590d\u6742<\/li>\n<li>\u9700\u8981\u624b\u52a8\u7ef4\u62a4\u7d22\u5f15\uff0c\u5bb9\u6613\u51fa\u9519<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6c42\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = sum([number for number in numbers])<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01<\/li>\n<li>\u9002\u5408\u5bf9\u5217\u8868\u5143\u7d20\u8fdb\u884c\u7b80\u5355\u7684\u9884\u5904\u7406\u540e\u518d\u6c42\u548c<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u7b80\u5355\u7684\u6c42\u548c\u64cd\u4f5c\uff0c\u663e\u5f97\u6709\u4e9b\u591a\u4f59<\/li>\n<li>\u5bf9\u521d\u5b66\u8005\u7406\u89e3\u53ef\u80fd\u6709\u4e9b\u56f0\u96be<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>functools.reduce()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>functools.reduce()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u5217\u8868\u5143\u7d20\u8fdb\u884c\u8fde\u7eed\u7684\u4e8c\u5143\u64cd\u4f5c\uff0c\u8fdb\u800c\u5b9e\u73b0\u6c42\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = reduce(lambda x, y: x + y, numbers)<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u7684\u7d2f\u79ef\u64cd\u4f5c<\/li>\n<li>\u53ef\u4ee5\u7075\u6d3b\u5730\u5b9a\u4e49\u4e0d\u540c\u7684\u4e8c\u5143\u64cd\u4f5c<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u76f8\u5bf9\u590d\u6742<\/li>\n<li>\u521d\u5b66\u8005\u53ef\u80fd\u4e0d\u6613\u7406\u89e3<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\uff1a<\/strong> \u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u5408\u5404\u79cd\u6c42\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868\uff1a<\/strong> \u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0\uff0c\u9002\u5408\u521d\u5b66\u8005\u7ec3\u4e60\u57fa\u672c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff1a<\/strong> \u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u5408\u5bf9\u5217\u8868\u5143\u7d20\u8fdb\u884c\u7b80\u5355\u9884\u5904\u7406\u540e\u518d\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><p><strong>4. \u4f7f\u7528<code>functools.reduce()<\/code>\u51fd\u6570\uff1a<\/strong> \u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u7684\u7d2f\u79ef\u64cd\u4f5c\uff0c\u4f46\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u4ecb\u7ecd\u548c\u5bf9\u6bd4\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u5217\u8868\u5143\u7d20\u7684\u6c42\u548c\u64cd\u4f5c\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u63a8\u8350\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\uff0c\u56e0\u4e3a\u5b83\u65e2\u7b80\u6d01\u53c8\u9ad8\u6548\uff0c\u662f\u5904\u7406\u5217\u8868\u6c42\u548c\u7684\u9996\u9009\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u5b57\u5143\u7d20\u8fdb\u884c\u6c42\u548c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u6765\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u5b57\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868<code>numbers = [1, 2, 3, 4]<\/code>\uff0c\u53ea\u9700\u8c03\u7528<code>sum(numbers)<\/code>\u5373\u53ef\u8fd4\u56de10\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u6570\u91cf\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5143\u7d20\uff0c\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>filter()<\/code>\u51fd\u6570\u6765\u8fc7\u6ee4\u51fa\u6570\u5b57\u5143\u7d20\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sum(x for x in my_list if isinstance(x, (int, float)))<\/code>\u6765\u786e\u4fdd\u53ea\u5bf9\u6570\u5b57\u8fdb\u884c\u6c42\u548c\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u907f\u514d\u56e0\u7c7b\u578b\u9519\u8bef\u800c\u5bfc\u81f4\u7684\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u5217\u8868\u5143\u7d20\u6c42\u548c\uff1f<\/strong><br \/>\u9664\u4e86<code>sum()<\/code>\u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>reduce()<\/code>\u51fd\u6570\uff0c\u7ed3\u5408<code>lambda<\/code>\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u6c42\u548c\u529f\u80fd\u3002\u9700\u8981\u5bfc\u5165<code>functools<\/code>\u6a21\u5757\uff0c\u4f7f\u7528<code>reduce(lambda x, y: x + y, my_list)<\/code>\u6765\u5bf9\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u7d2f\u52a0\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u590d\u6742\u7684\u6c42\u548c\u903b\u8f91\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u9700\u8981\u81ea\u5b9a\u4e49\u6c42\u548c\u89c4\u5219\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bf9\u5217\u8868\u5143\u7d20\u6c42\u548c\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1170695,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170689"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1170689"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170689\/revisions"}],"predecessor-version":[{"id":1170698,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170689\/revisions\/1170698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170695"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}