{"id":984141,"date":"2024-12-27T07:25:33","date_gmt":"2024-12-26T23:25:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/984141.html"},"modified":"2024-12-27T07:25:35","modified_gmt":"2024-12-26T23:25:35","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e6%b1%82%e5%92%8c%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/984141.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210\u6c42\u548c\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211906\/0d51413b-1b04-4c4d-8772-4b951db02e72.webp\" alt=\"python\u5982\u4f55\u751f\u6210\u6c42\u548c\u51fd\u6570\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210\u6c42\u548c\u51fd\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u3001\u5229\u7528\u9012\u5f52\u65b9\u6cd5\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u662f\u6700\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u4e13\u95e8\u7528\u4e8e\u6c42\u548c\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001SUM()\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>sum()<\/code>\u662fPython\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u5217\u8868\u3001\u5143\u7ec4\u7b49\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u3002\u5176\u8bed\u6cd5\u4e3a<code>sum(iterable, start=0)<\/code>\uff0c\u5176\u4e2d<code>iterable<\/code>\u662f\u8981\u6c42\u548c\u7684\u5bf9\u8c61\uff0c<code>start<\/code>\u662f\u4e00\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u8d77\u59cb\u503c\uff0c\u9ed8\u8ba4\u4e3a0\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5176\u7b80\u6d01\u6027\u548c\u6548\u7387\u3002\u7531\u4e8e<code>sum()<\/code>\u662fPython\u5185\u7f6e\u7684\uff0c\u5b83\u7ecf\u8fc7\u4f18\u5316\uff0c\u901a\u5e38\u6bd4\u81ea\u5b9a\u4e49\u7684\u5faa\u73af\u6c42\u548c\u65b9\u6cd5\u8981\u5feb\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/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(&quot;Total sum:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6c42\u548c\u64cd\u4f5c\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u989d\u5916\u7684\u4ee3\u7801\u6765\u5b9e\u73b0\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u6bd4\u5982\u5728\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u9700\u8981\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u6269\u5c55\uff0c\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_sum(numbers):<\/p>\n<p>    total = 0<\/p>\n<p>    for number in numbers:<\/p>\n<p>        total += number<\/p>\n<p>    return total<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>print(&quot;Total sum using custom function:&quot;, custom_sum(numbers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570<code>custom_sum<\/code>\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u6dfb\u52a0\u5230<code>total<\/code>\u53d8\u91cf\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u5728\u6c42\u548c\u7684\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\uff0c\u6bd4\u5982\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u68c0\u67e5\u6216\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001LAMBDA\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>Python\u7684lambda\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u533f\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5c0f\u578b\u51fd\u6570\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6c42\u548c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7ed3\u5408<code>reduce()<\/code>\u51fd\u6570\u6765\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\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(&quot;Total sum using lambda:&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>reduce()<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u4e8c\u5143\u51fd\u6570\u548c\u4e00\u4e2a\u5e8f\u5217\uff0c\u5e76\u5c06\u8be5\u51fd\u6570\u8fde\u7eed\u5e94\u7528\u4e8e\u5e8f\u5217\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u533f\u540d\u51fd\u6570\u7684\u573a\u5408\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u66f4\u4e3a\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9012\u5f52\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u7f16\u7a0b\u6280\u672f\uff0c\u5176\u4e2d\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u3002\u867d\u7136\u9012\u5f52\u4e0d\u662f\u6c42\u548c\u7684\u6700\u4f73\u9009\u62e9\uff0c\u4f46\u53ef\u4ee5\u7528\u6765\u5c55\u793a\u9012\u5f52\u7684\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_sum(numbers):<\/p>\n<p>    if len(numbers) == 0:<\/p>\n<p>        return 0<\/p>\n<p>    else:<\/p>\n<p>        return numbers[0] + recursive_sum(numbers[1:])<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>print(&quot;Total sum using recursion:&quot;, recursive_sum(numbers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u9012\u5f52\u65b9\u6cd5\u4e2d\uff0c\u51fd\u6570<code>recursive_sum<\/code>\u8c03\u7528\u81ea\u8eab\uff0c\u76f4\u5230\u8fbe\u5230\u57fa\u51c6\u6761\u4ef6\uff08\u5217\u8868\u4e3a\u7a7a\uff09\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u5217\u8868\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u751a\u81f3\u89e6\u53d1\u9012\u5f52\u6df1\u5ea6\u9650\u5236\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u751f\u6210\u6c42\u548c\u51fd\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u662f\u6700\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u65b9\u5f0f<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u6c42\u548c\u573a\u666f\u3002\u81ea\u5b9a\u4e49\u51fd\u6570\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002lambda\u8868\u8fbe\u5f0f\u9002\u5408\u7528\u4e8e\u9700\u8981\u533f\u540d\u51fd\u6570\u7684\u573a\u5408\uff0c\u800c\u9012\u5f52\u65b9\u6cd5\u5219\u4e3b\u8981\u7528\u4e8e\u7406\u89e3\u9012\u5f52\u7684\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u666e\u901a\u7684\u6c42\u548c\u64cd\u4f5c\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u901a\u5e38\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u9700\u8981\u5728\u6c42\u548c\u8fc7\u7a0b\u4e2d\u6267\u884c\u989d\u5916\u7684\u903b\u8f91\u6216\u5904\u7406\u7279\u6b8a\u7684\u8f93\u5165\u7c7b\u578b\uff0c\u5219\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u6216\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6c42\u548c\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u6c42\u548c\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def sum_numbers(a, b):\n    return a + b\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570<code>a<\/code>\u548c<code>b<\/code>\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002\u60a8\u53ea\u9700\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u4f20\u5165\u9700\u8981\u6c42\u548c\u7684\u6570\u5b57\u5373\u53ef\u3002<\/p>\n<p><strong>Python\u652f\u6301\u6c42\u548c\u591a\u4e2a\u6570\u5b57\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u53ef\u4ee5\u4f7f\u7528\u53ef\u53d8\u53c2\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u53c2\u6570\u7684\u6c42\u548c\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def sum_multiple(*args):\n    return sum(args)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u4f20\u5165\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u51fd\u6570\u5c06\u8fd4\u56de\u5b83\u4eec\u7684\u603b\u548c\u3002\u4f8b\u5982\uff0c<code>sum_multiple(1, 2, 3, 4)<\/code>\u5c06\u8fd4\u56de10\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6c42\u548c\u65f6\u7684\u9519\u8bef\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u6c42\u548c\u51fd\u6570\u65f6\uff0c\u5904\u7406\u9519\u8bef\u60c5\u51b5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u68c0\u67e5\u8f93\u5165\u662f\u5426\u4e3a\u6570\u5b57\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6539\u8fdb\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_sum(*args):\n    if not all(isinstance(i, (int, float)) for i in args):\n        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u6240\u6709\u8f93\u5165\u5fc5\u987b\u662f\u6570\u5b57&quot;)\n    return sum(args)\n<\/code><\/pre>\n<p>\u6b64\u51fd\u6570\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\uff0c\u5982\u679c\u8f93\u5165\u7684\u53c2\u6570\u4e2d\u6709\u975e\u6570\u5b57\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5e2e\u52a9\u60a8\u907f\u514d\u6f5c\u5728\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210\u6c42\u548c\u51fd\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u3001\u5229\u7528\u9012\u5f52\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":984149,"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\/984141"}],"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=984141"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984141\/revisions"}],"predecessor-version":[{"id":984152,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984141\/revisions\/984152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/984149"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=984141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=984141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=984141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}