{"id":1005573,"date":"2024-12-27T10:36:54","date_gmt":"2024-12-27T02:36:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1005573.html"},"modified":"2024-12-27T10:36:57","modified_gmt":"2024-12-27T02:36:57","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%97%e8%a1%a8%e6%b1%82%e5%92%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1005573.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5217\u8868\u6c42\u548c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082047\/34229e01-9ea4-44a0-aaf4-2bbe07945dd2.webp\" alt=\"python\u4e2d\u5982\u4f55\u5217\u8868\u6c42\u548c\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5217\u8868\u6c42\u548c\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6c42\u548c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408sum()\u3001\u4f7f\u7528Numpy\u5e93\u7684sum()\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u7b80\u5355\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u3002\u5b83\u80fd\u591f\u5feb\u901f\u5bf9\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6c42\u548c\uff0c\u5e76\u4e14\u4ee3\u7801\u7b80\u6d01\u6613\u8bfb\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()<\/strong>\u662fPython\u4e2d\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c<code>sum()<\/code>\u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6c42\u548c\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u5217\u8868<code>numbers<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sum(numbers)<\/code>\u6765\u8ba1\u7b97\u5176\u6240\u6709\u5143\u7d20\u7684\u548c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u6613\u7528\u6027\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u904d\u5386\u5217\u8868\u5143\u7d20\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5217\u8868\u6c42\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5bf9\u5404\u79cd\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u7684\u8bb2\u89e3\uff0c\u4ee5\u4fbf\u4f60\u5728\u4e0d\u540c\u7684\u573a\u5408\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()<\/p>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u662f\u6700\u5e38\u7528\u7684\u5217\u8868\u6c42\u548c\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u4e13\u4e3a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8bbe\u8ba1\uff0c\u80fd\u591f\u5feb\u901f\u8ba1\u7b97\u6240\u6709\u5143\u7d20\u7684\u603b\u548c\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868<code>numbers = [1, 2, 3, 4, 5]<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u6c42\u548c\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 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5217\u8868\u3002<code>sum()<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u9700\u8981\u6c42\u548c\u7684\u5217\u8868\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u53ef\u9009\u7684\u8d77\u59cb\u503c\uff08\u9ed8\u8ba4\u4e3a0\uff09\u3002\u5982\u679c\u4f60\u60f3\u4ece\u67d0\u4e2a\u7279\u5b9a\u503c\u5f00\u59cb\u7d2f\u52a0\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6c42\u548c<\/p>\n<\/p>\n<p><p>\u867d\u7136<code>sum()<\/code>\u51fd\u6570\u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u5408\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u66f4\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u5e76\u8fdb\u884c\u7d2f\u52a0\u3002\u4f8b\u5982\uff1a<\/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 num in numbers:<\/p>\n<p>    total += num<\/p>\n<p>print(total)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4ee3\u7801\u8f83\u957f\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5728\u6c42\u548c\u8fc7\u7a0b\u4e2d\u5bf9\u67d0\u4e9b\u5143\u7d20\u8fdb\u884c\u7279\u6b8a\u5904\u7406\uff0c\u6216\u8005\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u505c\u6b62\u7d2f\u52a0\uff0c\u5faa\u73af\u904d\u5386\u5c31\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408sum()<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u80fd\u591f\u5feb\u901f\u751f\u6210\u5217\u8868\u3002\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6c42\u548c\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ea\u5bf9\u5217\u8868\u4e2d\u7684\u5076\u6570\u6c42\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = sum([num for num in numbers if num % 2 == 0])<\/p>\n<p>print(total)  # \u8f93\u51fa 6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u65e2\u4fdd\u6301\u4e86\u4ee3\u7801\u7684\u7b80\u6d01\u6027\uff0c\u53c8\u63d0\u4f9b\u4e86\u8db3\u591f\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u8fc7\u6ee4\u6216\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Numpy\u5e93\u7684sum()\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0cPython\u7684Numpy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u3002Numpy\u7684<code>sum()<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5bf9\u5217\u8868\u6c42\u548c\uff0c\u8fd8\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u7684\u6c42\u548c\u64cd\u4f5c\u3002\u4f8b\u5982\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>total = np.sum(numbers)<\/p>\n<p>print(total)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Numpy\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u9ad8\u6548\u7684\u6570\u7ec4\u8fd0\u7b97\u80fd\u529b\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u6216\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u573a\u5408\u3002\u6b64\u5916\uff0cNumpy\u7684<code>sum()<\/code>\u51fd\u6570\u8fd8\u652f\u6301\u5bf9\u591a\u7ef4\u6570\u7ec4\u7684\u7279\u5b9a\u8f74\u8fdb\u884c\u6c42\u548c\uff0c\u8fd9\u5728\u5904\u7406\u77e9\u9635\u6216\u591a\u7ef4\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5217\u8868\u6c42\u548c\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5217\u8868\u6c42\u548c\uff0c\u76f4\u63a5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u662f\u6700\u4f73\u9009\u62e9\uff1b\u5982\u679c\u9700\u8981\u5728\u6c42\u548c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\u6216\u6761\u4ef6\u5224\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\uff1b\u800c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u591a\u7ef4\u6570\u7ec4\u65f6\uff0cNumpy\u5e93\u7684<code>sum()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5408\u7406\u5730\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5c06\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5904\u7406\u6570\u636e\uff0c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u5b57\u8fdb\u884c\u6c42\u548c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u5b57\u8fdb\u884c\u6c42\u548c\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5217\u8868\u8fdb\u884c\u6c42\u548c\u3002\u4f8b\u5982\uff0c<code>sum(my_list)<\/code>\u4f1a\u8fd4\u56de\u5217\u8868<code>my_list<\/code>\u4e2d\u6240\u6709\u5143\u7d20\u7684\u603b\u548c\u3002\u53e6\u5916\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u505a\u6cd5\uff0c\u6bd4\u5982<code>sum(x for x in my_list if isinstance(x, (int, float)))<\/code>\u53ef\u4ee5\u786e\u4fdd\u53ea\u5bf9\u6570\u5b57\u8fdb\u884c\u6c42\u548c\u3002<\/p>\n<p><strong>\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u5217\u8868\u6c42\u548c\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u6c42\u548c\u80fd\u591f\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u7ec4\u65f6\u3002NumPy\u7684<code>numpy.sum()<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u4e00\u7ef4\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u591a\u7ef4\u6570\u7ec4\u8fdb\u884c\u6c42\u548c\u3002\u5b83\u7684\u8fd0\u7b97\u901f\u5ea6\u901a\u5e38\u6bd4\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u5feb\u5f88\u591a\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u503c\u8ba1\u7b97\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u975e\u6570\u503c\u5143\u7d20\u7684\u5217\u8868\u4ee5\u8fdb\u884c\u6c42\u548c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u975e\u6570\u503c\u5143\u7d20\u7684\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7b5b\u9009\u51fa\u6709\u6548\u7684\u6570\u5b57\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\u4e3a<code>sum(x for x in my_list if isinstance(x, (int, float)))<\/code>\uff0c\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u904d\u5386\u5217\u8868<code>my_list<\/code>\uff0c\u53ea\u5bf9\u5176\u4e2d\u7684\u6574\u6570\u548c\u6d6e\u70b9\u6570\u8fdb\u884c\u6c42\u548c\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u975e\u6570\u503c\u5143\u7d20\u5bfc\u81f4\u7684\u9519\u8bef\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5217\u8868\u6c42\u548c\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6c42\u548c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408s [&hellip;]","protected":false},"author":3,"featured_media":1005583,"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\/1005573"}],"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=1005573"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005573\/revisions"}],"predecessor-version":[{"id":1005586,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005573\/revisions\/1005586"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1005583"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1005573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1005573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1005573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}