{"id":1070524,"date":"2025-01-08T10:59:00","date_gmt":"2025-01-08T02:59:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1070524.html"},"modified":"2025-01-08T10:59:03","modified_gmt":"2025-01-08T02:59:03","slug":"%e4%b8%a4%e4%b8%aafor%e5%be%aa%e7%8e%af%e5%a6%82%e4%bd%95%e6%89%a7%e8%a1%8cpython-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1070524.html","title":{"rendered":"\u4e24\u4e2afor\u5faa\u73af\u5982\u4f55\u6267\u884cpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101219\/b335801b-6f68-43a0-ad39-e240889ab2ed.webp\" alt=\"\u4e24\u4e2afor\u5faa\u73af\u5982\u4f55\u6267\u884cpython\" \/><\/p>\n<p><p> <strong>\u4e24\u4e2afor\u5faa\u73af\u5728Python\u4e2d\u7684\u6267\u884c\u65b9\u5f0f\u5305\u62ec\u5d4c\u5957\u5faa\u73af\u548c\u5e73\u884c\u5faa\u73af<\/strong>\uff0c\u5176\u4e2d\u5d4c\u5957\u5faa\u73af\u7528\u4e8e\u904d\u5386\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u77e9\u9635\u6216\u4e8c\u7ef4\u5217\u8868\uff0c\u800c\u5e73\u884c\u5faa\u73af\u7528\u4e8e\u72ec\u7acb\u5730\u904d\u5386\u4e24\u4e2a\u6216\u591a\u4e2a\u5e8f\u5217\u3002\u5d4c\u5957\u5faa\u73af\u53ef\u4ee5\u7528\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e73\u884c\u5faa\u73af\u5219\u9002\u5408\u5904\u7406\u72ec\u7acb\u7684\u5e8f\u5217\u6216\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5185\u5c42\u5faa\u73af\u5728\u6bcf\u6b21\u5916\u5c42\u5faa\u73af\u8fd0\u884c\u65f6\u90fd\u4f1a\u5b8c\u5168\u8fd0\u884c\u4e00\u6b21\u3002\u8fd9\u6837\u53ef\u4ee5\u904d\u5386\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5217\u8868\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u9010\u4e00\u8bbf\u95ee\u6bcf\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\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>for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u5c42<code>for<\/code>\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\uff0c\u800c\u5185\u5c42<code>for<\/code>\u5faa\u73af\u904d\u5386\u8be5\u884c\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u8be6\u7ec6\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5d4c\u5957\u5faa\u73af\u7684\u6267\u884c<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u7684\u6267\u884c\u662f\u9010\u5c42\u8fdb\u884c\u7684\uff0c\u9996\u5148\u5916\u5c42\u5faa\u73af\u83b7\u53d6\u4e00\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u5185\u5c42\u5faa\u73af\u5bf9\u8be5\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002\u5916\u5c42\u5faa\u73af\u7ed3\u675f\u540e\uff0c\u5916\u5c42\u5faa\u73af\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u88ab\u83b7\u53d6\uff0c\u5185\u5c42\u5faa\u73af\u518d\u6b21\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5916\u5c42\u5faa\u73af\u7684\u4f5c\u7528<\/h4>\n<\/p>\n<p><p>\u5916\u5c42\u5faa\u73af\u7684\u4f5c\u7528\u662f\u904d\u5386\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u4e8c\u7ef4\u5217\u8868\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u8d1f\u8d23\u83b7\u53d6\u6bcf\u4e00\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = [<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4],<\/p>\n<p>    [5, 6]<\/p>\n<p>]<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>[1, 2]<\/p>\n<p>[3, 4]<\/p>\n<p>[5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5185\u5c42\u5faa\u73af\u7684\u4f5c\u7528<\/h4>\n<\/p>\n<p><p>\u5185\u5c42\u5faa\u73af\u7684\u4f5c\u7528\u662f\u5bf9\u6bcf\u4e00\u884c\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002\u5728\u5916\u5c42\u5faa\u73af\u83b7\u53d6\u5230\u4e00\u884c\u4e4b\u540e\uff0c\u5185\u5c42\u5faa\u73af\u904d\u5386\u8be5\u884c\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in rows:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>1<\/p>\n<p>2<\/p>\n<p>3<\/p>\n<p>4<\/p>\n<p>5<\/p>\n<p>6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u6211\u4eec\u904d\u5386\u4e8c\u7ef4\u7ed3\u6784\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5e73\u884c\u5faa\u73af\u7684\u6267\u884c<\/h3>\n<\/p>\n<p><p>\u5e73\u884c\u5faa\u73af\u662f\u6307\u540c\u65f6\u904d\u5386\u591a\u4e2a\u72ec\u7acb\u7684\u5e8f\u5217\u3002\u53ef\u4ee5\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u5c06\u591a\u4e2a\u5e8f\u5217\u6253\u5305\u5230\u4e00\u8d77\uff0c\u7136\u540e\u8fdb\u884c\u904d\u5386\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for num, char in zip(list1, list2):<\/p>\n<p>    print(num, char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>zip<\/code>\u51fd\u6570\u5c06<code>list1<\/code>\u548c<code>list2<\/code>\u4e2d\u7684\u5143\u7d20\u914d\u5bf9\uff0c<code>for<\/code>\u5faa\u73af\u540c\u65f6\u904d\u5386\u8fd9\u4e24\u4e2a\u5217\u8868\u3002\u8be6\u7ec6\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>zip<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>zip<\/code>\u51fd\u6570\u5c06\u591a\u4e2a\u5e8f\u5217\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>zipped = zip(list1, list2)<\/p>\n<p>print(list(zipped))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>[(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u904d\u5386\u6253\u5305\u540e\u7684\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u4e00\u65e6\u5e8f\u5217\u88ab\u6253\u5305\u6210\u5143\u7ec4\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6765\u904d\u5386\u8fd9\u4e9b\u5143\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for num, char in zip(list1, list2):<\/p>\n<p>    print(num, char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>1 a<\/p>\n<p>2 b<\/p>\n<p>3 c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5d4c\u5957\u5faa\u73af\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u5728\u5904\u7406\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u77e9\u9635\u3001\u4e8c\u7ef4\u5217\u8868\u6216\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u77e9\u9635\u7684\u904d\u5386<\/h4>\n<\/p>\n<p><p>\u904d\u5386\u77e9\u9635\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\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>for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u67e5\u627e\u7279\u5b9a\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u67e5\u627e\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">target = 5<\/p>\n<p>found = False<\/p>\n<p>for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        if element == target:<\/p>\n<p>            found = True<\/p>\n<p>            break<\/p>\n<p>    if found:<\/p>\n<p>        break<\/p>\n<p>if found:<\/p>\n<p>    print(f&quot;Found {target}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{target} not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e73\u884c\u5faa\u73af\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5e73\u884c\u5faa\u73af\u5728\u5904\u7406\u591a\u4e2a\u72ec\u7acb\u5e8f\u5217\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u540c\u65f6\u904d\u5386\u4e24\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5408\u5e76\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5c06\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u914d\u5bf9\u5e76\u6253\u5370\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for num, char in zip(list1, list2):<\/p>\n<p>    print(f&quot;{num}-{char}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u4e24\u4e2a\u5217\u8868\u5bf9\u5e94\u5143\u7d20\u7684\u548c<\/h4>\n<\/p>\n<p><p>\u8ba1\u7b97\u4e24\u4e2a\u5217\u8868\u4e2d\u5bf9\u5e94\u5143\u7d20\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>sums = [a + b for a, b in zip(list1, list2)]<\/p>\n<p>print(sums)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>[5, 7, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u590d\u6742\u5e94\u7528\u573a\u666f\u4e2d\u7684\u5d4c\u5957\u5faa\u73af\u548c\u5e73\u884c\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5d4c\u5957\u5faa\u73af\u548c\u5e73\u884c\u5faa\u73af\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u8868\u683c\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u8868\u683c\u90fd\u662f\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u8868\u683c\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528\u5e73\u884c\u5faa\u73af\u5bf9\u6bcf\u4e2a\u8868\u683c\u8fdb\u884c\u6e05\u6d17\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tables = [<\/p>\n<p>    [<\/p>\n<p>        [1, 2, 3],<\/p>\n<p>        [4, 5, 6],<\/p>\n<p>        [7, 8, 9]<\/p>\n<p>    ],<\/p>\n<p>    [<\/p>\n<p>        [10, 11, 12],<\/p>\n<p>        [13, 14, 15],<\/p>\n<p>        [16, 17, 18]<\/p>\n<p>    ]<\/p>\n<p>]<\/p>\n<p>cleaned_tables = []<\/p>\n<p>for table in tables:<\/p>\n<p>    cleaned_table = []<\/p>\n<p>    for row in table:<\/p>\n<p>        cleaned_row = [element if element % 2 == 0 else 0 for element in row]<\/p>\n<p>        cleaned_table.append(cleaned_row)<\/p>\n<p>    cleaned_tables.append(cleaned_table)<\/p>\n<p>for table in cleaned_tables:<\/p>\n<p>    for row in table:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u5c06\u6240\u6709\u5947\u6570\u5143\u7d20\u66ff\u6362\u4e3a0\uff0c\u5e76\u8f93\u51fa\u6e05\u6d17\u540e\u7684\u8868\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code>[0, 2, 0]<\/p>\n<p>[4, 0, 6]<\/p>\n<p>[0, 8, 0]<\/p>\n<p>[10, 0, 12]<\/p>\n<p>[0, 14, 0]<\/p>\n<p>[16, 0, 18]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u805a\u5408<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u591a\u4e2a\u4f20\u611f\u5668\u7684\u6570\u636e\uff0c\u6bcf\u4e2a\u4f20\u611f\u5668\u7684\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5e73\u884c\u5faa\u73af\u5bf9\u8fd9\u4e9b\u6570\u636e\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sensor1 = [1, 2, 3]<\/p>\n<p>sensor2 = [4, 5, 6]<\/p>\n<p>sensor3 = [7, 8, 9]<\/p>\n<p>aggregated_data = []<\/p>\n<p>for data1, data2, data3 in zip(sensor1, sensor2, sensor3):<\/p>\n<p>    aggregated_data.append(data1 + data2 + data3)<\/p>\n<p>print(aggregated_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>[12, 15, 18]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u4f18\u5316\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u548c\u5e73\u884c\u5faa\u73af\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6027\u80fd\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5efa\u8bae\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u51cf\u5c11\u5d4c\u5957\u5c42\u6570<\/h4>\n<\/p>\n<p><p>\u5c3d\u91cf\u51cf\u5c11\u5d4c\u5957\u5c42\u6570\uff0c\u56e0\u4e3a\u5d4c\u5957\u5c42\u6570\u8d8a\u591a\uff0c\u6027\u80fd\u5f00\u9500\u8d8a\u5927\u3002\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u8003\u8651\u5c06\u5d4c\u5957\u5faa\u73af\u4f18\u5316\u4e3a\u5e73\u884c\u5faa\u73af\u6216\u5176\u4ed6\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u800c\u4e0d\u662f\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u5f00\u9500\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">large_list = range(1000000)<\/p>\n<p>squares = (x  2 for x in large_list)<\/p>\n<p>for square in squares:<\/p>\n<p>    pass  # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5185\u5c42\u5faa\u73af\u7684\u67d0\u4e9b\u6761\u4ef6\u5df2\u7ecf\u6ee1\u8db3\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u63d0\u524d\u9000\u51fa\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        if element == target:<\/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>\u901a\u8fc7\u8fd9\u4e9b\u4f18\u5316\u5efa\u8bae\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5d4c\u5957\u5faa\u73af\u548c\u5e73\u884c\u5faa\u73af\u7684\u6027\u80fd\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u9ad8\u6548\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e24\u4e2afor\u5faa\u73af\u7684\u6267\u884c\u65b9\u5f0f\u5305\u62ec\u5d4c\u5957\u5faa\u73af\u548c\u5e73\u884c\u5faa\u73af\u3002\u5d4c\u5957\u5faa\u73af\u7528\u4e8e\u904d\u5386\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u800c\u5e73\u884c\u5faa\u73af\u7528\u4e8e\u540c\u65f6\u904d\u5386\u591a\u4e2a\u72ec\u7acb\u5e8f\u5217\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u548c\u5e73\u884c\u5faa\u73af\uff0c\u53ef\u4ee5\u89e3\u51b3\u5404\u79cd\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u6027\u80fd\u4f18\u5316\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u9ad8\u6548\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5d4c\u5957for\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5d4c\u5957for\u5faa\u73af\u662f\u6307\u5728\u4e00\u4e2afor\u5faa\u73af\u5185\u90e8\u518d\u653e\u7f6e\u4e00\u4e2afor\u5faa\u73af\u3002\u8fd9\u79cd\u7ed3\u6784\u5e38\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5217\u8868\u7684\u5217\u8868\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(3):  # \u5916\u5c42\u5faa\u73af\n    for j in range(2):  # \u5185\u5c42\u5faa\u73af\n        print(f&quot;i: {i}, j: {j}&quot;)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u6253\u5370\u51fa\u6240\u6709<code>i<\/code>\u548c<code>j<\/code>\u7684\u7ec4\u5408\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528for\u5faa\u73af\u65f6\uff0c\u5982\u4f55\u907f\u514d\u51fa\u73b0\u6b7b\u5faa\u73af\uff1f<\/strong><br \/>\u6b7b\u5faa\u73af\u901a\u5e38\u662f\u7531\u4e8e\u5faa\u73af\u6761\u4ef6\u672a\u80fd\u6b63\u786e\u66f4\u65b0\u5bfc\u81f4\u7684\u3002\u786e\u4fdd\u5728\u5faa\u73af\u4e2d\u6709\u66f4\u65b0\u6761\u4ef6\u7684\u8bed\u53e5\uff0c\u4f8b\u5982\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6216\u5728\u5faa\u73af\u4f53\u5185\u66f4\u65b0\u53d8\u91cf\u3002\u4f7f\u7528<code>range()<\/code>\u51fd\u6570\u53ef\u4ee5\u6709\u6548\u63a7\u5236\u5faa\u73af\u6b21\u6570\uff0c\u4ece\u800c\u907f\u514d\u610f\u5916\u7684\u6b7b\u5faa\u73af\u73b0\u8c61\u3002<\/p>\n<p><strong>Python\u4e2d\u7684for\u5faa\u73af\u4e0ewhile\u5faa\u73af\u6709\u4f55\u4e0d\u540c\uff1f<\/strong><br \/>for\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\uff0c\u9002\u5408\u4e8e\u5df2\u77e5\u6b21\u6570\u7684\u5faa\u73af\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0cwhile\u5faa\u73af\u5219\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\uff0c\u9002\u7528\u4e8e\u4e0d\u786e\u5b9a\u5faa\u73af\u6b21\u6570\u7684\u60c5\u51b5\u3002\u9009\u62e9\u54ea\u79cd\u5faa\u73af\u7ed3\u6784\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e24\u4e2afor\u5faa\u73af\u5728Python\u4e2d\u7684\u6267\u884c\u65b9\u5f0f\u5305\u62ec\u5d4c\u5957\u5faa\u73af\u548c\u5e73\u884c\u5faa\u73af\uff0c\u5176\u4e2d\u5d4c\u5957\u5faa\u73af\u7528\u4e8e\u904d\u5386\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u77e9\u9635\u6216\u4e8c [&hellip;]","protected":false},"author":3,"featured_media":1070529,"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\/1070524"}],"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=1070524"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070524\/revisions"}],"predecessor-version":[{"id":1070531,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070524\/revisions\/1070531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1070529"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1070524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1070524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1070524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}