{"id":1107671,"date":"2025-01-08T16:52:18","date_gmt":"2025-01-08T08:52:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1107671.html"},"modified":"2025-01-08T16:52:21","modified_gmt":"2025-01-08T08:52:21","slug":"python%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1107671.html","title":{"rendered":"python\u5982\u4f55\u904d\u5386\u5217\u8868\u4e2d\u7684\u6570\u636e\u7ed3\u6784"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071602\/4717a157-dfb9-496c-9c1b-2d99e5b7dcb0.webp\" alt=\"python\u5982\u4f55\u904d\u5386\u5217\u8868\u4e2d\u7684\u6570\u636e\u7ed3\u6784\" \/><\/p>\n<p><p> <strong>Python\u904d\u5386\u5217\u8868\u4e2d\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001enumerate\u51fd\u6570\u3001itertools\u5e93\u3001\u9012\u5f52\u7b49\u65b9\u6cd5<\/strong>\u3002\u8fd9\u91cc\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001FOR\u5faa\u73af\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4e5f\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for element in my_list:<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5f53\u5217\u8868\u4e2d\u5305\u542b\u5d4c\u5957\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\u8fdb\u884c\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>for sublist in nested_list:<\/p>\n<p>    for element in sublist:<\/p>\n<p>        print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001WHILE\u5faa\u73af\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528while\u5faa\u73af\u904d\u5386\u5217\u8868\u9700\u8981\u624b\u52a8\u7ef4\u62a4\u5faa\u73af\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>index = 0<\/p>\n<p>while index &lt; len(my_list):<\/p>\n<p>    print(my_list[index])<\/p>\n<p>    index += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u4f7f\u7528while\u5faa\u73af\u4e5f\u53ef\u4ee5\u904d\u5386\u5d4c\u5957\u5217\u8868\uff0c\u4f46\u4ee3\u7801\u4f1a\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>outer_index = 0<\/p>\n<p>while outer_index &lt; len(nested_list):<\/p>\n<p>    inner_index = 0<\/p>\n<p>    while inner_index &lt; len(nested_list[outer_index]):<\/p>\n<p>        print(nested_list[outer_index][inner_index])<\/p>\n<p>        inner_index += 1<\/p>\n<p>    outer_index += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u5bf9\u5217\u8868\u7684\u904d\u5386\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>squared_list = [x2 for x in my_list]<\/p>\n<p>print(squared_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5d4c\u5957\u5217\u8868\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>flattened_list = [element for sublist in nested_list for element in sublist]<\/p>\n<p>print(flattened_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528ENUMERATE\u51fd\u6570\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>enumerate\u51fd\u6570\u5728\u904d\u5386\u5217\u8868\u65f6\u63d0\u4f9b\u4e86\u7d22\u5f15\u548c\u503c\u7684\u914d\u5bf9\uff0c\u65b9\u4fbf\u5bf9\u5143\u7d20\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/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><h3>2. \u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5d4c\u5957\u5217\u8868\uff0cenumerate\u51fd\u6570\u540c\u6837\u9002\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>for outer_index, sublist in enumerate(nested_list):<\/p>\n<p>    for inner_index, element in enumerate(sublist):<\/p>\n<p>        print(f&quot;Outer Index: {outer_index}, Inner Index: {inner_index}, Element: {element}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528ITERTTOOLS\u5e93\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>itertools\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u9ad8\u6548\u904d\u5386\u548c\u64cd\u4f5c\u8fed\u4ee3\u5668\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for element in itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n(my_list):<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>itertools.chain.from_iterable\u53ef\u4ee5\u7528\u4e8e\u5c06\u5d4c\u5957\u5217\u8868\u5c55\u5e73\u6210\u5355\u4e00\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>for element in itertools.chain.from_iterable(nested_list):<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u9012\u5f52\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u6811\u5f62\u6216\u591a\u7ea7\u5d4c\u5957\u7684\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_traverse(data):<\/p>\n<p>    if isinstance(data, list):<\/p>\n<p>        for item in data:<\/p>\n<p>            recursive_traverse(item)<\/p>\n<p>    else:<\/p>\n<p>        print(data)<\/p>\n<p>nested_list = [1, [2, [3, [4, 5]]]]<\/p>\n<p>recursive_traverse(nested_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u9012\u5f52\u904d\u5386\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u5305\u542b\u5b57\u5178\u7684\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_traverse_dict(data):<\/p>\n<p>    if isinstance(data, dict):<\/p>\n<p>        for key, value in data.items():<\/p>\n<p>            print(f&quot;Key: {key}&quot;)<\/p>\n<p>            recursive_traverse_dict(value)<\/p>\n<p>    elif isinstance(data, list):<\/p>\n<p>        for item in data:<\/p>\n<p>            recursive_traverse_dict(item)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Value: {data}&quot;)<\/p>\n<p>nested_dict = {&#39;a&#39;: 1, &#39;b&#39;: {&#39;c&#39;: 2, &#39;d&#39;: {&#39;e&#39;: 3}}}<\/p>\n<p>recursive_traverse_dict(nested_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528MAP\u51fd\u6570\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>map\u51fd\u6570\u9002\u7528\u4e8e\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u5e94\u7528\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>squared_list = map(lambda x: x2, my_list)<\/p>\n<p>print(list(squared_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5d4c\u5957\u5217\u8868\uff0cmap\u51fd\u6570\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>flattened_list = map(lambda sublist: map(lambda x: x2, sublist), nested_list)<\/p>\n<p>print([list(sublist) for sublist in flattened_list])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u4f7f\u7528\u751f\u6210\u5668\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa\u8fed\u4ee3\u5668\u7684\u7b80\u5355\u800c\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u6570\u636e\u7ed3\u6784\u65f6\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generator_traverse(data):<\/p>\n<p>    for item in data:<\/p>\n<p>        yield item<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for element in generator_traverse(my_list):<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8fd8\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u5d4c\u5957\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generator_traverse_nested(data):<\/p>\n<p>    for sublist in data:<\/p>\n<p>        for item in sublist:<\/p>\n<p>            yield item<\/p>\n<p>nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>for element in generator_traverse_nested(nested_list):<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u4f7f\u7528NUMPY\u5e93\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>numpy\u5e93\u662f\u5904\u7406\u5927\u578b\u6570\u7ec4\u548c\u77e9\u9635\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u904d\u5386\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 element in np.nditer(my_array):<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5d4c\u5957\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>numpy\u7684nditer\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_array = np.array([[1, 2], [3, 4], [5, 6]])<\/p>\n<p>for element in np.nditer(nested_array):<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u3001\u4f7f\u7528PANDAS\u5e93\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>pandas\u5e93\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u63d0\u4f9b\u4e86\u904d\u5386DataFrame\u548cSeries\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>my_series = pd.Series([1, 2, 3, 4, 5])<\/p>\n<p>for element in my_series:<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. DataFrame\u904d\u5386<\/h3>\n<\/p>\n<p><p>DataFrame\u662fpandas\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u53ef\u4ee5\u4f7f\u7528iterrows\u6216itertuples\u8fdb\u884c\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dataframe = pd.DataFrame({&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]})<\/p>\n<p>for index, row in my_dataframe.iterrows():<\/p>\n<p>    print(f&quot;Index: {index}, Row: {row[&#39;A&#39;]}, {row[&#39;B&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u4e00\u3001\u5e76\u884c\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5e76\u884c\u904d\u5386\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u63d0\u9ad8\u904d\u5386\u6548\u7387\u3002\u8fd9\u91cc\u4ee5concurrent.futures\u5e93\u4e3a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>def process_element(element):<\/p>\n<p>    return element  2<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>    results = list(executor.map(process_element, my_list))<\/p>\n<p>print(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5d4c\u5957\u5217\u8868\u5e76\u884c\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5d4c\u5957\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684\u5e76\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>def process_sublist(sublist):<\/p>\n<p>    with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>        return list(executor.map(process_element, sublist))<\/p>\n<p>with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>    results = list(executor.map(process_sublist, nested_list))<\/p>\n<p>print(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u904d\u5386\u5217\u8868\u4e2d\u7684\u6570\u636e\u7ed3\u6784\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u8fd0\u884c\u6548\u7387\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u60a8\u80fd\u5bf9\u5982\u4f55\u904d\u5386Python\u5217\u8868\u4e2d\u7684\u6570\u636e\u7ed3\u6784\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\u548c\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u904d\u5386\u5217\u8868\u4e2d\u7684\u5b57\u5178\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u904d\u5386\u5217\u8868\u4e2d\u7684\u5b57\u5178\u3002\u901a\u8fc7\u8fed\u4ee3\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u6bcf\u4e2a\u5b57\u5178\uff0c\u5e76\u4f7f\u7528\u5b57\u5178\u7684\u952e\u6765\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}, {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}]\nfor item in my_list:\n    print(f&quot;Name: {item[&#39;name&#39;]}, Age: {item[&#39;age&#39;]}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u8bbf\u95ee\u5217\u8868\u4e2d\u6bcf\u4e2a\u5b57\u5178\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u904d\u5386\u5305\u542b\u5217\u8868\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5f53\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u5217\u8868\u7684\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\u6765\u904d\u5386\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nfor sublist in nested_list:\n    for item in sublist:\n        print(item)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u6709\u6548\u5730\u8bbf\u95ee\u5230\u6bcf\u4e00\u4e2a\u5b50\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u5217\u8868\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/strong><br \/>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u4f18\u96c5\u4e14\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u5217\u8868\u7684\u540c\u65f6\u521b\u5efa\u65b0\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u4ece\u4e00\u4e2a\u6570\u5b57\u5217\u8868\u4e2d\u83b7\u53d6\u5e73\u65b9\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]\nsquares = [x**2 for x in numbers]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u63d0\u9ad8\u6027\u80fd\uff0c\u7279\u522b\u662f\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u904d\u5386\u5217\u8868\u4e2d\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001enumerate\u51fd\u6570\u3001ite [&hellip;]","protected":false},"author":3,"featured_media":1107679,"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\/1107671"}],"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=1107671"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107671\/revisions"}],"predecessor-version":[{"id":1107680,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107671\/revisions\/1107680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1107679"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1107671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1107671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1107671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}