{"id":1131556,"date":"2025-01-08T20:49:31","date_gmt":"2025-01-08T12:49:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1131556.html"},"modified":"2025-01-08T20:49:31","modified_gmt":"2025-01-08T12:49:31","slug":"python%e5%a6%82%e4%bd%95%e8%bf%9e%e7%bb%ad%e5%87%8f%e5%8e%bb%e4%b8%a4%e4%b8%aa%e4%b8%8d%e5%90%8c%e7%9a%84%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1131556.html","title":{"rendered":"python\u5982\u4f55\u8fde\u7eed\u51cf\u53bb\u4e24\u4e2a\u4e0d\u540c\u7684\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101301\/d4122e95-a74b-4690-8f0e-bd0b90110121.webp\" alt=\"python\u5982\u4f55\u8fde\u7eed\u51cf\u53bb\u4e24\u4e2a\u4e0d\u540c\u7684\u6570\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684\u8fde\u7eed\u51cf\u6cd5\u64cd\u4f5c\u4e3b\u8981\u6d89\u53ca\u4e24\u79cd\u60c5\u51b5\uff1a<\/strong>\u901a\u8fc7for\u5faa\u73af\u5b9e\u73b0\u8fde\u7eed\u51cf\u6cd5\u3001\u4f7f\u7528reduce\u51fd\u6570\u8fdb\u884c\u8fde\u7eed\u51cf\u6cd5\u3002<strong>\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u5173\u952e\u662f\u8981\u7406\u89e3\u5982\u4f55\u4ece\u5217\u8868\u4e2d\u51cf\u53bb\u4e0d\u540c\u7684\u6570\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7for\u5faa\u73af\u5b9e\u73b0\u8fde\u7eed\u51cf\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u8fdb\u884c\u8fde\u7eed\u51cf\u6cd5\u64cd\u4f5c\u662f\u6700\u76f4\u63a5\u548c\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9010\u6b65\u4ece\u4e00\u4e2a\u521d\u59cb\u503c\u4e2d\u51cf\u53bb\u4e0d\u540c\u7684\u6570\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u521d\u59cb\u5316\u53d8\u91cf\u548c\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf\u4f5c\u4e3a\u5f00\u59cb\u7684\u6570\u503c\uff0c\u5e76\u51c6\u5907\u4e00\u4e2a\u5305\u542b\u8981\u51cf\u53bb\u7684\u6570\u503c\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">start_value = 100<\/p>\n<p>numbers_to_subtract = [10, 5, 20, 15]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528for\u5faa\u73af\u8fdb\u884c\u51cf\u6cd5\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u503c\uff0c\u5e76\u4ece\u521d\u59cb\u503c\u4e2d\u51cf\u53bb\u8fd9\u4e9b\u6570\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for number in numbers_to_subtract:<\/p>\n<p>    start_value -= number<\/p>\n<p>print(&quot;\u6700\u7ec8\u7ed3\u679c:&quot;, start_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>start_value<\/code>\u5c06\u4f9d\u6b21\u51cf\u53bb10\u30015\u300120\u548c15\u3002\u5faa\u73af\u7ed3\u675f\u540e\uff0c<code>start_value<\/code>\u5c06\u5305\u542b\u6700\u7ec8\u7684\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>1.3 \u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7for\u5faa\u73af\u5b9e\u73b0\u8fde\u7eed\u51cf\u6cd5\u975e\u5e38\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\u3002\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u5faa\u73af\u6bcf\u6b21\u8fed\u4ee3\u65f6\uff0c<code>start_value<\/code>\u90fd\u4f1a\u66f4\u65b0\u4e3a\u51cf\u53bb\u5f53\u524d\u5217\u8868\u5143\u7d20\u540e\u7684\u65b0\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u5355\u660e\u4e86\uff0c\u6613\u4e8e\u8c03\u8bd5\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528reduce\u51fd\u6570\u8fdb\u884c\u8fde\u7eed\u51cf\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684<code>functools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u51fd\u6570<code>reduce<\/code>\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u8fde\u7eed\u5e94\u7528\u67d0\u4e2a\u64cd\u4f5c\u3002<code>reduce<\/code>\u51fd\u6570\u80fd\u591f\u7b80\u5316\u4ee3\u7801\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u8fde\u7eed\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5bfc\u5165functools\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165<code>reduce<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u5b9a\u4e49\u51cf\u6cd5\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7528\u4e8e\u51cf\u6cd5\u64cd\u4f5c\u7684lambda\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">subtract = lambda x, y: x - y<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u4f7f\u7528reduce\u51fd\u6570\u8fdb\u884c\u51cf\u6cd5\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528<code>reduce<\/code>\u51fd\u6570\u5c06\u51cf\u6cd5\u64cd\u4f5c\u5e94\u7528\u4e8e\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">start_value = 100<\/p>\n<p>numbers_to_subtract = [10, 5, 20, 15]<\/p>\n<p>result = reduce(subtract, numbers_to_subtract, start_value)<\/p>\n<p>print(&quot;\u6700\u7ec8\u7ed3\u679c:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>reduce<\/code>\u51fd\u6570\u5c06\u4ece<code>start_value<\/code>\u5f00\u59cb\uff0c\u4f9d\u6b21\u5e94\u7528<code>subtract<\/code>\u51fd\u6570\uff0c\u51cf\u53bb\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u6700\u7ec8\u8fd4\u56de\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2.4 \u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>reduce<\/code>\u51fd\u6570\u80fd\u591f\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u4f18\u96c5\u3002<code>reduce<\/code>\u51fd\u6570\u4f1a\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u4e0e\u5f53\u524d\u7d2f\u79ef\u7684\u7ed3\u679c\u5e94\u7528\u4e8e\u63d0\u4f9b\u7684\u64cd\u4f5c\u51fd\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u4f46\u5bf9\u4e8e\u4e0d\u719f\u6089\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u53ef\u80fd\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u901a\u8fc7for\u5faa\u73af\u8fd8\u662f\u4f7f\u7528<code>reduce<\/code>\u51fd\u6570\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fde\u7eed\u51cf\u6cd5\u3002<strong>\u901a\u8fc7for\u5faa\u73af\u5b9e\u73b0\u8fde\u7eed\u51cf\u6cd5\u7684\u4ee3\u7801\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002<\/strong>\u800c<strong>\u4f7f\u7528<code>reduce<\/code>\u51fd\u6570\u5219\u80fd\u591f\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u4f18\u96c5\uff0c\u4f46\u9700\u8981\u5bf9\u51fd\u6570\u5f0f\u7f16\u7a0b\u6709\u4e00\u5b9a\u7684\u7406\u89e3\u3002<\/strong>\u6839\u636e\u5177\u4f53\u60c5\u51b5\u548c\u4e2a\u4eba\u504f\u597d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fde\u7eed\u51cf\u6cd5\u64cd\u4f5c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7406\u89e3\u5e76\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u8fde\u7eed\u51cf\u53bb\u4e24\u4e2a\u4e0d\u540c\u7684\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5faa\u73af\u6216\u9012\u5f52\u51fd\u6570\u5b9e\u73b0\u8fde\u7eed\u51cf\u53bb\u4e24\u4e2a\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528while\u5faa\u73af\u53ef\u4ee5\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u51cf\u53bb\u8fd9\u4e24\u4e2a\u6570\uff0c\u76f4\u5230\u5176\u4e2d\u4e00\u4e2a\u6570\u53d8\u4e3a\u96f6\u6216\u8d1f\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">a = 10\nb = 3\nwhile a &gt; 0:\n    a -= b\n    print(a)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u5c06\u4ece10\u5f00\u59cb\uff0c\u6bcf\u6b21\u51cf\u53bb3\uff0c\u76f4\u5230a\u7684\u503c\u4e3a\u96f6\u6216\u66f4\u5c0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u8fde\u7eed\u51cf\u53bb\u4e24\u4e2a\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u5faa\u73af\uff0c\u8fd8\u6709\u5176\u4ed6\u51e0\u79cd\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684map\u548clambda\u8868\u8fbe\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u5217\uff0c\u5176\u4e2d\u5305\u542b\u8fde\u7eed\u51cf\u53bb\u540e\u7684\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u8868\u8fbe\u903b\u8f91\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8fde\u7eed\u51cf\u6cd5\u4e2d\u7684\u8d1f\u6570\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u8fde\u7eed\u51cf\u6cd5\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8d1f\u6570\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u6761\u4ef6\u5224\u65ad\uff0c\u786e\u4fdd\u5728\u51cf\u6cd5\u64cd\u4f5c\u4e4b\u524d\u5224\u65ad\u7ed3\u679c\u662f\u5426\u4f1a\u4f4e\u4e8e\u96f6\u3002\u5982\u679c\u662f\uff0c\u53ef\u4ee5\u9009\u62e9\u505c\u6b62\u51cf\u6cd5\u6216\u8c03\u6574\u51cf\u6cd5\u7684\u6b65\u957f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">a = 10\nb = 3\nwhile a &gt; b:\n    a -= b\n    print(a)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u51cf\u6cd5\u64cd\u4f5c\u5728\u5408\u7406\u8303\u56f4\u5185\u8fdb\u884c\uff0c\u907f\u514d\u51fa\u73b0\u4e0d\u5fc5\u8981\u7684\u8d1f\u6570\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684\u8fde\u7eed\u51cf\u6cd5\u64cd\u4f5c\u4e3b\u8981\u6d89\u53ca\u4e24\u79cd\u60c5\u51b5\uff1a\u901a\u8fc7for\u5faa\u73af\u5b9e\u73b0\u8fde\u7eed\u51cf\u6cd5\u3001\u4f7f\u7528reduce\u51fd\u6570\u8fdb\u884c\u8fde\u7eed\u51cf\u6cd5\u3002\u5728 [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131556"}],"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=1131556"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131556\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1131556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1131556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1131556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}