{"id":1166776,"date":"2025-01-15T15:36:07","date_gmt":"2025-01-15T07:36:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1166776.html"},"modified":"2025-01-15T15:36:09","modified_gmt":"2025-01-15T07:36:09","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e5%88%97%e8%a1%a8%e6%95%b0%e6%8d%ae%e6%b1%82%e5%92%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1166776.html","title":{"rendered":"python\u5982\u4f55\u628a\u5217\u8868\u6570\u636e\u6c42\u548c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25210829\/2647d73d-d587-4dc0-bb77-2ddc86ca7376.webp\" alt=\"python\u5982\u4f55\u628a\u5217\u8868\u6570\u636e\u6c42\u548c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u628a\u5217\u8868\u6570\u636e\u6c42\u548c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u4ee5\u53ca\u4f7f\u7528numpy\u5e93\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u7b80\u5355\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e0d\u4ec5\u64cd\u4f5c\u7b80\u5355\uff0c\u800c\u4e14\u6027\u80fd\u8f83\u4f18\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u53ca\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5217\u8868\u4e2d\u7684\u6240\u6709\u6570\u503c\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u3002\u5b83\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>sum()<\/code>\u51fd\u6570\u5373\u53ef\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(&quot;\u5217\u8868\u6c42\u548c\u7684\u7ed3\u679c\u662f\uff1a&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1a\u4e0d\u9700\u8981\u989d\u5916\u7684\u5bfc\u5165\u6216\u590d\u6742\u7684\u903b\u8f91\u3002<\/li>\n<li><strong>\u6027\u80fd\u8f83\u4f18<\/strong>\uff1a\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c<code>sum()<\/code>\u51fd\u6570\u7684\u6027\u80fd\u8868\u73b0\u4f18\u8d8a\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u53ea\u80fd\u7528\u4e8e\u6570\u503c\u7c7b\u578b\u7684\u5217\u8868\uff0c\u65e0\u6cd5\u76f4\u63a5\u5e94\u7528\u4e8e\u6df7\u5408\u7c7b\u578b\u7684\u5217\u8868\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u904d\u5386\u5217\u8868\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u7d2f\u52a0\u5230\u4e00\u4e2a\u521d\u59cb\u503c\u4e3a0\u7684\u53d8\u91cf\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5217\u8868\u7684\u6c42\u548c\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6ca1\u6709<code>sum()<\/code>\u51fd\u6570\u7b80\u6d01\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\uff08\u5982\u9700\u8981\u5bf9\u5143\u7d20\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\u540e\u518d\u6c42\u548c\uff09\u4f1a\u975e\u5e38\u6709\u7528\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(&quot;\u5217\u8868\u6c42\u548c\u7684\u7ed3\u679c\u662f\uff1a&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u5728\u6c42\u548c\u8fc7\u7a0b\u4e2d\u5bf9\u5143\u7d20\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\u3002<\/li>\n<li><strong>\u9002\u7528\u8303\u56f4\u5e7f<\/strong>\uff1a\u53ef\u4ee5\u5904\u7406\u66f4\u591a\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982\u5d4c\u5957\u5217\u8868\u7b49\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u4ee3\u7801\u5197\u957f<\/strong>\uff1a\u76f8\u8f83\u4e8e\u76f4\u63a5\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\uff0c\u4ee3\u7801\u663e\u5f97\u66f4\u4e3a\u590d\u6742\u3002<\/li>\n<li><strong>\u6027\u80fd\u53ef\u80fd\u8f83\u4f4e<\/strong>\uff1a\u5c24\u5176\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6548\u7387\u4e0d\u5982\u5185\u7f6e\u51fd\u6570\u9ad8\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684Python\u7279\u6027\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u5bf9\u5217\u8868\u7684\u6c42\u548c\u3002\u5176\u539f\u7406\u662f\u901a\u8fc7\u4e00\u4e2a\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff0c\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u7d2f\u52a0\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(&quot;\u5217\u8868\u6c42\u548c\u7684\u7ed3\u679c\u662f\uff1a&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u901a\u8fc7\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u590d\u6742\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1a\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\uff0c\u5bf9\u7279\u5b9a\u5143\u7d20\u6c42\u548c\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u719f\u6089\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u5f00\u53d1\u8005\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u53ef\u80fd\u8f83\u5dee\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u76f8\u6bd4\u76f4\u63a5\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\uff0c\u6027\u80fd\u4e0a\u6ca1\u6709\u660e\u663e\u63d0\u5347\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u7684\u573a\u666f\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u6c42\u548c\u65b9\u6cd5\u3002numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\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>total = np.sum(numbers)<\/p>\n<p>print(&quot;\u5217\u8868\u6c42\u548c\u7684\u7ed3\u679c\u662f\uff1a&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u4e13\u4e3a\u6570\u503c\u8ba1\u7b97\u8bbe\u8ba1\uff0c\u6027\u80fd\u4f18\u8d8a\uff0c\u5c24\u5176\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1a\u63d0\u4f9b\u4e86\u4f17\u591a\u5176\u4ed6\u6570\u503c\u8ba1\u7b97\u529f\u80fd\uff0c\u53ef\u4ee5\u6ee1\u8db3\u66f4\u591a\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u590d\u6742\u6027<\/strong>\uff1a\u9700\u8981\u989d\u5916\u5b89\u88c5\u548c\u5bfc\u5165\u5e93\uff0c\u5bf9\u4e8e\u53ea\u9700\u8981\u7b80\u5355\u6c42\u548c\u7684\u573a\u666f\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c<\/strong>\uff1a\u9700\u8981\u4e00\u4e9b\u5b66\u4e60\u6210\u672c\u6765\u638c\u63e1numpy\u7684\u57fa\u672c\u7528\u6cd5\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f7f\u7528functools.reduce\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>functools.reduce<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u4e8c\u5143\u64cd\u4f5c\u5e94\u7528\u4e8e\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u5217\u8868\u7684\u6c42\u548c\u3002\u5b83\u662f\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e00\u90e8\u5206\uff0c\u9002\u7528\u4e8e\u4e00\u4e9b\u7279\u5b9a\u7684\u573a\u666f\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;\u5217\u8868\u6c42\u548c\u7684\u7ed3\u679c\u662f\uff1a&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b<\/strong>\uff1a\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e8c\u5143\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8ba1\u7b97\u9700\u6c42\u3002<\/li>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1a\u4e0d\u4ec5\u9650\u4e8e\u6c42\u548c\uff0c\u8fd8\u53ef\u4ee5\u5e94\u7528\u4e8e\u5176\u4ed6\u9700\u8981\u4e8c\u5143\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u590d\u6742\u6027\u9ad8<\/strong>\uff1a\u76f8\u6bd4\u5176\u4ed6\u65b9\u6cd5\uff0c\u4ee3\u7801\u663e\u5f97\u66f4\u4e3a\u590d\u6742\uff0c\u4e0d\u6613\u7406\u89e3\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6027\u80fd\u4e0d\u5982<code>sum()<\/code>\u51fd\u6570\u9ad8\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u7f16\u7a0b\u6280\u672f\uff0c\u901a\u8fc7\u51fd\u6570\u81ea\u8eab\u7684\u8c03\u7528\u6765\u5b9e\u73b0\u5bf9\u5217\u8868\u7684\u6c42\u548c\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5e38\u7528\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u7684\u7f16\u7a0b\u98ce\u683c\u4e2d\u4f9d\u7136\u6709\u5176\u72ec\u7279\u7684\u4ef7\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_sum(numbers):<\/p>\n<p>    if not numbers:<\/p>\n<p>        return 0<\/p>\n<p>    return numbers[0] + recursive_sum(numbers[1:])<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = recursive_sum(numbers)<\/p>\n<p>print(&quot;\u5217\u8868\u6c42\u548c\u7684\u7ed3\u679c\u662f\uff1a&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7f16\u7a0b\u98ce\u683c<\/strong>\uff1a\u9002\u7528\u4e8e\u559c\u6b22\u9012\u5f52\u7f16\u7a0b\u98ce\u683c\u7684\u5f00\u53d1\u8005\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u6269\u5c55\u5230\u66f4\u590d\u6742\u7684\u9012\u5f52\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u548c\u6548\u7387<\/strong>\uff1a\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u9012\u5f52\u65b9\u6cd5\u7684\u6027\u80fd\u548c\u6548\u7387\u8f83\u4f4e\uff0c\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002<\/li>\n<li><strong>\u4ee3\u7801\u590d\u6742\u6027<\/strong>\uff1a\u9012\u5f52\u7684\u7406\u89e3\u548c\u8c03\u8bd5\u76f8\u5bf9\u56f0\u96be\uff0c\u4e0d\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u6570\u636e\u6846\u548c\u66f4\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u573a\u666f\uff0cpandas\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u6c42\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>numbers = pd.Series([1, 2, 3, 4, 5])<\/p>\n<p>total = numbers.sum()<\/p>\n<p>print(&quot;\u5217\u8868\u6c42\u548c\u7684\u7ed3\u679c\u662f\uff1a&quot;, total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1a\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u6570\u636e\u6846\u7b49\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u4e13\u4e3a\u6570\u636e\u5206\u6790\u8bbe\u8ba1\uff0c\u6027\u80fd\u4f18\u8d8a\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u590d\u6742\u6027<\/strong>\uff1a\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165\u5e93\uff0c\u5bf9\u4e8e\u7b80\u5355\u6c42\u548c\u7684\u573a\u666f\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c<\/strong>\uff1a\u9700\u8981\u5b66\u4e60pandas\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u5bf9\u4e8e\u4e0d\u4ece\u4e8b\u6570\u636e\u5206\u6790\u7684\u5f00\u53d1\u8005\u53ef\u80fd\u4e0d\u592a\u5212\u7b97\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u5217\u8868\u7684\u6c42\u548c\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u662f\u6700\u7b80\u5355\u3001\u6700\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u7684\u573a\u666f\uff0c\u5efa\u8bae\u4f7f\u7528<strong>numpy\u5e93<\/strong>\u3002\u5982\u679c\u9700\u8981\u5728\u6c42\u548c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<strong>\u5faa\u73af\u904d\u5386\u5217\u8868<\/strong>\u6216<strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u3002\u603b\u4e4b\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5efa\u8bae\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u610f\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728Python\u4e2d\u5904\u7406\u5217\u8868\u6c42\u548c\u7684\u95ee\u9898\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5217\u8868\u4e2d\u7684\u6570\u5b57\u76f8\u52a0\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u6765\u5feb\u901f\u6c42\u548c\u5217\u8868\u4e2d\u7684\u6570\u5b57\u3002\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>sum()<\/code>\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<code>total = sum(my_list)<\/code>\uff0c\u5176\u4e2d<code>my_list<\/code>\u662f\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\u3002\u8fd9\u6837\u5c31\u80fd\u5f97\u5230\u5217\u8868\u6240\u6709\u5143\u7d20\u7684\u548c\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5143\u7d20\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff08list comprehension\uff09\u6765\u8fc7\u6ee4\u51fa\u6570\u5b57\u3002\u4f8b\u5982\uff1a<code>total = sum(x for x in my_list if isinstance(x, (int, float)))<\/code>\u3002\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u786e\u4fdd\u53ea\u6709\u6570\u5b57\u88ab\u6c42\u548c\uff0c\u4ece\u800c\u907f\u514d\u56e0\u7c7b\u578b\u9519\u8bef\u800c\u5bfc\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u624b\u52a8\u8ba1\u7b97\u5217\u8868\u7684\u548c\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u4f7f\u7528\u5faa\u73af\u662f\u4e00\u79cd\u76f4\u89c2\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u5217\u8868\u7684\u548c\u3002\u53ef\u4ee5\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u7d2f\u52a0\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">total = 0\nfor number in my_list:\n    total += number\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982<code>sum()<\/code>\u51fd\u6570\u7b80\u6d01\uff0c\u4f46\u5b83\u5141\u8bb8\u4f60\u5728\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u903b\u8f91\u6216\u6761\u4ef6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u975e\u5e38\u5927\u7684\u5217\u8868\u6c42\u548c\u95ee\u9898\uff1f<\/strong><br \/>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>numpy<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u6c42\u548c\u529f\u80fd\u3002\u901a\u8fc7<code>numpy.sum()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5feb\u901f\u5730\u5bf9\u5927\u89c4\u6a21\u6570\u636e\u8fdb\u884c\u6c42\u548c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\ntotal = np.sum(np.array(my_list))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u63d0\u5347\u4e86\u6027\u80fd\uff0c\u8fd8\u6709\u52a9\u4e8e\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u5b66\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u628a\u5217\u8868\u6570\u636e\u6c42\u548c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u4ee5\u53ca\u4f7f\u7528num [&hellip;]","protected":false},"author":3,"featured_media":1166780,"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\/1166776"}],"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=1166776"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1166776\/revisions"}],"predecessor-version":[{"id":1166782,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1166776\/revisions\/1166782"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1166780"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1166776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1166776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1166776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}