{"id":1069973,"date":"2025-01-08T10:53:59","date_gmt":"2025-01-08T02:53:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069973.html"},"modified":"2025-01-08T10:54:02","modified_gmt":"2025-01-08T02:54:02","slug":"%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e5%88%97%e8%a1%a8%e4%b8%ad%e6%95%b0%e7%9a%84%e5%92%8cpython-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069973.html","title":{"rendered":"\u5982\u4f55\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548cpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100828\/47d833e1-3717-4503-abd8-f443bc53d0c3.webp\" alt=\"\u5982\u4f55\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548cpython\" \/><\/p>\n<p><p> <strong>\u8981\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528\u9012\u5f52\u3001\u4ee5\u53ca\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()<\/strong> \u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u53ea\u9700\u4e00\u884c\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u3002\u8be6\u7ec6\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684\u51fd\u6570 <code>sum()<\/code>\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u7528\u6765\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u548c\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868 <code>numbers<\/code>\uff0c\u4f60\u53ea\u9700\u8c03\u7528 <code>sum(numbers)<\/code> \u5c31\u80fd\u5f97\u5230\u5217\u8868\u4e2d\u6240\u6709\u6570\u7684\u548c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = sum(numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5faa\u73af\u3001\u9012\u5f52\u548c\u5217\u8868\u751f\u6210\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 sum()<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>sum()<\/code> \u662f\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\u7684\u6700\u7b80\u4fbf\u65b9\u6cd5\u3002\u8fd9\u4e2a\u51fd\u6570\u4e0d\u4ec5\u9002\u7528\u4e8e\u5217\u8868\uff0c\u8fd8\u9002\u7528\u4e8e\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5982\u5143\u7ec4\u3001\u96c6\u5408\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = sum(numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u4ee3\u7801\u7b80\u6d01\u3001\u53ef\u8bfb\u6027\u9ad8\uff0c\u5e76\u4e14\u6027\u80fd\u4f18\u8d8a\uff0c\u56e0\u4e3a <code>sum()<\/code> \u51fd\u6570\u662f\u7528 C \u8bed\u8a00\u5b9e\u73b0\u7684\uff0c\u6267\u884c\u901f\u5ea6\u8f83\u5feb\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u6765\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\u662f\u4e00\u4e2a\u8f83\u4e3a\u4f20\u7edf\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6216\u8005 <code>while<\/code> \u5faa\u73af\u6765\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5b83\u4eec\u7d2f\u52a0\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528 for \u5faa\u73af<\/h3>\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><h3>2. \u4f7f\u7528 while \u5faa\u73af<\/h3>\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 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u7684\u65b9\u6cd5\u867d\u7136\u7a0d\u663e\u7e41\u7410\uff0c\u4f46\u53ef\u4ee5\u5728\u5faa\u73af\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u903b\u8f91\uff0c\u5982\u5bf9\u67d0\u4e9b\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u8fdb\u884c\u8fc7\u6ee4\u6216\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u9012\u5f52<\/h2>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\u3002\u4f7f\u7528\u9012\u5f52\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\uff0c\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u5177\u9012\u5f52\u601d\u7ef4\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u907f\u514d\u9012\u5f52\u6df1\u5ea6\u8fc7\u5927\u5bfc\u81f4\u7684\u6808\u6ea2\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_recursive(numbers):<\/p>\n<p>    if len(numbers) == 0:<\/p>\n<p>        return 0<\/p>\n<p>    else:<\/p>\n<p>        return numbers[0] + sum_recursive(numbers[1:])<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = sum_recursive(numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u4f46\u5728\u5904\u7406\u975e\u5e38\u957f\u7684\u5217\u8868\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6027\u80fd\u95ee\u9898\u6216\u8d85\u51fa\u9012\u5f52\u6df1\u5ea6\u9650\u5236\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u662f\u4e00\u79cd\u975e\u5e38 Pythonic \u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5217\u8868\uff0c\u5e76\u5728\u751f\u6210\u8fc7\u7a0b\u4e2d\u5bf9\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002\u7ed3\u5408 <code>sum()<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = sum([number for number in numbers])<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u4ee3\u7801\u7b80\u6d01\u3001\u53ef\u8bfb\u6027\u9ad8\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u751f\u6210\u5f0f\u4e2d\u6dfb\u52a0\u6761\u4ef6\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528 numpy \u5e93<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u5efa\u8bae\u4f7f\u7528 <code>numpy<\/code> \u5e93\u3002<code>numpy<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u9ad8\u6548\u7684\u6570\u7ec4\u8fd0\u7b97\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([1, 2, 3, 4, 5])<\/p>\n<p>result = np.sum(numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>numpy<\/code> \u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u6027\u80fd\u4f18\u8d8a\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528 functools.reduce<\/h2>\n<\/p>\n<p><p><code>functools<\/code> \u6a21\u5757\u4e2d\u7684 <code>reduce<\/code> \u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\u3002<code>reduce<\/code> \u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u51fd\u6570\u4f9d\u6b21\u5e94\u7528\u5230\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u5143\u7d20\u4e0a\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>result = reduce(lambda x, y: x + y, numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>reduce<\/code> \u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7d2f\u52a0\u51fd\u6570\uff0c\u4f46\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>sum()<\/code><\/strong> \u662f\u6700\u7b80\u4fbf\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7edd\u5927\u591a\u6570\u573a\u666f\u3002\u5bf9\u4e8e\u7279\u5b9a\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5faa\u73af\u3001\u9012\u5f52\u3001\u5217\u8868\u751f\u6210\u5f0f\u3001<code>numpy<\/code> \u5e93\u6216 <code>functools.reduce<\/code> \u7b49\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u89c4\u6a21\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u5217\u8868\u7684\u603b\u548c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u4e00\u4e2a\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u603b\u548c\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\uff0c\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5217\u8868<code>numbers = [1, 2, 3, 4, 5]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>total = sum(numbers)<\/code>\u6765\u8ba1\u7b97\u603b\u548c\uff0c\u7ed3\u679c\u5c06\u662f15\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u7c7b\u578b\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u7c7b\u578b\u65f6\uff0c\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u4f1a\u5f15\u53d1<code>TypeError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u51fa\u6570\u5b57\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>total = sum(x for x in numbers if isinstance(x, (int, float)))<\/code>\u6765\u786e\u4fdd\u53ea\u8ba1\u7b97\u6574\u6570\u548c\u6d6e\u70b9\u6570\u7684\u603b\u548c\uff0c\u4ece\u800c\u83b7\u5f97\u9884\u671f\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u8ba1\u7b97\u5217\u8868\u7684\u603b\u548c\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4f7f\u7528\u5faa\u73af\u6765\u8ba1\u7b97\u603b\u548c\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u52a0\u5230\u4e00\u4e2a\u521d\u59cb\u5316\u4e3a0\u7684\u53d8\u91cf\u4e2d\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">total = 0\nfor number in numbers:\n    total += number\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u60a8\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0\u4efb\u4f55\u9700\u8981\u7684\u903b\u8f91\uff0c\u4f8b\u5982\u6761\u4ef6\u5224\u65ad\u6216\u8c03\u8bd5\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8ba1\u7b97\u5217\u8868\u4e2d\u6570\u7684\u548c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528\u9012\u5f52\u3001\u4ee5\u53ca\u4f7f\u7528\u5217\u8868 [&hellip;]","protected":false},"author":3,"featured_media":1069986,"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\/1069973"}],"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=1069973"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069973\/revisions"}],"predecessor-version":[{"id":1069990,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069973\/revisions\/1069990"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069986"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}