{"id":969191,"date":"2024-12-27T05:14:25","date_gmt":"2024-12-26T21:14:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/969191.html"},"modified":"2024-12-27T05:14:27","modified_gmt":"2024-12-26T21:14:27","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8sum","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/969191.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4f7f\u7528sum"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183613\/f9c140f4-a4cd-491e-ad98-60f670571009.webp\" alt=\"python\u4e2d\u5982\u4f55\u4f7f\u7528sum\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u5143\u7d20\u7684\u603b\u548c<\/strong>\u3002<code>sum<\/code>\u51fd\u6570\u662fPython\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u5bf9\u5217\u8868\u3001\u5143\u7ec4\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6570\u503c\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002<strong>\u5176\u57fa\u672c\u7528\u6cd5\u662f<code>sum(iterable, start=0)<\/code><\/strong>\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\u6307\u5b9a\u6c42\u548c\u7684\u8d77\u59cb\u503c\uff0c\u9ed8\u8ba4\u4e3a0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u7684\u5e38\u89c1\u573a\u666f\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SUM \u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u4e2d<code>sum<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002\u4f60\u53ea\u9700\u8981\u5c06\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f20\u9012\u7ed9<code>sum<\/code>\u51fd\u6570\uff0c\u5b83\u5c31\u4f1a\u8fd4\u56de\u8be5\u5bf9\u8c61\u4e2d\u6240\u6709\u5143\u7d20\u7684\u548c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\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(total)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>sum<\/code>\u51fd\u6570\u5bf9\u5217\u8868<code>numbers<\/code>\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6c42\u548c\uff0c\u8fd4\u56de\u503c\u4e3a15\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 START \u53c2\u6570<\/h3>\n<\/p>\n<p><p><code>sum<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u9009\u7684<code>start<\/code>\u53c2\u6570\uff0c\u7528\u4e8e\u6307\u5b9a\u6c42\u548c\u7684\u8d77\u59cb\u503c\u3002\u8fd9\u4e2a\u53c2\u6570\u5728\u4f60\u9700\u8981\u5c06\u67d0\u4e2a\u56fa\u5b9a\u503c\u52a0\u5230\u603b\u548c\u4e2d\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = sum(numbers, 10)<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>sum<\/code>\u51fd\u6570\u4e0d\u4ec5\u5bf9\u5217\u8868<code>numbers<\/code>\u4e2d\u7684\u5143\u7d20\u6c42\u548c\uff0c\u8fd8\u52a0\u4e0a\u4e86<code>start<\/code>\u53c2\u6570\u7684\u503c10\u3002\u56e0\u6b64\uff0c\u603b\u548c\u4e3a25\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>sum<\/code>\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5305\u62ec\u5143\u7ec4\u3001\u5217\u8868\u751f\u6210\u5668\u7b49\u3002\u8fd9\u4f7f\u5f97\u5b83\u5728\u5904\u7406\u6570\u636e\u65f6\u975e\u5e38\u7075\u6d3b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = sum(x * x for x in range(1, 6))<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a55<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>sum<\/code>\u51fd\u6570\u5bf9\u4ece1\u52305\u7684\u6240\u6709\u6574\u6570\u7684\u5e73\u65b9\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>sum<\/code>\u51fd\u6570\u53ef\u4ee5\u4e0ePython\u4e2d\u7684\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u6765\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u67d0\u79cd\u64cd\u4f5c\uff0c\u7136\u540e\u5bf9\u7ed3\u679c\u8fdb\u884c\u6c42\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = sum(map(lambda x: x * 2, numbers))<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>map<\/code>\u51fd\u6570\u5c06\u5217\u8868<code>numbers<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4e58\u4ee52\uff0c\u7136\u540e<code>sum<\/code>\u51fd\u6570\u5bf9\u7ed3\u679c\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u591a\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u7ef4\u6570\u7ec4\uff08\u5982\u5217\u8868\u7684\u5217\u8868\uff09\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5bf9\u5185\u90e8\u5217\u8868\u8fdb\u884c\u6c42\u548c\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>total = sum(sum(row) for row in matrix)<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>sum<\/code>\u51fd\u6570\u9996\u5148\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u6c42\u548c\uff0c\u7136\u540e\u5bf9\u6240\u6709\u884c\u7684\u603b\u548c\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>sum<\/code>\u51fd\u6570\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8fd0\u884c\u5f97\u975e\u5e38\u5feb\uff0c\u4f46\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\u3002<code>sum<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u9010\u4e00\u7d2f\u52a0\u7684\u8fc7\u7a0b\uff0c\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u5e93\u4e2d\u7684<code>sum<\/code>\u51fd\u6570\uff0c\u8be5\u5e93\u4e13\u4e3a\u5904\u7406\u5927\u6570\u636e\u548c\u6570\u7ec4\u8fd0\u7b97\u8fdb\u884c\u4e86\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528 NUMPY \u8fdb\u884c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u5927\u89c4\u6a21\u7684\u6570\u636e\u96c6\uff0c<code>NumPy<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u9ad8\u6548\u7684<code>sum<\/code>\u51fd\u6570\u3002<code>NumPy<\/code>\u7684<code>sum<\/code>\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u591a\u7ef4\u6570\u7ec4\uff0c\u5e76\u4e14\u901a\u5e38\u6bd4\u5185\u7f6e\u7684<code>sum<\/code>\u51fd\u6570\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<p>total = np.sum(array)<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>NumPy<\/code>\u7684<code>sum<\/code>\u51fd\u6570\u4e0d\u4ec5\u66f4\u5feb\uff0c\u800c\u4e14\u8fd8\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u6cbf\u6307\u5b9a\u7684\u8f74\u8fdb\u884c\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5e38\u89c1\u9519\u8bef\u4e0e\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5c1d\u8bd5\u5bf9\u975e\u6570\u5b57\u5143\u7d20\u6c42\u548c\u4f1a\u5bfc\u81f4\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, &#39;two&#39;, 3]<\/p>\n<p>try:<\/p>\n<p>    total = sum(numbers)<\/p>\n<p>except TypeError as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)  # \u8f93\u51fa\uff1aError: unsupported operand type(s) for +: &#39;int&#39; and &#39;str&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u907f\u514d\u6b64\u7c7b\u9519\u8bef\uff0c\u5728\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u524d\uff0c\u786e\u4fdd\u6240\u6709\u5143\u7d20\u90fd\u662f\u6570\u503c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u7684<code>sum<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u80fd\u591f\u7b80\u5316\u5f88\u591a\u5e38\u89c1\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<strong>\u901a\u8fc7\u7406\u89e3<code>sum<\/code>\u51fd\u6570\u7684\u7528\u6cd5\u3001<code>start<\/code>\u53c2\u6570\u7684\u4f5c\u7528\u4ee5\u53ca\u5982\u4f55\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528<\/strong>\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u6570\u636e\u96c6\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u8003\u8651\u4f7f\u7528<code>NumPy<\/code>\u7b49\u4f18\u5316\u5e93\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u4ee5\u4e0a\u6280\u5de7\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u4f60\u53ef\u4ee5\u5728\u5404\u79cd\u573a\u666f\u4e0b\u6709\u6548\u5730\u4f7f\u7528<code>sum<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u63d0\u9ad8Python\u7f16\u7a0b\u7684\u6548\u7387\u548c\u80fd\u529b\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\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u6240\u6709\u5143\u7d20\u7684\u603b\u548c\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u4e3a<code>sum(iterable, start)<\/code>\uff0c\u5176\u4e2d<code>iterable<\/code>\u662f\u8981\u8ba1\u7b97\u7684\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\uff0c\u800c<code>start<\/code>\u662f\u53ef\u9009\u7684\u521d\u59cb\u503c\uff0c\u9ed8\u8ba4\u4e3a0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>sum([1, 2, 3])<\/code>\u4f1a\u8fd4\u56de6\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528sum\u51fd\u6570\u8ba1\u7b97\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u503c\uff1f<\/strong><br \/>sum\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u6570\u503c\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u6570\u503c\u5b58\u50a8\u5728\u5217\u8868\u3001\u5143\u7ec4\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u3002\u5982\u679c\u4f60\u7684\u6570\u636e\u4e2d\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u503c\uff08\u5982\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff09\uff0csum\u51fd\u6570\u4e5f\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u8fd4\u56de\u7ed3\u679c\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528sum\u51fd\u6570\u5904\u7406\u5d4c\u5957\u5217\u8868\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5d4c\u5957\u5217\u8868\uff08\u5373\u5217\u8868\u4e2d\u5305\u542b\u5176\u4ed6\u5217\u8868\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5faa\u73af\u5c06\u5176\u5c55\u5e73\uff0c\u7136\u540e\u518d\u5e94\u7528sum\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u5217\u8868<code>nested_list = [[1, 2], [3, 4]]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sum(sum(inner) for inner in nested_list)<\/code>\u6765\u8ba1\u7b97\u603b\u548c\uff0c\u7ed3\u679c\u4e3a10\u3002\u6b64\u65b9\u6cd5\u6709\u6548\u5730\u5c06\u5185\u5c42\u5217\u8868\u7684\u5143\u7d20\u76f8\u52a0\u540e\uff0c\u518d\u8fdb\u884c\u5916\u5c42\u7684\u603b\u548c\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528sum\u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u5143\u7d20\u7684\u603b\u548c\u3002sum\u51fd\u6570\u662fPython\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u80fd\u591f\u65b9\u4fbf [&hellip;]","protected":false},"author":3,"featured_media":969202,"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\/969191"}],"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=969191"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969191\/revisions"}],"predecessor-version":[{"id":969208,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969191\/revisions\/969208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/969202"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=969191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=969191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=969191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}