{"id":1081994,"date":"2025-01-08T12:44:45","date_gmt":"2025-01-08T04:44:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1081994.html"},"modified":"2025-01-08T12:44:47","modified_gmt":"2025-01-08T04:44:47","slug":"python%e5%a6%82%e4%bd%95%e5%8f%a0%e5%8a%a0%e5%88%97%e8%a1%a8%e9%87%8c%e9%9d%a2%e7%9a%84%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1081994.html","title":{"rendered":"python\u5982\u4f55\u53e0\u52a0\u5217\u8868\u91cc\u9762\u7684\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183732\/90220e30-b5c6-4a1f-9c09-accb75254f5b.webp\" alt=\"python\u5982\u4f55\u53e0\u52a0\u5217\u8868\u91cc\u9762\u7684\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u53e0\u52a0\u5217\u8868\u91cc\u9762\u7684\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u65b9\u6cd5<\/strong>\u662f\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528sum()\u51fd\u6570\u4f20\u5165\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u6765\u6c42\u548c\uff0c\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u5e76\u5217\u4e3e\u5176\u4ed6\u53ef\u80fd\u7684\u65b9\u6cd5\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\u5b83\u53ef\u4ee5\u7528\u6765\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u8fdb\u884c\u6c42\u548c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\u76f4\u89c2\u3002\u793a\u4f8b\u5982\u4e0b\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>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>sum(numbers)<\/code>\u5bf9\u5217\u8868<code>numbers<\/code>\u4e2d\u7684\u6240\u6709\u6570\u8fdb\u884c\u6c42\u548c\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf<code>result<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u5e76\u8fdb\u884c\u7d2f\u52a0\u6765\u5b9e\u73b0\u5217\u8868\u4e2d\u6570\u7684\u53e0\u52a0\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = 0<\/p>\n<p>for number in numbers:<\/p>\n<p>    result += number<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf<code>result<\/code>\u4e3a0\uff0c\u7136\u540e\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u503c\u52a0\u5230<code>result<\/code>\u4e2d\uff0c\u6700\u7ec8\u5f97\u5230\u7684<code>result<\/code>\u5c31\u662f\u5217\u8868\u4e2d\u6240\u6709\u6570\u7684\u548c\u3002<\/p>\n<\/p>\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\u7684\u5217\u8868\u751f\u6210\u65b9\u5f0f\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7528\u5b83\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u5217\u8868\u4e2d\u6570\u7684\u53e0\u52a0\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/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>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8be5\u5217\u8868\u5305\u542b\u4e86\u539f\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u7136\u540e\u901a\u8fc7<code>sum()<\/code>\u51fd\u6570\u5bf9\u8fd9\u4e2a\u65b0\u5217\u8868\u8fdb\u884c\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528functools.reduce()<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>functools<\/code>\u6a21\u5757\u4e2d\u7684<code>reduce()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5bf9\u6570\u636e\u8fdb\u884c\u9012\u5f52\u64cd\u4f5c\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/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>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>reduce()<\/code>\u51fd\u6570\u4f7f\u7528\u4e00\u4e2a\u533f\u540d\u51fd\u6570<code>lambda<\/code>\u6765\u9010\u4e2a\u7d2f\u52a0\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u6700\u7ec8\u5f97\u5230\u6240\u6709\u5143\u7d20\u7684\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u8fdb\u884c\u6570\u636e\u79d1\u5b66\u6216\u9700\u8981\u5904\u7406\u5927\u6570\u636e\uff0cNumPy\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002NumPy\u63d0\u4f9b\u4e86\u5bf9\u6570\u7ec4\u8fdb\u884c\u9ad8\u6548\u64cd\u4f5c\u7684\u529f\u80fd\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/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\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>np.array()<\/code>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>np.sum()<\/code>\u5bf9\u6570\u7ec4\u8fdb\u884c\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u7f16\u7a0b\u6280\u5de7\uff0c\u5c3d\u7ba1\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4e0d\u5982<code>sum()<\/code>\u51fd\u6570\u7b80\u6d01\uff0c\u4f46\u5b83\u4e5f\u662f\u4e00\u79cd\u5b9e\u73b0\u5217\u8868\u6c42\u548c\u7684\u65b9\u6cd5\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/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>result = recursive_sum(numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>recursive_sum()<\/code>\u51fd\u6570\u4f7f\u7528\u9012\u5f52\u6765\u9010\u4e2a\u7d2f\u52a0\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u76f4\u5230\u5217\u8868\u4e3a\u7a7a\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u5b83\u662f\u4ee5\u4e00\u79cd\u60f0\u6027\u8ba1\u7b97\u7684\u65b9\u5f0f\u751f\u6210\u503c\uff0c\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/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>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u60f0\u6027\u6c42\u503c\u7684\u751f\u6210\u5668\uff0c\u5e76\u901a\u8fc7<code>sum()<\/code>\u51fd\u6570\u5bf9\u751f\u6210\u5668\u8fdb\u884c\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u5e38\u7528\u7684\u5de5\u5177\uff0c\u7279\u522b\u662f\u5f53\u6570\u636e\u5b58\u50a8\u5728DataFrame\u4e2d\u65f6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/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>result = numbers.sum()<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u7684<code>Series<\/code>\u5bf9\u8c61\u5b58\u50a8\u5217\u8868\u6570\u636e\uff0c\u5e76\u4f7f\u7528<code>sum()<\/code>\u65b9\u6cd5\u5bf9\u5176\u8fdb\u884c\u6c42\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u8fdb\u884c\u6c42\u548c\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()<\/strong>\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u9700\u6c42\uff0c\u5982\u9012\u5f52\u8ba1\u7b97\u3001\u5904\u7406\u5927\u6570\u636e\u96c6\u6216\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u53ef\u4ee5\u9009\u62e9\u5176\u4ed6\u65b9\u6cd5\u5982\u5faa\u73af\u904d\u5386\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001NumPy\u5e93\u3001Pandas\u5e93\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u8981\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5217\u8868\u4e2d\u7684\u6570\u7684\u53e0\u52a0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5feb\u901f\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u548c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u5feb\u901f\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u6570\u5b57\u7684\u548c\u3002\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff0c<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\u8f7b\u677e\u5f97\u5230\u6240\u6709\u6570\u5b57\u7684\u603b\u548c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5faa\u73af\u53e0\u52a0\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\uff0c\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\u6765\u53e0\u52a0\u5143\u7d20\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff0c\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\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u9002\u5408\u9700\u8981\u5728\u53e0\u52a0\u8fc7\u7a0b\u4e2d\u6267\u884c\u989d\u5916\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8ba1\u7b97\u603b\u548c\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u4e0e<code>sum()<\/code>\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u7b80\u5316\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u8981\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u6b63\u6570\u7684\u603b\u548c\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-python\">total = sum(num for num in my_list if num &gt; 0)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6613\u4e8e\u9605\u8bfb\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u590d\u6742\u6761\u4ef6\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u53e0\u52a0\u5217\u8868\u91cc\u9762\u7684\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1082001,"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\/1081994"}],"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=1081994"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081994\/revisions"}],"predecessor-version":[{"id":1082005,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081994\/revisions\/1082005"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1082001"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1081994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1081994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1081994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}