{"id":1163375,"date":"2025-01-15T14:54:34","date_gmt":"2025-01-15T06:54:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1163375.html"},"modified":"2025-01-15T14:54:37","modified_gmt":"2025-01-15T06:54:37","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8sum%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1163375.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4f7f\u7528sum\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25204201\/cb292091-6a14-465d-9310-71a3519ea5f2.webp\" alt=\"python\u4e2d\u5982\u4f55\u4f7f\u7528sum\u51fd\u6570\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>sum\u51fd\u6570\u7528\u4e8e\u5bf9\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\uff09\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c<\/strong>\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1a<code>sum(iterable, start=0)<\/code>\uff0c\u5176\u4e2d<code>iterable<\/code>\u662f\u9700\u8981\u6c42\u548c\u7684\u5e8f\u5217\uff0c<code>start<\/code>\u662f\u53ef\u9009\u7684\u53c2\u6570\uff0c\u8868\u793a\u6c42\u548c\u7684\u8d77\u59cb\u503c\uff0c\u9ed8\u8ba4\u4e3a0\u3002<strong>\u901a\u8fc7\u4f7f\u7528sum\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5e8f\u5217\u4e2d\u7684\u6570\u503c\u8fdb\u884c\u7d2f\u52a0\uff0c\u4ece\u800c\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u6548\u7387<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u7840\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>sum<\/code>\u51fd\u6570\u975e\u5e38\u7b80\u6d01\u4e14\u529f\u80fd\u5f3a\u5927\uff0c\u5b83\u80fd\u591f\u5bf9\u4e00\u4e2a\u5217\u8868\u3001\u5143\u7ec4\u7b49\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6240\u6709\u6570\u503c\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u7528\u6cd5\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>\u5217\u8868\u6c42\u548c<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c<code>sum<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u503c\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b5\u4e2a\u6574\u6570\u7684\u5217\u8868<code>numbers<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u6c42\u548c\uff0c\u7ed3\u679c\u4e3a15\u3002<\/p>\n<\/p>\n<p><h4>\u5143\u7ec4\u6c42\u548c<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u5217\u8868\uff0c\u5143\u7ec4\u4e5f\u662fPython\u4e2d\u7684\u5e38\u7528\u6570\u636e\u7ed3\u6784\u3002<code>sum<\/code>\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u5bf9\u5143\u7ec4\u4e2d\u7684\u6570\u503c\u5143\u7d20\u8fdb\u884c\u6c42\u548c\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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b5\u4e2a\u6574\u6570\u7684\u5143\u7ec4<code>numbers<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u6c42\u548c\uff0c\u7ed3\u679c\u540c\u6837\u4e3a15\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528start\u53c2\u6570<\/h3>\n<\/p>\n<p><p><code>sum<\/code>\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>start<\/code>\u662f\u53ef\u9009\u7684\uff0c\u5b83\u8868\u793a\u6c42\u548c\u7684\u8d77\u59cb\u503c\uff0c\u9ed8\u8ba4\u4e3a0\u3002\u5f53\u9700\u8981\u4ece\u4e00\u4e2a\u975e\u96f6\u503c\u5f00\u59cb\u7d2f\u52a0\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = sum(numbers, 10)<\/p>\n<p>print(result)  # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u5bf9\u5217\u8868<code>numbers<\/code>\u4e2d\u7684\u6570\u503c\u8fdb\u884c\u6c42\u548c\uff0c\u5e76\u4ece10\u5f00\u59cb\u7d2f\u52a0\uff0c\u6700\u7ec8\u7ed3\u679c\u4e3a25\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p><code>sum<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u6574\u6570\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u6d6e\u70b9\u6570\u3002\u5728\u8fdb\u884c\u6d6e\u70b9\u6570\u6c42\u548c\u65f6\uff0c<code>sum<\/code>\u51fd\u6570\u540c\u6837\u80fd\u591f\u6709\u6548\u5730\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1.1, 2.2, 3.3, 4.4, 5.5]<\/p>\n<p>result = sum(numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa: 16.5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b5\u4e2a\u6d6e\u70b9\u6570\u7684\u5217\u8868<code>numbers<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u6c42\u548c\uff0c\u7ed3\u679c\u4e3a16.5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5bf9\u590d\u6742\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6570\u503c\u8fdb\u884c\u6c42\u548c\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b50\u5217\u8868\u7684\u5217\u8868\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u751f\u6210\u5f0f\u548c<code>sum<\/code>\u51fd\u6570\u6765\u7b80\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>result = sum([sum(sublist) for sublist in numbers])<\/p>\n<p>print(result)  # \u8f93\u51fa: 45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b50\u5217\u8868\u7684\u5217\u8868<code>numbers<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u5bf9\u6bcf\u4e2a\u5b50\u5217\u8868\u8fdb\u884c\u6c42\u548c\uff0c\u518d\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u5bf9\u6240\u6709\u5b50\u5217\u8868\u7684\u548c\u8fdb\u884c\u7d2f\u52a0\uff0c\u6700\u7ec8\u7ed3\u679c\u4e3a45\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>sum<\/code>\u51fd\u6570\u53ef\u4ee5\u4e0e\u5176\u4ed6Python\u5185\u7f6e\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u591a\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u7ed3\u5408<code>map<\/code>\u51fd\u6570\u5bf9\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u53d8\u6362\u540e\u518d\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squared_sum = sum(map(lambda x: x2, numbers))<\/p>\n<p>print(squared_sum)  # \u8f93\u51fa: 55<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u5bf9\u5217\u8868<code>numbers<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u7136\u540e\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u5bf9\u5e73\u65b9\u540e\u7684\u6570\u503c\u8fdb\u884c\u6c42\u548c\uff0c\u6700\u7ec8\u7ed3\u679c\u4e3a55\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u9519\u8bef\u5904\u7406\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u548c\u95ee\u9898\u3002\u4f8b\u5982\uff0c<code>sum<\/code>\u51fd\u6570\u53ea\u80fd\u5bf9\u6570\u503c\u7c7b\u578b\u7684\u5143\u7d20\u8fdb\u884c\u6c42\u548c\uff0c\u5982\u679c\u5e8f\u5217\u4e2d\u5305\u542b\u975e\u6570\u503c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u5c06\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, &#39;2&#39;, 3, 4, 5]<\/p>\n<p>try:<\/p>\n<p>    result = sum(numbers)<\/p>\n<p>except TypeError as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e\u5217\u8868<code>numbers<\/code>\u4e2d\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u4e32\u5143\u7d20<code>&#39;2&#39;<\/code>\uff0c<code>sum<\/code>\u51fd\u6570\u5728\u8fdb\u884c\u6c42\u548c\u65f6\u4f1a\u5f15\u53d1<code>TypeError<\/code>\u3002\u4e3a\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u6c42\u548c\u4e4b\u524d\u5bf9\u5e8f\u5217\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u95ee\u9898\u53ef\u80fd\u4f1a\u53d8\u5f97\u66f4\u52a0\u660e\u663e\u3002\u4e3a\u4e86\u63d0\u9ad8<code>sum<\/code>\u51fd\u6570\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u76f8\u6bd4\u5217\u8868\u751f\u6210\u5f0f\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u5143\u7d20\uff0c\u800c\u662f\u6309\u9700\u751f\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = sum(x2 for x in numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa: 55<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5bf9\u5217\u8868<code>numbers<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u7136\u540e\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u5bf9\u5e73\u65b9\u540e\u7684\u6570\u503c\u8fdb\u884c\u6c42\u548c\uff0c\u6700\u7ec8\u7ed3\u679c\u4e3a55\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Numpy\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u65f6\uff0cNumpy\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u6c42\u548c\u51fd\u6570<code>numpy.sum<\/code>\uff0c\u5b83\u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u548c\u66f4\u591a\u7684\u529f\u80fd\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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Numpy\u5e93\u4e2d\u7684<code>numpy.sum<\/code>\u51fd\u6570\u5bf9\u6570\u7ec4<code>numbers<\/code>\u8fdb\u884c\u6c42\u548c\uff0c\u7ed3\u679c\u4e3a15\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790\u4e0e\u7edf\u8ba1<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e0e\u7edf\u8ba1\u4e2d\uff0c\u6c42\u548c\u64cd\u4f5c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u4e00\u7ec4\u6570\u636e\u7684\u603b\u548c\u3001\u5e73\u5747\u503c\u3001\u6807\u51c6\u5dee\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statistics<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>total = sum(data)<\/p>\n<p>mean = statistics.mean(data)<\/p>\n<p>stdev = statistics.stdev(data)<\/p>\n<p>print(f&quot;Total: {total}, Mean: {mean}, Standard Deviation: {stdev}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u8ba1\u7b97\u6570\u636e<code>data<\/code>\u7684\u603b\u548c\uff0c\u4f7f\u7528<code>statistics.mean<\/code>\u51fd\u6570\u8ba1\u7b97\u5e73\u5747\u503c\uff0c\u4f7f\u7528<code>statistics.stdev<\/code>\u51fd\u6570\u8ba1\u7b97\u6807\u51c6\u5dee\u3002<\/p>\n<\/p>\n<p><h4>\u8d22\u52a1\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u8d22\u52a1\u8ba1\u7b97\u4e2d\uff0c\u6c42\u548c\u64cd\u4f5c\u4e5f\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u4e00\u6bb5\u65f6\u95f4\u5185\u7684\u603b\u6536\u5165\u3001\u603b\u652f\u51fa\u3001\u51c0\u5229\u6da6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">incomes = [5000, 6000, 7000, 8000, 9000]<\/p>\n<p>expenses = [2000, 2500, 3000, 3500, 4000]<\/p>\n<p>total_income = sum(incomes)<\/p>\n<p>total_expense = sum(expenses)<\/p>\n<p>net_profit = total_income - total_expense<\/p>\n<p>print(f&quot;Total Income: {total_income}, Total Expense: {total_expense}, Net Profit: {net_profit}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u8ba1\u7b97\u6536\u5165<code>incomes<\/code>\u548c\u652f\u51fa<code>expenses<\/code>\u7684\u603b\u548c\uff0c\u7136\u540e\u8ba1\u7b97\u51c0\u5229\u6da6\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>sum<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u4e14\u9ad8\u6548\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u5e8f\u5217\u4e2d\u7684\u6570\u503c\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002<strong>\u901a\u8fc7\u638c\u63e1<code>sum<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u3001start\u53c2\u6570\u3001\u5904\u7406\u6d6e\u70b9\u6570\u3001\u590d\u6742\u6570\u636e\u7ed3\u6784\u3001\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3001\u9519\u8bef\u5904\u7406\u4e0e\u6ce8\u610f\u4e8b\u9879\u3001\u6027\u80fd\u4f18\u5316\u4ee5\u53ca\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u7075\u6d3b\u8fd0\u7528<code>sum<\/code>\u51fd\u6570\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387<\/strong>\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u7684<code>sum<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0csum\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>sum\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u6240\u6709\u5143\u7d20\u7684\u548c\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>sum(iterable, start=0)<\/code>\uff0c\u5176\u4e2d<code>iterable<\/code>\u662f\u4f60\u60f3\u8981\u8ba1\u7b97\u548c\u7684\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\uff0c\u800c<code>start<\/code>\u53c2\u6570\u5219\u662f\u52a0\u6cd5\u7684\u8d77\u59cb\u503c\uff0c\u9ed8\u8ba4\u662f0\u3002\u4f8b\u5982\uff0c<code>sum([1, 2, 3])<\/code>\u5c06\u8fd4\u56de6\uff0c<code>sum([1, 2, 3], 10)<\/code>\u5c06\u8fd4\u56de16\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528sum\u51fd\u6570\u5bf9\u590d\u6742\u6570\u636e\u7ed3\u6784\u6c42\u548c\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5d4c\u5957\u6570\u636e\u7ed3\u6784\uff0c\u6bd4\u5982\u5217\u8868\u4e2d\u7684\u5b57\u5178\uff0csum\u51fd\u6570\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u4e00\u8d77\u4f7f\u7528\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u53d6\u7279\u5b9a\u7684\u503c\uff0c\u7136\u540e\u518d\u8fdb\u884c\u6c42\u548c\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2a\u5217\u8868\u5305\u542b\u5b57\u5178\uff0c\u53ef\u4ee5\u8fd9\u6837\u8ba1\u7b97\u67d0\u4e2a\u952e\u7684\u603b\u548c\uff1a<code>sum(d[&#39;key&#39;] for d in list_of_dicts)<\/code>\uff0c\u8fd9\u5c06\u5bf9\u6240\u6709\u5b57\u5178\u4e2d\u6307\u5b9a\u952e\u7684\u503c\u8fdb\u884c\u6c42\u548c\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528sum\u51fd\u6570\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u4f7f\u7528sum\u51fd\u6570\u65f6\uff0c\u5e38\u89c1\u9519\u8bef\u5305\u62ec\u5bf9\u975e\u6570\u5b57\u7c7b\u578b\u8fdb\u884c\u6c42\u548c\uff0c\u6bd4\u5982\u5bf9\u5b57\u7b26\u4e32\u6216None\u503c\u8fdb\u884c\u64cd\u4f5c\u3002\u786e\u4fdd\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u662f\u6570\u503c\u7c7b\u578b\u3002\u6b64\u5916\uff0cstart\u53c2\u6570\u7684\u4f7f\u7528\u4e5f\u9700\u6ce8\u610f\uff0c\u786e\u4fdd\u5b83\u7684\u6570\u636e\u7c7b\u578b\u4e0eiterable\u4e2d\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\u4e00\u81f4\uff0c\u907f\u514d\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0csum\u51fd\u6570\u7528\u4e8e\u5bf9\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\uff09\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1asum [&hellip;]","protected":false},"author":3,"featured_media":1163380,"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\/1163375"}],"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=1163375"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163375\/revisions"}],"predecessor-version":[{"id":1163381,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163375\/revisions\/1163381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1163380"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1163375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1163375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1163375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}