{"id":1090567,"date":"2025-01-08T14:03:46","date_gmt":"2025-01-08T06:03:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1090567.html"},"modified":"2025-01-08T14:03:48","modified_gmt":"2025-01-08T06:03:48","slug":"python%e5%a6%82%e4%bd%95%e8%b7%b3%e5%87%ba%e4%b8%a4%e4%b8%aafor%e5%be%aa%e7%8e%af-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1090567.html","title":{"rendered":"python\u5982\u4f55\u8df3\u51fa\u4e24\u4e2afor\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203153\/4ffa4c3b-9f6d-4202-b0bb-a6b2778163b3.webp\" alt=\"python\u5982\u4f55\u8df3\u51fa\u4e24\u4e2afor\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8df3\u51fa\u4e24\u4e2afor\u5faa\u73af\u6709\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u3001\u51fd\u6570\u8fd4\u56de\u3001\u5f02\u5e38\u5904\u7406\u7b49\u65b9\u5f0f\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\u53d8\u91cf\u6765\u63a7\u5236\u5916\u5c42\u5faa\u73af\u7684\u7ec8\u6b62\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u89e3\u91ca\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5e76\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u8df3\u51fa\u5d4c\u5957\u7684for\u5faa\u73af\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u5e03\u5c14\u53d8\u91cf\u6765\u6807\u8bb0\u662f\u5426\u9700\u8981\u9000\u51fa\u5916\u5c42\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>found = False<\/p>\n<p>for i in range(10):<\/p>\n<p>    for j in range(10):<\/p>\n<p>        if some_condition(i, j):<\/p>\n<p>            found = True<\/p>\n<p>            break<\/p>\n<p>    if found:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a<code>found<\/code>\u53d8\u91cf\u4f5c\u4e3a\u6807\u5fd7\u3002\u5f53\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\uff0c\u8bbe\u7f6e<code>found<\/code>\u4e3aTrue\uff0c\u5e76\u5728\u5185\u5c42\u5faa\u73af\u4e2d\u4f7f\u7528<code>break<\/code>\u9000\u51fa\u3002\u7136\u540e\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u68c0\u67e5<code>found<\/code>\u53d8\u91cf\uff0c\u5982\u679c\u4e3aTrue\uff0c\u518d\u6b21\u4f7f\u7528<code>break<\/code>\u9000\u51fa\u5916\u5c42\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u51fd\u6570\u8fd4\u56de<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u5d4c\u5957\u7684for\u5faa\u73af\u653e\u5165\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u5e76\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u9000\u51fa\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def nested_loops():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        for j in range(10):<\/p>\n<p>            if some_condition(i, j):<\/p>\n<p>                return i, j<\/p>\n<p>    return None<\/p>\n<p>result = nested_loops()<\/p>\n<p>if result:<\/p>\n<p>    print(f&quot;Condition met at: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c<code>return<\/code>\u8bed\u53e5\u4f1a\u7acb\u5373\u9000\u51fa\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u9700\u8981\u7684\u7ed3\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u590d\u6742\u7684\u6807\u5fd7\u53d8\u91cf\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u4e5f\u662f\u4e00\u79cd\u8df3\u51fa\u5d4c\u5957\u5faa\u73af\u7684\u6709\u6548\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u5e76\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u4ece\u800c\u8df3\u51fa\u5d4c\u5957\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class BreakLoop(Exception):<\/p>\n<p>    pass<\/p>\n<p>try:<\/p>\n<p>    for i in range(10):<\/p>\n<p>        for j in range(10):<\/p>\n<p>            if some_condition(i, j):<\/p>\n<p>                r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se BreakLoop<\/p>\n<p>except BreakLoop:<\/p>\n<p>    print(f&quot;Loop exited at: ({i}, {j})&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38<code>BreakLoop<\/code>\uff0c\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u5e76\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u6355\u83b7\u8be5\u5f02\u5e38\uff0c\u4ece\u800c\u8df3\u51fa\u5d4c\u5957\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 itertools.product \u7ed3\u5408\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u904d\u5386\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u7b1b\u5361\u5c14\u79ef\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools.product<\/code>\u7ed3\u5408\u5185\u7f6e\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>for i, j in itertools.product(range(10), repeat=2):<\/p>\n<p>    if some_condition(i, j):<\/p>\n<p>        print(f&quot;Condition met at: ({i}, {j})&quot;)<\/p>\n<p>        break<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Condition not met&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>itertools.product<\/code>\u751f\u6210\u4e86\u6240\u6709\u53ef\u80fd\u7684(i, j)\u7ec4\u5408\u3002\u5185\u5c42\u5faa\u73af\u9000\u51fa\u65f6\uff0c<code>else<\/code>\u5b50\u53e5\u4f1a\u5728\u5faa\u73af\u6ca1\u6709\u88ab<code>break<\/code>\u8bed\u53e5\u4e2d\u65ad\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 while \u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u79cd\u65b9\u6cd5\u662f\u7528<code>while<\/code>\u5faa\u73af\u4ee3\u66ff<code>for<\/code>\u5faa\u73af\uff0c\u901a\u8fc7\u624b\u52a8\u63a7\u5236\u5faa\u73af\u53d8\u91cf\u6765\u5b9e\u73b0\u8df3\u51fa\u5d4c\u5957\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">i = 0<\/p>\n<p>while i &lt; 10:<\/p>\n<p>    j = 0<\/p>\n<p>    while j &lt; 10:<\/p>\n<p>        if some_condition(i, j):<\/p>\n<p>            break<\/p>\n<p>        j += 1<\/p>\n<p>    if j &lt; 10:  # Condition met, break the outer loop<\/p>\n<p>        break<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u624b\u52a8\u63a7\u5236\u5faa\u73af\u53d8\u91cf<code>i<\/code>\u548c<code>j<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u8df3\u51fa\u5d4c\u5957\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8df3\u51fa\u4e24\u4e2afor\u5faa\u73af\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u3001\u51fd\u6570\u8fd4\u56de\u3001\u5f02\u5e38\u5904\u7406\u3001itertools.product\u7ed3\u5408\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528while\u5faa\u73af<\/strong>\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u4ee3\u7801\u7684\u590d\u6742\u5ea6\u3002\u901a\u5e38\uff0c\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u662f\u6700\u7b80\u5355\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u800c\u5176\u4ed6\u65b9\u6cd5\u5219\u9002\u7528\u4e8e\u66f4\u590d\u6742\u6216\u7279\u5b9a\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u548c\u793a\u4f8b\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u597d\u5730\u5904\u7406\u5d4c\u5957\u5faa\u73af\u7684\u9000\u51fa\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u9000\u51fa\u591a\u4e2a\u5d4c\u5957\u7684for\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u8df3\u51fa\u591a\u4e2a\u5d4c\u5957\u7684for\u5faa\u73af\u3002\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u5e76\u5728\u5185\u5c42\u5faa\u73af\u4e2d\u5f15\u53d1\u5b83\uff0c\u7136\u540e\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u3002\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u4ece\u591a\u4e2a\u5c42\u6b21\u7684\u5faa\u73af\u4e2d\u76f4\u63a5\u9000\u51fa\uff0c\u4fdd\u6301\u4ee3\u7801\u7684\u6574\u6d01\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u5f02\u5e38\u6765\u8df3\u51fa\u591a\u4e2a\u5faa\u73af\uff1f<\/strong><br \/>\u5f53\u7136\uff0c\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u3002\u60a8\u53ef\u4ee5\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5e03\u5c14\u53d8\u91cf\uff0c\u5f53\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\uff0c\u5c06\u5176\u8bbe\u7f6e\u4e3aTrue\uff0c\u7136\u540e\u5728\u5185\u5c42\u5faa\u73af\u4e2d\u68c0\u67e5\u8fd9\u4e2a\u53d8\u91cf\u3002\u5982\u679c\u53d8\u91cf\u4e3aTrue\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528break\u8bed\u53e5\u6765\u9000\u51fa\u5faa\u73af\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u903b\u8f91\u4e0a\u6bd4\u8f83\u7b80\u5355\uff0c\u9002\u5408\u4e0d\u60f3\u4f7f\u7528\u5f02\u5e38\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u4f55\u65f6\u9700\u8981\u8df3\u51fa\u591a\u4e2afor\u5faa\u73af\uff1f<\/strong><br \/>\u5728\u6570\u636e\u5904\u7406\u3001\u641c\u7d22\u7b97\u6cd5\u6216\u9700\u8981\u627e\u5230\u7279\u5b9a\u6761\u4ef6\u4e0b\u7684\u7ed3\u679c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u9700\u8981\u8df3\u51fa\u591a\u4e2a\u5faa\u73af\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u60a8\u5728\u5904\u7406\u5d4c\u5957\u5217\u8868\u6216\u591a\u7ef4\u6570\u7ec4\u65f6\uff0c\u5982\u679c\u5728\u5185\u5c42\u5faa\u73af\u4e2d\u627e\u5230\u4e86\u76ee\u6807\u5143\u7d20\uff0c\u53ef\u80fd\u5e0c\u671b\u7acb\u5373\u7ec8\u6b62\u6240\u6709\u5faa\u73af\u3002\u8fd9\u79cd\u573a\u666f\u4e0b\uff0c\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6216\u6807\u5fd7\u53d8\u91cf\u7684\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8df3\u51fa\u4e24\u4e2afor\u5faa\u73af\u6709\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u3001\u51fd\u6570\u8fd4\u56de\u3001\u5f02\u5e38\u5904\u7406\u7b49\u65b9\u5f0f\u3002 \u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1090574,"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\/1090567"}],"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=1090567"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090567\/revisions"}],"predecessor-version":[{"id":1090575,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090567\/revisions\/1090575"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1090574"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1090567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1090567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1090567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}