{"id":1155964,"date":"2025-01-13T18:10:17","date_gmt":"2025-01-13T10:10:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1155964.html"},"modified":"2025-01-13T18:10:20","modified_gmt":"2025-01-13T10:10:20","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1155964.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u904d\u5386\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194446\/b4d0bc2a-947b-41fc-84fa-1f60a1fc5b85.webp\" alt=\"python\u4e2d\u5982\u4f55\u904d\u5386\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u3001\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u662f\u6700\u5e38\u89c1\u548c\u76f4\u89c2\u7684\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u4f60\u76f4\u63a5\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5bf9\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>for<\/code>\u5faa\u73af\u662f\u4e00\u79cd\u9ad8\u6548\u4e14\u76f4\u89c2\u7684\u904d\u5386\u5217\u8868\u7684\u65b9\u6cd5\u3002\u5b83\u7684\u8bed\u6cd5\u7b80\u5355\uff0c\u5e76\u4e14\u80fd\u591f\u76f4\u63a5\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4f60\u53ea\u9700\u6307\u5b9a\u4e00\u4e2a\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u5c06\u5728\u6bcf\u6b21\u8fed\u4ee3\u65f6\u4f9d\u6b21\u5f15\u7528\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for item in my_list:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>item<\/code>\u662f\u4e00\u4e2a\u5360\u4f4d\u7b26\u53d8\u91cf\uff0c\u5b83\u5728\u6bcf\u6b21\u8fed\u4ee3\u65f6\u4f9d\u6b21\u5f15\u7528<code>my_list<\/code>\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u975e\u5e38\u5bb9\u6613\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>for<\/code>\u5faa\u73af<\/h2>\n<\/p>\n<p><h3>\u7b80\u5355\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u53ef\u4ee5\u7b80\u5355\u76f4\u63a5\u5730\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u65e0\u8bba\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u8fd8\u662f\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for item in my_list:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5e26\u7d22\u5f15\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u4e0d\u4ec5\u9700\u8981\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u8fd8\u9700\u8981\u8bbf\u95ee\u5b83\u4eec\u7684\u7d22\u5f15\u3002\u6b64\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>range<\/code>\u51fd\u6570\u7ed3\u5408<code>len<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<p>for i in range(len(my_list)):<\/p>\n<p>    print(f&quot;Index: {i}, Element: {my_list[i]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>enumerate<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5728\u904d\u5386\u5217\u8868\u65f6\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u53ca\u5176\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    print(f&quot;Index: {index}, Value: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>while<\/code>\u5faa\u73af<\/h2>\n<\/p>\n<p><h3>\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>for<\/code>\u5faa\u73af\u662f\u904d\u5386\u5217\u8868\u7684\u9996\u9009\u65b9\u6cd5\uff0c\u4f46<code>while<\/code>\u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u9009\u62e9\uff0c\u7279\u522b\u662f\u5f53\u4f60\u9700\u8981\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u4e00\u4e9b\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; len(my_list):<\/p>\n<p>    print(my_list[i])<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6761\u4ef6\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u904d\u5386\u5217\u8868\uff0c\u8fd9\u5728\u4e00\u4e9b\u7279\u5b9a\u7684\u7b97\u6cd5\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; len(my_list) and my_list[i] != 4:<\/p>\n<p>    print(my_list[i])<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><h3>\u7b80\u5355\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u4e00\u79cd\u7279\u6027\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>[print(item) for item in my_list]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5e26\u6761\u4ef6\u7684\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u8fd8\u5141\u8bb8\u4f60\u5728\u904d\u5386\u5217\u8868\u65f6\u6dfb\u52a0\u6761\u4ef6\uff0c\u4ece\u800c\u53ea\u5904\u7406\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>[print(item) for item in my_list if item % 2 == 0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h2>\n<\/p>\n<p><h3><code>map<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>map<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>def square(x):<\/p>\n<p>    return x * x<\/p>\n<p>squared_list = list(map(square, my_list))<\/p>\n<p>print(squared_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><code>filter<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>filter<\/code>\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u4e00\u4e2a\u6761\u4ef6\u51fd\u6570\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u53ea\u8fd4\u56de\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>def is_even(x):<\/p>\n<p>    return x % 2 == 0<\/p>\n<p>even_list = list(filter(is_even, my_list))<\/p>\n<p>print(even_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u8fed\u4ee3\u5668<\/h2>\n<\/p>\n<p><h3>\u901a\u8fc7<code>iter<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>iter<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u4ece\u800c\u53ef\u4ee5\u4f7f\u7528<code>next<\/code>\u51fd\u6570\u9010\u4e2a\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>iterator = iter(my_list)<\/p>\n<p>print(next(iterator))  # \u8f93\u51fa1<\/p>\n<p>print(next(iterator))  # \u8f93\u51fa2<\/p>\n<h2><strong>\u4f9d\u6b21\u7c7b\u63a8\uff0c\u76f4\u5230\u629b\u51faStopIteration\u5f02\u5e38<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662fPython\u4e2d\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5728\u904d\u5386\u5927\u91cf\u6570\u636e\u65f6\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator(lst):<\/p>\n<p>    for item in lst:<\/p>\n<p>        yield item<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>gen = my_generator(my_list)<\/p>\n<p>for item in gen:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h2>\n<\/p>\n<p><h3>NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([1, 2, 3, 4, 5])<\/p>\n<p>for item in my_array:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>Pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0cPandas\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u63d0\u4f9b\u4e86DataFrame\u5bf9\u8c61\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u904d\u5386\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;col1&#39;: [1, 2, 3], &#39;col2&#39;: [4, 5, 6]})<\/p>\n<p>for index, row in df.iterrows():<\/p>\n<p>    print(f&quot;Index: {index}, Row: {row[&#39;col1&#39;]}, {row[&#39;col2&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u5e76\u884c\u904d\u5386<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528<code>zip<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u540c\u65f6\u904d\u5386\u591a\u4e2a\u5217\u8868\u65f6\uff0c<code>zip<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u5de5\u5177\u3002<\/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 item1, item2 in zip(list1, list2):<\/p>\n<p>    print(f&quot;List1: {item1}, List2: {item2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>itertools<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u8fed\u4ee3\u5de5\u5177\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u904d\u5386\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for item1, item2 in itertools.product(list1, list2):<\/p>\n<p>    print(f&quot;List1: {item1}, List2: {item2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u53cd\u5411\u904d\u5386<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528<code>reversed<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u53cd\u5411\u904d\u5386\u4e00\u4e2a\u5217\u8868\u65f6\uff0c<code>reversed<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for item in reversed(my_list):<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528\u8d1f\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15\u6765\u53cd\u5411\u904d\u5386\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for i in range(len(my_list)-1, -1, -1):<\/p>\n<p>    print(my_list[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u8df3\u8dc3\u904d\u5386<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528\u6b65\u957f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u5728\u904d\u5386\u5217\u8868\u65f6\u8df3\u8fc7\u4e00\u4e9b\u5143\u7d20\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u6b65\u957f\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for i in range(0, len(my_list), 2):<\/p>\n<p>    print(my_list[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u7684\u5207\u7247\u529f\u80fd\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8df3\u8dc3\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for item in my_list[::2]:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u3001\u6761\u4ef6\u904d\u5386<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528<code>if<\/code>\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5728\u904d\u5386\u5217\u8868\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u6765\u8fc7\u6ee4\u5143\u7d20\uff0c\u53ea\u5904\u7406\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for item in my_list:<\/p>\n<p>    if item % 2 == 0:<\/p>\n<p>        print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>continue<\/code>\u8bed\u53e5<\/h3>\n<\/p>\n<p><p><code>continue<\/code>\u8bed\u53e5\u53ef\u4ee5\u5728\u67d0\u4e9b\u6761\u4ef6\u4e0b\u8df3\u8fc7\u5f53\u524d\u7684\u8fed\u4ee3\uff0c\u7ee7\u7eed\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for item in my_list:<\/p>\n<p>    if item == 3:<\/p>\n<p>        continue<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u4e00\u3001\u5d4c\u5957\u904d\u5386<\/h2>\n<\/p>\n<p><h3>\u904d\u5386\u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u904d\u5386\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b50\u5217\u8868\u7684\u5d4c\u5957\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684<code>for<\/code>\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>for sublist in nested_list:<\/p>\n<p>    for item in sublist:<\/p>\n<p>        print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u904d\u5386\u5b57\u5178\u4e2d\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u9700\u8981\u904d\u5386\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u6bcf\u4e2a\u952e\u5bf9\u5e94\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict_of_lists = {&#39;a&#39;: [1, 2, 3], &#39;b&#39;: [4, 5, 6]}<\/p>\n<p>for key, value_list in dict_of_lists.items():<\/p>\n<p>    print(f&quot;Key: {key}&quot;)<\/p>\n<p>    for item in value_list:<\/p>\n<p>        print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u4e8c\u3001\u7efc\u5408\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u904d\u5386\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af<\/strong><\/h2>\n<p>for item in my_list:<\/p>\n<p>    print(f&quot;Item: {item}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528while\u5faa\u73af<\/strong><\/h2>\n<p>i = 0<\/p>\n<p>while i &lt; len(my_list):<\/p>\n<p>    print(f&quot;While Item: {my_list[i]}&quot;)<\/p>\n<p>    i += 1<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>[print(f&quot;List Comprehension Item: {item}&quot;) for item in my_list]<\/p>\n<h2><strong>\u4f7f\u7528enumerate<\/strong><\/h2>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    print(f&quot;Enumerate - Index: {index}, Value: {value}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528zip\u51fd\u6570<\/strong><\/h2>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for item1, item2 in zip(list1, list2):<\/p>\n<p>    print(f&quot;Zip - List1: {item1}, List2: {item2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u904d\u5386\u5217\u8868\u7684\u9700\u6c42\u975e\u5e38\u5e38\u89c1\uff0c\u65e0\u8bba\u662f\u5728\u6570\u636e\u5206\u6790\u3001\u6587\u4ef6\u5904\u7406\u8fd8\u662f\u7f51\u7edc\u722c\u866b\u4e2d\uff0c\u90fd\u4f1a\u7ecf\u5e38\u7528\u5230\u3002\u638c\u63e1\u591a\u79cd\u904d\u5386\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u904d\u5386\u4e00\u4e2a\u5305\u542b\u6570\u767e\u4e07\u6761\u8bb0\u5f55\u7684\u5217\u8868\uff0c\u8fd9\u65f6\u4f7f\u7528\u751f\u6210\u5668\u6216NumPy\u5e93\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u5728\u7f51\u7edc\u722c\u866b\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u904d\u5386\u4e00\u4e2a\u5305\u542b\u591a\u4e2aURL\u7684\u5217\u8868\uff0c\u5e76\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u7b5b\u9009\u548c\u5904\u7406\u8fd9\u4e9bURL\uff0c\u8fd9\u65f6\u4f7f\u7528\u6761\u4ef6\u904d\u5386\u548c\u5e76\u884c\u904d\u5386\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u904d\u5386\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u6bcf\u4e00\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u6613\u8bfb\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u904d\u5386\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u904d\u5386\u5217\u8868\uff0c\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u4ee5\u53ca\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528for\u5faa\u73af\uff0c\u5b83\u53ef\u4ee5\u7b80\u6d01\u5730\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]\nfor item in my_list:\n    print(item)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u6613\u4e8e\u7406\u89e3\u3002\u4f7f\u7528while\u5faa\u73af\u65f6\uff0c\u9700\u8981\u624b\u52a8\u7ba1\u7406\u7d22\u5f15\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u8d85\u51fa\u5217\u8868\u7684\u8303\u56f4\u3002<\/p>\n<p><strong>\u5728\u904d\u5386\u5217\u8868\u65f6\u5982\u4f55\u540c\u65f6\u83b7\u53d6\u7d22\u5f15\uff1f<\/strong><br \/>\u5982\u679c\u5728\u904d\u5386\u5217\u8868\u7684\u540c\u65f6\u9700\u8981\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528enumerate()\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u6bcf\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\nfor index, value in enumerate(my_list):\n    print(f&quot;Index: {index}, Value: {value}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u8ba9\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5904\u7406\u5143\u7d20\u4f4d\u7f6e\u7684\u573a\u666f\u4e2d\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u904d\u5386\u5217\u8868\u7684\u9519\u8bef\u4ee5\u53ca\u5982\u4f55\u907f\u514d\uff1f<\/strong><br \/>\u904d\u5386\u5217\u8868\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u7d22\u5f15\u8d8a\u754c\u3001\u4fee\u6539\u5217\u8868\u5185\u5bb9\u5bfc\u81f4\u904d\u5386\u5f02\u5e38\u7b49\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u4e4b\u524d\u590d\u5236\u5217\u8868\uff0c\u6216\u8005\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u751f\u6210\u65b0\u5217\u8868\uff0c\u4ee5\u786e\u4fdd\u539f\u5217\u8868\u4e0d\u88ab\u4fee\u6539\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 3]\nfor item in original_list[:]:  # \u590d\u5236\u5217\u8868\n    if item == 2:\n        original_list.remove(item)  # \u907f\u514d\u76f4\u63a5\u4fee\u6539\u539f\u5217\u8868\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u4e0d\u5f71\u54cd\u539f\u5217\u8868\u7684\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528for\u5faa\u73af\u3001\u4f7f\u7528while\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528enumerate\u51fd\u6570\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528for\u5faa\u73af\u662f\u6700\u5e38\u89c1 [&hellip;]","protected":false},"author":3,"featured_media":1155969,"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\/1155964"}],"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=1155964"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155964\/revisions"}],"predecessor-version":[{"id":1155970,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155964\/revisions\/1155970"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1155969"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1155964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1155964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1155964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}