{"id":989459,"date":"2024-12-27T08:11:47","date_gmt":"2024-12-27T00:11:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/989459.html"},"modified":"2024-12-27T08:11:50","modified_gmt":"2024-12-27T00:11:50","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e5%88%97%e8%a1%a8%e6%b1%82%e5%92%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/989459.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u5217\u8868\u6c42\u548c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064644\/f16f78f7-c537-41c3-8ec8-5b927da476ff.webp\" alt=\"python\u5982\u4f55\u5bf9\u5217\u8868\u6c42\u548c\" \/><\/p>\n<p><p> <strong>Python\u5bf9\u5217\u8868\u6c42\u548c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548creduce\u51fd\u6570\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u662f\u6700\u5e38\u7528\u4e14\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002sum()\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u503c\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\uff0c\u8fd4\u56de\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u603b\u548c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570SUM()<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684sum()\u51fd\u6570\uff0c\u5b83\u4e13\u95e8\u7528\u4e8e\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u5176\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9sum()\u51fd\u6570\u5373\u53ef\u3002sum()\u51fd\u6570\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u9ad8\u6548\u6027\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5217\u8868\u6c42\u548c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = sum(numbers)<\/p>\n<p>print(total)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0csum()\u51fd\u6570\u63a5\u6536\u5217\u8868numbers\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5176\u4e2d\u6240\u6709\u5143\u7d20\u7684\u548c\u3002sum()\u51fd\u6570\u5185\u90e8\u5b9e\u73b0\u4e86\u9ad8\u6548\u7684\u7b97\u6cd5\uff0c\u80fd\u591f\u5feb\u901f\u8ba1\u7b97\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528FOR\u5faa\u73af\u904d\u5386\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u6216\u5728\u6c42\u548c\u8fc7\u7a0b\u4e2d\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\u8fdb\u884c\u6c42\u548c\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4ee3\u7801\u91cf\u8f83\u5927\uff0c\u4f46\u5177\u6709\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u3002<\/p>\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 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cftotal\uff0c\u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3a0\u3002\u7136\u540e\uff0c\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u7d2f\u52a0\u5230total\u4e2d\u3002\u6700\u540e\u8f93\u51fatotal\u7684\u503c\uff0c\u7ed3\u679c\u4e3a15\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u4f18\u96c5\u7684Python\u7279\u6027\uff0c\u53ef\u4ee5\u7528\u4e8e\u5bf9\u5217\u8868\u8fdb\u884c\u64cd\u4f5c\u3002\u867d\u7136\u5217\u8868\u63a8\u5bfc\u5f0f\u4e3b\u8981\u7528\u4e8e\u751f\u6210\u65b0\u5217\u8868\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\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 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u4e0e\u539f\u5217\u8868\u76f8\u540c\u7684\u65b0\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528sum()\u51fd\u6570\u5bf9\u5176\u6c42\u548c\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u76f4\u63a5\u4f7f\u7528sum()\u51fd\u6570\u7b80\u6d01\uff0c\u4f46\u5b83\u5c55\u793a\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528REDUCE\u51fd\u6570<\/p>\n<\/p>\n<p><p>reduce()\u51fd\u6570\u662ffunctools\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u7d2f\u79ef\u64cd\u4f5c\u3002\u867d\u7136reduce()\u51fd\u6570\u5728Python 3\u4e2d\u4e0d\u518d\u662f\u5185\u7f6e\u51fd\u6570\uff0c\u4f46\u901a\u8fc7\u5bfc\u5165functools\u6a21\u5757\uff0c\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b9e\u73b0\u5217\u8868\u6c42\u548c\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 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0creduce()\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u533f\u540d\u51fd\u6570lambda x, y: x + y\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u51fd\u6570\u7528\u4e8e\u5b9a\u4e49\u7d2f\u79ef\u64cd\u4f5c\uff1b\u63a5\u7740\uff0creduce()\u51fd\u6570\u4f9d\u6b21\u904d\u5386\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5c06\u5176\u7d2f\u52a0\u5230\u521d\u59cb\u503cx\u4e2d\u3002\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a15\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u5bf9\u5217\u8868\u6c42\u548c\u7684\u65b9\u5f0f\u591a\u79cd\u591a\u6837<\/strong>\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002sum()\u51fd\u6570\u662f\u6700\u4e3a\u7b80\u6d01\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\uff1bfor\u5faa\u73af\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u9002\u5408\u9700\u8981\u5728\u6c42\u548c\u8fc7\u7a0b\u4e2d\u6267\u884c\u989d\u5916\u4efb\u52a1\u7684\u573a\u5408\uff1b\u5217\u8868\u63a8\u5bfc\u5f0f\u867d\u7136\u4e0d\u5e38\u7528\u4e8e\u6c42\u548c\uff0c\u4f46\u5c55\u793a\u4e86\u5176\u5f3a\u5927\u7684\u751f\u6210\u80fd\u529b\uff1breduce()\u51fd\u6570\u5219\u662f\u53e6\u4e00\u79cd\u5b9e\u73b0\u7d2f\u79ef\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u4e60\u60ef\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5f00\u53d1\u8005\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\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\u8fdb\u884c\u6c42\u548c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u5b57\u8fdb\u884c\u6c42\u548c\u3002\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>sum()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff0c<code>sum([1, 2, 3, 4])<\/code>\u5c06\u8fd4\u56de10\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u521d\u59cb\u503c\u6765\u8fdb\u884c\u6c42\u548c\uff0c\u6bd4\u5982<code>sum([1, 2, 3], 10)<\/code>\u5c06\u8fd4\u56de13\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5143\u7d20\uff0c\u5982\u4f55\u5904\u7406\u6c42\u548c\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5143\u7d20\u65f6\uff0c\u76f4\u63a5\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u4f1a\u629b\u51faTypeError\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8fc7\u6ee4\u975e\u6570\u5b57\u5143\u7d20\uff0c\u4f8b\u5982\uff1a<code>sum(x for x in my_list if isinstance(x, (int, float)))<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u53ea\u5bf9\u6570\u5b57\u8fdb\u884c\u6c42\u548c\uff0c\u907f\u514d\u9519\u8bef\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u5217\u8868\u8fdb\u884c\u6c42\u548c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5229\u7528<code>numpy<\/code>\u5e93\u4e2d\u7684<code>numpy.sum()<\/code>\u51fd\u6570\u6765\u5bf9\u5217\u8868\u8fdb\u884c\u6c42\u548c\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>numpy<\/code>\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3anumpy\u6570\u7ec4\u5e76\u4f7f\u7528<code>numpy.sum()<\/code>\u8fdb\u884c\u6c42\u548c\uff0c\u4f8b\u5982\uff1a<code>import numpy as np; np.sum(np.array([1, 2, 3, 4]))<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u901a\u5e38\u4f1a\u66f4\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bf9\u5217\u8868\u6c42\u548c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548cr [&hellip;]","protected":false},"author":3,"featured_media":989474,"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\/989459"}],"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=989459"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989459\/revisions"}],"predecessor-version":[{"id":989479,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989459\/revisions\/989479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/989474"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=989459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=989459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=989459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}