{"id":1182713,"date":"2025-01-15T19:06:28","date_gmt":"2025-01-15T11:06:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182713.html"},"modified":"2025-01-15T19:06:32","modified_gmt":"2025-01-15T11:06:32","slug":"python%e5%a6%82%e4%bd%95%e7%ae%97%e6%95%b0%e7%bb%84%e4%b8%ad%e4%b9%98%e7%a7%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182713.html","title":{"rendered":"python\u5982\u4f55\u7b97\u6570\u7ec4\u4e2d\u4e58\u79ef"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130638\/a492a4a6-c5c2-4485-ba41-235482c324f1.webp\" alt=\"python\u5982\u4f55\u7b97\u6570\u7ec4\u4e2d\u4e58\u79ef\" \/><\/p>\n<p><p> Python\u4e2d\u8ba1\u7b97\u6570\u7ec4\u4e2d\u4e58\u79ef\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\u3001\u4f7f\u7528reduce\u51fd\u6570\u3001\u4f7f\u7528numpy\u5e93<\/strong>\u7b49\u3002\u8fd9\u91cc\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528reduce\u51fd\u6570<\/strong>\u3002<\/p>\n<\/p>\n<p><p><strong>reduce\u51fd\u6570<\/strong>\u662fPython functools\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u5e8f\u5217\u8fdb\u884c\u7d2f\u8ba1\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528reduce\u51fd\u6570\u914d\u5408lambda\u8868\u8fbe\u5f0f\u6765\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>arr = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528reduce\u51fd\u6570\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef<\/strong><\/h2>\n<p>product = reduce(lambda x, y: x * y, arr)<\/p>\n<p>print(&quot;\u6570\u7ec4\u7684\u4e58\u79ef\u662f:&quot;, product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>reduce<\/code>\u51fd\u6570\u4f1a\u4ece\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u5c06\u6bcf\u4e2a\u5143\u7d20\u4f9d\u6b21\u4e0e\u524d\u4e00\u4e2a\u7d2f\u8ba1\u7ed3\u679c\u8fdb\u884c\u76f8\u4e58\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u6700\u7ec8\u5f97\u5230\u6570\u7ec4\u7684\u4e58\u79ef\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u6570\u7ec4\u4e2d\u7684\u4e58\u79ef\uff0c\u5e76\u5206\u6790\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4<\/h2>\n<\/p>\n<p><h3>\u57fa\u672c\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p>\u6700\u76f4\u63a5\u7684\u8ba1\u7b97\u6570\u7ec4\u4e58\u79ef\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u9010\u4e2a\u5143\u7d20\u76f8\u4e58\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4efb\u4f55Python\u7248\u672c\uff0c\u4e0d\u9700\u8981\u989d\u5916\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def array_product(arr):<\/p>\n<p>    product = 1<\/p>\n<p>    for num in arr:<\/p>\n<p>        product *= num<\/p>\n<p>    return product<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>arr = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef<\/strong><\/h2>\n<p>product = array_product(arr)<\/p>\n<p>print(&quot;\u6570\u7ec4\u7684\u4e58\u79ef\u662f:&quot;, product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u61c2\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li>\u4e0d\u4f9d\u8d56\u5916\u90e8\u5e93\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u7ec4\uff0c\u53ef\u80fd\u6548\u7387\u4e0d\u591f\u9ad8\u3002<\/li>\n<li>\u6ca1\u6709\u5185\u7f6e\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u4f8b\u5982\u7a7a\u6570\u7ec4\u6216\u975e\u6570\u5b57\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u4f7f\u7528reduce\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>\u57fa\u672c\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p><code>reduce<\/code>\u51fd\u6570\u662fPython functools\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u5e8f\u5217\u8fdb\u884c\u7d2f\u8ba1\u64cd\u4f5c\u3002\u5b83\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u5e8f\u5217\u3002\u51fd\u6570\u5fc5\u987b\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7d2f\u8ba1\u7ed3\u679c\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5e8f\u5217\u4e2d\u7684\u5f53\u524d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>arr = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528reduce\u51fd\u6570\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef<\/strong><\/h2>\n<p>product = reduce(lambda x, y: x * y, arr)<\/p>\n<p>print(&quot;\u6570\u7ec4\u7684\u4e58\u79ef\u662f:&quot;, product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01\uff0c\u51cf\u5c11\u4e86\u5faa\u73af\u7684\u4f7f\u7528\u3002<\/li>\n<li>\u5185\u7f6e\u51fd\u6570\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5bfc\u5165functools\u6a21\u5757\uff0c\u589e\u52a0\u4e86\u590d\u6742\u6027\u3002<\/li>\n<li>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u7ec4\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982\u5176\u4ed6\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><h2>\u4e09\u3001\u4f7f\u7528numpy\u5e93<\/h2>\n<\/p>\n<p><h3>\u57fa\u672c\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528numpy\u5e93\u7684<code>prod<\/code>\u51fd\u6570\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u4f7f\u7528numpy\u7684prod\u51fd\u6570\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef<\/strong><\/h2>\n<p>product = np.prod(arr)<\/p>\n<p>print(&quot;\u6570\u7ec4\u7684\u4e58\u79ef\u662f:&quot;, product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6027\u80fd\u9ad8\u6548\uff0c\u9002\u5408\u5927\u6570\u636e\u5904\u7406\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5176\u4ed6\u6570\u7ec4\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165numpy\u5e93\uff0c\u589e\u52a0\u4e86\u590d\u6742\u6027\u3002<\/li>\n<li>\u5bf9\u4e8e\u7b80\u5355\u7684\u4efb\u52a1\uff0c\u53ef\u80fd\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u4f7f\u7528math.prod\u51fd\u6570\uff08Python 3.8\u53ca\u4ee5\u4e0a\u7248\u672c\uff09<\/h2>\n<\/p>\n<p><h3>\u57fa\u672c\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p>Python 3.8\u5f15\u5165\u4e86<code>math.prod<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u5e8f\u5217\u7684\u4e58\u79ef\uff0c\u8fd9\u662f\u8ba1\u7b97\u6570\u7ec4\u4e58\u79ef\u7684\u6700\u7b80\u5355\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>arr = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528math.prod\u51fd\u6570\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef<\/strong><\/h2>\n<p>product = math.prod(arr)<\/p>\n<p>print(&quot;\u6570\u7ec4\u7684\u4e58\u79ef\u662f:&quot;, product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01\uff0c\u6027\u80fd\u9ad8\u6548\u3002<\/li>\n<li>\u5185\u7f6e\u51fd\u6570\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u5e93\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ec5\u9002\u7528\u4e8ePython 3.8\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<\/ul>\n<p><h2>\u4e94\u3001\u6bd4\u8f83\u4e0e\u603b\u7ed3<\/h2>\n<\/p>\n<p><h3>\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u5bf9\u4e8e\u5c0f\u6570\u7ec4\uff0c\u5dee\u5f02\u53ef\u80fd\u4e0d\u660e\u663e\uff0c\u4f46\u5bf9\u4e8e\u5927\u6570\u7ec4\uff0c\u6027\u80fd\u5dee\u5f02\u4f1a\u8f83\u4e3a\u663e\u8457\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u6570\u7ec4\u5927\u5c0f\u4e0b\u7684\u6027\u80fd\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import numpy as np<\/p>\n<p>from functools import reduce<\/p>\n<p>import math<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5927\u6570\u7ec4<\/strong><\/h2>\n<p>arr = list(range(1, 100000))<\/p>\n<h2><strong>\u6d4b\u8bd5\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>product = 1<\/p>\n<p>for num in arr:<\/p>\n<p>    product *= num<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<h2><strong>\u6d4b\u8bd5reduce\u51fd\u6570\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>product = reduce(lambda x, y: x * y, arr)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;reduce\u51fd\u6570\u7684\u65b9\u6cd5\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<h2><strong>\u6d4b\u8bd5numpy\u5e93\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>product = np.prod(arr)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;numpy\u5e93\u7684\u65b9\u6cd5\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<h2><strong>\u6d4b\u8bd5math.prod\u51fd\u6570\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>product = math.prod(arr)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;math.prod\u51fd\u6570\u7684\u65b9\u6cd5\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u6bd4\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u548c\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5<\/strong>\uff1a\u9002\u5408\u5c0f\u6570\u7ec4\u548c\u521d\u5b66\u8005\uff0c\u4e0d\u4f9d\u8d56\u5916\u90e8\u5e93\uff0c\u4f46\u5bf9\u4e8e\u5927\u6570\u7ec4\u6027\u80fd\u8f83\u5dee\u3002<\/li>\n<li><strong>reduce\u51fd\u6570\u7684\u65b9\u6cd5<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u5408\u4e2d\u5c0f\u89c4\u6a21\u6570\u7ec4\uff0c\u4f46\u9700\u8981\u5bfc\u5165functools\u6a21\u5757\u3002<\/li>\n<li><strong>numpy\u5e93\u7684\u65b9\u6cd5<\/strong>\uff1a\u6027\u80fd\u9ad8\u6548\uff0c\u9002\u5408\u5927\u6570\u636e\u5904\u7406\uff0c\u4f46\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165numpy\u5e93\u3002<\/li>\n<li><strong>math.prod\u51fd\u6570\u7684\u65b9\u6cd5<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u6027\u80fd\u9ad8\u6548\uff0c\u9002\u5408\u6240\u6709\u6570\u7ec4\uff0c\u4f46\u4ec5\u9002\u7528\u4e8ePython 3.8\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<\/ul>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u73af\u5883\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef\u3002\u5bf9\u4e8e\u4e00\u822c\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528<code>math.prod<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u7ed3\u5408\u4e86\u7b80\u6d01\u6027\u548c\u9ad8\u6548\u6027\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u6570\u636e\u7684\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528<code>numpy<\/code>\u5e93\u3002\u5bf9\u4e8e\u9700\u8981\u517c\u5bb9\u6027\u548c\u7b80\u5355\u6027\u7684\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u7684\u4e58\u79ef\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5<code>numpy<\/code>\u5e93\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install numpy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>numpy.prod()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u4e58\u79ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\narray = [1, 2, 3, 4]\nproduct = np.prod(array)\nprint(product)  # \u8f93\u51fa 24\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u6211\u4e0d\u60f3\u4f7f\u7528\u5916\u90e8\u5e93\uff0c\u5982\u4f55\u624b\u52a8\u8ba1\u7b97\u6570\u7ec4\u7684\u4e58\u79ef\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5faa\u73af\u6216\u8005\u4f7f\u7528<code>functools.reduce()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u624b\u52a8\u8ba1\u7b97\u4e58\u79ef\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from functools import reduce\n\narray = [1, 2, 3, 4]\nproduct = reduce(lambda x, y: x * y, array)\nprint(product)  # \u8f93\u51fa 24\n<\/code><\/pre>\n<p><strong>\u5728\u8ba1\u7b97\u4e58\u79ef\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5305\u542b\u96f6\u7684\u6570\u7ec4\uff1f<\/strong><br \/>\u5f53\u6570\u7ec4\u4e2d\u5305\u542b\u96f6\u65f6\uff0c\u4e58\u79ef\u5c06\u4f1a\u662f\u96f6\u3002\u8fd9\u662f\u56e0\u4e3a\u4efb\u4f55\u6570\u4e0e\u96f6\u76f8\u4e58\u7ed3\u679c\u5747\u4e3a\u96f6\u3002\u5982\u679c\u9700\u8981\u68c0\u67e5\u6570\u7ec4\u4e2d\u662f\u5426\u5305\u542b\u96f6\uff0c\u53ef\u4ee5\u5728\u8ba1\u7b97\u524d\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">array = [1, 2, 0, 4]\nif 0 in array:\n    product = 0\nelse:\n    product = np.prod(array)\n\nprint(product)  # \u8f93\u51fa 0\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8ba1\u7b97\u6570\u7ec4\u4e2d\u4e58\u79ef\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\u3001\u4f7f\u7528reduce\u51fd\u6570\u3001\u4f7f\u7528numpy\u5e93\u7b49\u3002\u8fd9\u91cc [&hellip;]","protected":false},"author":3,"featured_media":1182723,"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\/1182713"}],"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=1182713"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182713\/revisions"}],"predecessor-version":[{"id":1182725,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182713\/revisions\/1182725"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182723"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}