{"id":978176,"date":"2024-12-27T06:35:37","date_gmt":"2024-12-26T22:35:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/978176.html"},"modified":"2024-12-27T06:35:39","modified_gmt":"2024-12-26T22:35:39","slug":"python%e5%88%97%e5%a6%82%e4%bd%95%e6%b1%82%e5%92%8c%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/978176.html","title":{"rendered":"python\u5217\u5982\u4f55\u6c42\u548c\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203302\/3175e61c-9d0d-45c3-8e67-5b97d892a012.webp\" alt=\"python\u5217\u5982\u4f55\u6c42\u548c\u51fd\u6570\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868\u5143\u7d20\u8fdb\u884c\u7d2f\u52a0\u3001\u4ee5\u53ca\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982NumPy\u6765\u5b9e\u73b0\u9ad8\u6548\u7684\u6c42\u548c\u64cd\u4f5c<\/strong>\u3002\u5176\u4e2d\uff0c<code>sum()<\/code>\u51fd\u6570\u662f\u6700\u76f4\u63a5\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5904\u7406\u4e00\u822c\u7684\u5217\u8868\u6c42\u548c\u4efb\u52a1\uff0c\u800cNumPy\u5219\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u8fdb\u884c\u590d\u6742\u6570\u503c\u8ba1\u7b97\u7684\u573a\u666f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e<code>sum()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u662f\u6700\u7b80\u5355\u4e14\u5e38\u7528\u7684\u65b9\u5f0f\u6765\u5bf9\u5217\u8868\u8fdb\u884c\u6c42\u548c\u3002<code>sum()<\/code>\u51fd\u6570\u80fd\u591f\u5bf9\u6240\u6709\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\uff0c\u4e3b\u8981\u7528\u4e8e\u6570\u503c\u578b\u5217\u8868\u7684\u6c42\u548c\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(total)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1a<code>sum()<\/code>\u51fd\u6570\u7684\u8bed\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>\u6027\u80fd\u826f\u597d<\/strong>\uff1a\u5bf9\u4e8e\u4e2d\u5c0f\u89c4\u6a21\u7684\u6570\u636e\uff0c<code>sum()<\/code>\u51fd\u6570\u80fd\u591f\u5feb\u901f\u5730\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u4e0d\u8db3<\/strong>\uff1a<code>sum()<\/code>\u51fd\u6570\u4e0d\u652f\u6301\u590d\u6742\u7684\u6c42\u548c\u64cd\u4f5c\uff0c\u5982\u5bf9\u6761\u4ef6\u6c42\u548c\u6216\u591a\u7ef4\u5217\u8868\u6c42\u548c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u5bf9\u5217\u8868\u8fdb\u884c\u66f4\u7075\u6d3b\u7684\u6c42\u548c\u64cd\u4f5c\u3002\u901a\u8fc7\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u7d2f\u52a0\u5230\u4e00\u4e2a\u521d\u59cb\u503c\u4e3a0\u7684\u53d8\u91cf\u4e0a\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 num in numbers:<\/p>\n<p>    total += num<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff0c\u6216\u5bf9\u591a\u7ef4\u5217\u8868\u8fdb\u884c\u6c42\u548c\u3002<\/li>\n<li><strong>\u53ef\u6269\u5c55\u6027\u5f3a<\/strong>\uff1a\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u52a0\u5165\u5176\u4ed6\u64cd\u4f5c\uff0c\u5982\u7edf\u8ba1\u7b26\u5408\u67d0\u4e00\u6761\u4ef6\u7684\u5143\u7d20\u4e2a\u6570\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4ee3\u7801\u5197\u957f<\/strong>\uff1a\u76f8\u6bd4\u4e8e<code>sum()<\/code>\u51fd\u6570\uff0c\u5faa\u73af\u7684\u65b9\u6cd5\u4ee3\u7801\u8f83\u957f\uff0c\u4e0d\u591f\u7b80\u6d01\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\u3002\u4f7f\u7528NumPy\u7684<code>np.sum()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u5927\u89c4\u6a21\u6570\u636e\u8fdb\u884c\u5feb\u901f\u6c42\u548c\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(total)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u6027<\/strong>\uff1aNumPy\u5229\u7528\u5e95\u5c42C\u8bed\u8a00\u5b9e\u73b0\uff0c\u8fd0\u7b97\u901f\u5ea6\u5feb\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1a\u652f\u6301\u5bf9\u591a\u7ef4\u6570\u7ec4\u8fdb\u884c\u6c42\u548c\uff0c\u53ef\u4ee5\u6307\u5b9a\u6c42\u548c\u7684\u8f74\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9700\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<\/strong>\uff1a\u4f7f\u7528NumPy\u9700\u8981\u989d\u5916\u5b89\u88c5\u5e93\uff0c\u4e0d\u9002\u5408\u6240\u6709\u573a\u666f\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c<\/strong>\uff1a\u5bf9\u4e8e\u53ea\u9700\u8981\u7b80\u5355\u6c42\u548c\u7684\u60c5\u51b5\uff0c\u5f15\u5165NumPy\u53ef\u80fd\u589e\u52a0\u4e86\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5bf9\u591a\u7ef4\u5217\u8868\u6c42\u548c<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u7ef4\u5217\u8868\uff08\u5982\u4e8c\u7ef4\u6570\u7ec4\uff09\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u5faa\u73af\u6216NumPy\u5e93\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5faa\u73af<\/h4>\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 = 0<\/p>\n<p>for row in matrix:<\/p>\n<p>    total += sum(row)<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528NumPy<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([<\/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 = np.sum(matrix)<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6761\u4ef6\u6c42\u548c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5bf9\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u5143\u7d20\u8fdb\u884c\u6c42\u548c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u7ed3\u5408NumPy\u7684\u5e03\u5c14\u7d22\u5f15\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\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 &gt; 2])<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a12<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528NumPy\u5e03\u5c14\u7d22\u5f15<\/h4>\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[numbers &gt; 2])<\/p>\n<p>print(total)  # \u8f93\u51fa\uff1a12<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5bf9Python\u5217\u8868\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\u3002<strong>\u5bf9\u4e8e\u7b80\u5355\u7684\u5217\u8868\u6c42\u548c\uff0c\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u662f\u6700\u65b9\u4fbf\u7684\u9009\u62e9\uff0c\u800c\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848<\/strong>\u3002\u5728\u5904\u7406\u591a\u7ef4\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5faa\u73af\u6216NumPy\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u53e6\u5916\uff0c\u5bf9\u4e8e\u6709\u6761\u4ef6\u7684\u6c42\u548c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6216NumPy\u7684\u5e03\u5c14\u7d22\u5f15\u8fdb\u884c\u5b9e\u73b0\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u5217\u8868\u8fdb\u884c\u6c42\u548c\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u8f7b\u677e\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868<code>numbers = [1, 2, 3, 4, 5]<\/code>\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>total = sum(numbers)<\/code>\u6765\u8ba1\u7b97\u5176\u603b\u548c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u503c\u7c7b\u578b\u7684\u5217\u8868\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u5217\u8868\u6c42\u548c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6765\u5b9e\u73b0\u5217\u8868\u6c42\u548c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\uff0c\u9010\u4e2a\u7d2f\u52a0\u5143\u7d20\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>sum()<\/code>\uff0c\u4f8b\u5982\uff1a<code>total = sum(x for x in numbers)<\/code>\uff0c\u8fd9\u5728\u9700\u8981\u5bf9\u5143\u7d20\u8fdb\u884c\u7279\u5b9a\u6761\u4ef6\u8fc7\u6ee4\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u975e\u6570\u503c\u5143\u7d20\u7684\u5217\u8868\u6c42\u548c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u975e\u6570\u503c\u5143\u7d20\u7684\u5217\u8868\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8fc7\u6ee4\u6389\u8fd9\u4e9b\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5217\u8868<code>mixed_list = [1, &#39;a&#39;, 2, None, 3]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>total = sum(x for x in mixed_list if isinstance(x, (int, float)))<\/code>\u6765\u786e\u4fdd\u53ea\u5bf9\u6570\u503c\u7c7b\u578b\u7684\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u3002\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u4ee3\u7801\u7684\u5b89\u5168\u6027\u548c\u6709\u6548\u6027\uff0c\u907f\u514d\u4e86\u56e0\u7c7b\u578b\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6c42\u548c\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684sum()\u51fd\u6570\u3001\u4f7f\u7528\u5faa [&hellip;]","protected":false},"author":3,"featured_media":978181,"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\/978176"}],"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=978176"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978176\/revisions"}],"predecessor-version":[{"id":978182,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978176\/revisions\/978182"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/978181"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=978176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=978176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=978176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}