{"id":940871,"date":"2024-12-26T21:13:36","date_gmt":"2024-12-26T13:13:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940871.html"},"modified":"2024-12-26T21:13:37","modified_gmt":"2024-12-26T13:13:37","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940871.html","title":{"rendered":"python\u5982\u4f55\u7528\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075320\/f61bebc0-35bd-4517-b91f-c920b1562232.webp\" alt=\"python\u5982\u4f55\u7528\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u4e24\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<code>for<\/code>\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u5df2\u77e5\u8303\u56f4\u7684\u5e8f\u5217\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\u6216\u8303\u56f4\u5bf9\u8c61\uff1b\u800c<code>while<\/code>\u5faa\u73af\u5219\u7528\u4e8e\u5728\u67d0\u4e2a\u6761\u4ef6\u4e3a\u771f\u65f6\u53cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u638c\u63e1\u8fd9\u4e24\u79cd\u5faa\u73af\u65b9\u6cd5\u7684\u7528\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>for<\/code>\u5faa\u73af\u66f4\u9002\u5408\u7528\u4e8e\u8fed\u4ee3\u56fa\u5b9a\u6b21\u6570\u7684\u573a\u666f\uff0c\u800c<code>while<\/code>\u5faa\u73af\u9002\u5408\u7528\u4e8e\u9700\u8981\u4e0d\u65ad\u68c0\u67e5\u6761\u4ef6\u7684\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001FOR\u5faa\u73af<\/h3>\n<\/p>\n<p><p><code>for<\/code>\u5faa\u73af\u5728Python\u4e2d\u662f\u4e00\u79cd\u7528\u4e8e\u904d\u5386\u5e8f\u5217\u7684\u5faa\u73af\u7ed3\u6784\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for item in iterable:<\/p>\n<p>    # \u6267\u884c\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u8bed\u6cd5\u7ed3\u6784\u4e2d\uff0c<code>iterable<\/code>\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001\u8303\u56f4\u5bf9\u8c61\u7b49\u3002<code>item<\/code>\u5219\u662f\u6bcf\u6b21\u8fed\u4ee3\u65f6\u4ece<code>iterable<\/code>\u4e2d\u53d6\u51fa\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u904d\u5386\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\u662f\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u4e4b\u4e00\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>for fruit in fruits:<\/p>\n<p>    print(fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5faa\u73af\u4f1a\u4f9d\u6b21\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u904d\u5386\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u4e5f\u662f\u53ef\u8fed\u4ee3\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528<code>for<\/code>\u5faa\u73af\u6765\u904d\u5386\u5176\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">word = &quot;hello&quot;<\/p>\n<p>for char in word:<\/p>\n<p>    print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5faa\u73af\u4f1a\u4f9d\u6b21\u8f93\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1.3\u3001\u4f7f\u7528range()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>range()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u5185\u5efa\u51fd\u6570\uff0c\u5e38\u7528\u4e8e\u751f\u6210\u4e00\u7cfb\u5217\u6570\u5b57\u3002\u5b83\u901a\u5e38\u4e0e<code>for<\/code>\u5faa\u73af\u4e00\u8d77\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5faa\u73af\u5c06\u8f93\u51fa\u4ece0\u52304\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001WHILE\u5faa\u73af<\/h3>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u5728Python\u4e2d\u662f\u4e00\u79cd\u57fa\u4e8e\u6761\u4ef6\u7684\u5faa\u73af\u7ed3\u6784\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while condition:<\/p>\n<p>    # \u6267\u884c\u4ee3\u7801\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u8bed\u6cd5\u7ed3\u6784\u4e2d\uff0c<code>condition<\/code>\u662f\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\u3002\u5f53<code>condition<\/code>\u4e3a<code>True<\/code>\u65f6\uff0c\u5faa\u73af\u5c06\u7ee7\u7eed\u6267\u884c\uff1b\u5f53<code>condition<\/code>\u4e3a<code>False<\/code>\u65f6\uff0c\u5faa\u73af\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u7b80\u5355\u7684while\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684<code>while<\/code>\u5faa\u73af\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5faa\u73af\u5c06\u8f93\u51fa\u4ece0\u52304\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>2.2\u3001\u65e0\u9650\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u5c0f\u5fc3\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u6c38\u8fdc\u4e3a<code>True<\/code>\u7684\u6761\u4ef6\uff0c<code>while<\/code>\u5faa\u73af\u53ef\u80fd\u4f1a\u53d8\u6210\u65e0\u9650\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    print(&quot;This is an infinite loop&quot;)<\/p>\n<p>    # \u4f7f\u7528 break \u8bed\u53e5\u53ef\u4ee5\u8df3\u51fa\u5faa\u73af<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u9000\u51fa\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5faa\u73af\u4e2d\u7684\u63a7\u5236\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5728Python\u5faa\u73af\u4e2d\uff0c\u8fd8\u6709\u4e00\u4e9b\u63a7\u5236\u8bed\u53e5\u53ef\u4ee5\u5f71\u54cd\u5faa\u73af\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u6bd4\u5982<code>break<\/code>\u548c<code>continue<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001break\u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u7acb\u5373\u9000\u51fa\u5faa\u73af\uff0c\u4e0d\u518d\u6267\u884c\u5faa\u73af\u4f53\u4e2d\u7684\u5269\u4f59\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):<\/p>\n<p>    if i == 5:<\/p>\n<p>        break<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5faa\u73af\u5728<code>i<\/code>\u7b49\u4e8e5\u65f6\u4f1a\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h4>3.2\u3001continue\u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>continue<\/code>\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u7684\u5269\u4f59\u4ee3\u7801\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u4e2a\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):<\/p>\n<p>    if i % 2 == 0:<\/p>\n<p>        continue<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5faa\u73af\u53ea\u4f1a\u8f93\u51fa\u5947\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5faa\u73af\u4e2d\u7684else\u5b50\u53e5<\/h3>\n<\/p>\n<p><p>Python\u7684\u5faa\u73af\u7ed3\u6784\u5141\u8bb8\u5728\u5faa\u73af\u7ed3\u675f\u540e\u6267\u884c\u4e00\u4e2a<code>else<\/code>\u5b50\u53e5\uff0c\u8fd9\u5728\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5e76\u4e0d\u5e38\u89c1\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001for\u5faa\u73af\u4e2d\u7684else<\/h4>\n<\/p>\n<p><p>\u5f53<code>for<\/code>\u5faa\u73af\u6b63\u5e38\u7ed3\u675f\uff08\u5373\u6ca1\u6709\u88ab<code>break<\/code>\u4e2d\u9014\u9000\u51fa\uff09\uff0c<code>else<\/code>\u5b50\u53e5\u4f1a\u6267\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Loop ended normally&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001while\u5faa\u73af\u4e2d\u7684else<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c<code>while<\/code>\u5faa\u73af\u6b63\u5e38\u7ed3\u675f\u65f6\uff0c<code>else<\/code>\u5b50\u53e5\u4e5f\u4f1a\u6267\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(count)<\/p>\n<p>    count += 1<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Loop ended normally&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5d4c\u5957\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u5185\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u3002\u5d4c\u5957\u5faa\u73af\u5e38\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u5982\u4e8c\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u5d4c\u5957for\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>for row in matrix:<\/p>\n<p>    for item in row:<\/p>\n<p>        print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5d4c\u5957\u5faa\u73af\u7528\u4e8e\u904d\u5386\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>5.2\u3001\u5d4c\u5957while\u5faa\u73af<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">i = 0<\/p>\n<p>while i &lt; 3:<\/p>\n<p>    j = 0<\/p>\n<p>    while j &lt; 3:<\/p>\n<p>        print(f&quot;i={i}, j={j}&quot;)<\/p>\n<p>        j += 1<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5d4c\u5957<code>while<\/code>\u5faa\u73af\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5750\u6807\u7f51\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5faa\u73af\u4e2d\u7684\u5e38\u89c1\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5faa\u73af\u65f6\uff0c\u6709\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u6ce8\u610f\uff0c\u4ee5\u907f\u514d\u4ea7\u751f\u4e0d\u5fc5\u8981\u7684\u9ebb\u70e6\u3002<\/p>\n<\/p>\n<p><h4>6.1\u3001\u5fd8\u8bb0\u66f4\u65b0\u5faa\u73af\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u65f6\uff0c\u5982\u679c\u5fd8\u8bb0\u66f4\u65b0\u5faa\u73af\u53d8\u91cf\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65e0\u9650\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(count)<\/p>\n<p>    # count += 1  # \u5fd8\u8bb0\u66f4\u65b0\u5faa\u73af\u53d8\u91cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2\u3001\u9519\u8bef\u4f7f\u7528break\u548ccontinue<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>break<\/code>\u548c<code>continue<\/code>\u65f6\uff0c\u8981\u786e\u4fdd\u5b83\u4eec\u7684\u4f7f\u7528\u7b26\u5408\u9884\u671f\u7684\u903b\u8f91\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5faa\u73af\u63d0\u524d\u7ed3\u675f\u6216\u8df3\u8fc7\u91cd\u8981\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5faa\u73af\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5458\u4ee5\u7b80\u6d01\u7684\u65b9\u5f0f\u6267\u884c\u91cd\u590d\u7684\u4efb\u52a1\u3002\u901a\u8fc7\u638c\u63e1<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\uff0c\u4ee5\u53ca<code>break<\/code>\u548c<code>continue<\/code>\u7b49\u63a7\u5236\u8bed\u53e5\uff0c\u80fd\u591f\u7f16\u5199\u51fa\u66f4\u9ad8\u6548\u548c\u7075\u6d3b\u7684\u4ee3\u7801\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u5e76\u6ce8\u610f\u9632\u8303\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5229\u7528Python\u7684\u5faa\u73af\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5faa\u73af\u7ed3\u6784\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5faa\u73af\u7ed3\u6784\u4e3b\u8981\u6709\u4e24\u79cd\uff1a<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u3002<code>for<\/code>\u5faa\u73af\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\uff0c\u800c<code>while<\/code>\u5faa\u73af\u4f1a\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>range()<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u6570\u5b57\u5e8f\u5217\u6765\u63a7\u5236\u5faa\u73af\u6b21\u6570\u3002\u4f8b\u5982\uff0c<code>for i in range(5): print(i)<\/code>\u5c06\u8f93\u51fa0\u52304\u7684\u6570\u5b57\u3002<code>while<\/code>\u5faa\u73af\u7684\u4f7f\u7528\u65b9\u5f0f\u4e3a\uff1a<code>while condition: do_something()<\/code>\uff0c\u5728<code>condition<\/code>\u4e3a\u771f\u65f6\uff0c\u4f1a\u6301\u7eed\u6267\u884c<code>do_something()<\/code>\u3002<\/p>\n<p><strong>Python\u5faa\u73af\u4e2d\u5982\u4f55\u63a7\u5236\u5faa\u73af\u7684\u7ec8\u6b62\uff1f<\/strong><br \/>\u5728Python\u7684\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u548c<code>continue<\/code>\u8bed\u53e5\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002<code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\uff0c\u800c<code>continue<\/code>\u8bed\u53e5\u5219\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u7684\u5269\u4f59\u90e8\u5206\u5e76\u5f00\u59cb\u4e0b\u4e00\u6b21\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u5f53\u5728<code>for<\/code>\u5faa\u73af\u4e2d\u4f7f\u7528<code>break<\/code>\u65f6\uff0c\u5982\u679c\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\uff0c\u5faa\u73af\u4f1a\u7acb\u5373\u505c\u6b62\uff1b\u4f7f\u7528<code>continue<\/code>\u65f6\uff0c\u5f53\u524d\u5faa\u73af\u7684\u540e\u7eed\u4ee3\u7801\u4f1a\u88ab\u8df3\u8fc7\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5d4c\u5957\u5faa\u73af\uff1f<\/strong><br \/>\u5d4c\u5957\u5faa\u73af\u662f\u6307\u5728\u4e00\u4e2a\u5faa\u73af\u5185\u90e8\u518d\u5305\u542b\u4e00\u4e2a\u5faa\u73af\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5728<code>for<\/code>\u6216<code>while<\/code>\u5faa\u73af\u5185\u4f7f\u7528\u53e6\u4e00\u79cd\u5faa\u73af\u3002\u5d4c\u5957\u5faa\u73af\u5e38\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u5982\u4e8c\u7ef4\u6570\u7ec4\u6216\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5d4c\u5957\u7684<code>for<\/code>\u5faa\u73af\u904d\u5386\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nfor row in matrix:\n    for item in row:\n        print(item)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u9010\u4e2a\u8f93\u51fa\u77e9\u9635\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u5d4c\u5957\u5faa\u73af\u7684\u4f7f\u7528\u9700\u8c28\u614e\uff0c\u4ee5\u907f\u514d\u6027\u80fd\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\u548cwhile\u5faa\u73af\u4e24\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002for\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u5df2\u77e5\u8303\u56f4 [&hellip;]","protected":false},"author":3,"featured_media":940874,"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\/940871"}],"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=940871"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940871\/revisions"}],"predecessor-version":[{"id":940875,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940871\/revisions\/940875"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/940874"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=940871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=940871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=940871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}