{"id":1161801,"date":"2025-01-13T19:20:49","date_gmt":"2025-01-13T11:20:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161801.html"},"modified":"2025-01-13T19:20:52","modified_gmt":"2025-01-13T11:20:52","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%adlist%e5%88%b0%e8%be%be%e6%9c%ab%e5%b0%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161801.html","title":{"rendered":"Python\u5982\u4f55\u5224\u65adlist\u5230\u8fbe\u672b\u5c3e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202947\/09c6f65a-785a-4e49-ae55-c268822cd8ce.webp\" alt=\"Python\u5982\u4f55\u5224\u65adlist\u5230\u8fbe\u672b\u5c3e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2alist\u662f\u5426\u5230\u8fbe\u672b\u5c3e\uff0c\u53ef\u4ee5\u901a\u8fc7<strong>\u904d\u5386\u5217\u8868\u3001\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u3001\u5f02\u5e38\u5904\u7406<\/strong>\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c<strong>\u904d\u5386\u5217\u8868<\/strong>\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u8fed\u4ee3\u5668\u904d\u5386\u5217\u8868\u5143\u7d20\u5e76\u5224\u65ad\u662f\u5426\u6709\u5269\u4f59\u5143\u7d20\u3002\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u904d\u5386\u5217\u8868\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5<\/strong>\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6216while\u5faa\u73af\u6765\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u5faa\u73af\u7ed3\u675f\u65f6\u77e5\u9053\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af<\/strong><\/h2>\n<p>for item in my_list:<\/p>\n<p>    print(item)<\/p>\n<p>print(&quot;Reached the end of the list.&quot;)<\/p>\n<h2><strong>\u4f7f\u7528while\u5faa\u73af<\/strong><\/h2>\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>print(&quot;Reached the end of the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7for\u5faa\u73af\u548cwhile\u5faa\u73af\u904d\u5386\u5217\u8868\uff0c\u5f53\u5faa\u73af\u7ed3\u675f\u65f6\uff0c\u5373\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u904d\u5386\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u904d\u5386\u5217\u8868\u662f\u5224\u65ad\u5217\u8868\u662f\u5426\u5230\u8fbe\u672b\u5c3e\u7684\u4e00\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u904d\u5386\u5217\u8868\uff0c\u5305\u62ecfor\u5faa\u73af\u548cwhile\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>for\u5faa\u73af\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u76f4\u89c2\u7684\u65b9\u6cd5\u6765\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\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>print(&quot;Reached the end of the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor\u5faa\u73af\u81ea\u52a8\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u5faa\u73af\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528while\u5faa\u73af<\/h3>\n<\/p>\n<p><p>while\u5faa\u73af\u662f\u4e00\u79cd\u7075\u6d3b\u6027\u66f4\u9ad8\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u63a7\u5236\u7d22\u5f15\u6765\u904d\u5386\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528while\u5faa\u73af\u904d\u5386\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/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>print(&quot;Reached the end of the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528while\u5faa\u73af\u624b\u52a8\u63a7\u5236\u7d22\u5f15\uff0c\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u5faa\u73af\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u7d22\u5f15\u8d85\u51fa\u8303\u56f4<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5224\u65ad\u5217\u8868\u662f\u5426\u5230\u8fbe\u672b\u5c3e\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u6765\u5b9e\u73b0\u3002\u5f53\u8bbf\u95ee\u4e00\u4e2a\u8d85\u8fc7\u5217\u8868\u957f\u5ea6\u7684\u7d22\u5f15\u65f6\uff0c\u4f1a\u5f15\u53d1IndexError\u5f02\u5e38\uff0c\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u70b9\u6765\u5224\u65ad\u5217\u8868\u662f\u5426\u5230\u8fbe\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528try-except\u6355\u83b7\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7try-except\u8bed\u53e5\u6355\u83b7IndexError\u5f02\u5e38\uff0c\u53ef\u4ee5\u5224\u65ad\u5217\u8868\u662f\u5426\u5230\u8fbe\u672b\u5c3e\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/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 True:<\/p>\n<p>    try:<\/p>\n<p>        print(my_list[index])<\/p>\n<p>        index += 1<\/p>\n<p>    except IndexError:<\/p>\n<p>        print(&quot;Reached the end of the list.&quot;)<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7IndexError\u5f02\u5e38\uff0c\u5f53\u8bbf\u95ee\u8d85\u51fa\u5217\u8868\u957f\u5ea6\u7684\u7d22\u5f15\u65f6\uff0c\u4f1a\u5f15\u53d1\u5f02\u5e38\u5e76\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u8fed\u4ee3\u5668<\/h2>\n<\/p>\n<p><p>Python\u7684\u8fed\u4ee3\u5668\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u4e0d\u9700\u8981\u624b\u52a8\u63a7\u5236\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\u904d\u5386\u5217\u8868\u3002\u4f7f\u7528iter()\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u8fed\u4ee3\u5668\uff0c\u7136\u540e\u4f7f\u7528next()\u51fd\u6570\u83b7\u53d6\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528iter()\u548cnext()<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528iter()\u548cnext()\u904d\u5386\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>it = iter(my_list)<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        item = next(it)<\/p>\n<p>        print(item)<\/p>\n<p>    except StopIteration:<\/p>\n<p>        print(&quot;Reached the end of the list.&quot;)<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528iter()\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u8fed\u4ee3\u5668\uff0c\u7136\u540e\u4f7f\u7528next()\u51fd\u6570\u83b7\u53d6\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u5f53\u8fed\u4ee3\u5668\u5230\u8fbe\u672b\u5c3e\u65f6\uff0c\u4f1a\u5f15\u53d1StopIteration\u5f02\u5e38\uff0c\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u5e76\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u751f\u6210\u5668<\/h2>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u9010\u4e2a\u751f\u6210\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9010\u4e2a\u5904\u7406\u5927\u6570\u636e\u96c6\u7684\u60c5\u51b5\u3002\u4f7f\u7528\u751f\u6210\u5668\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u904d\u5386\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570\u5e76\u904d\u5386\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def list_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 = list_generator(my_list)<\/p>\n<p>for item in gen:<\/p>\n<p>    print(item)<\/p>\n<p>print(&quot;Reached the end of the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570list_generator\uff0c\u901a\u8fc7yield\u5173\u952e\u5b57\u9010\u4e2a\u751f\u6210\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u751f\u6210\u5668\uff0c\u5f53\u751f\u6210\u5668\u5230\u8fbe\u672b\u5c3e\u65f6\uff0c\u5faa\u73af\u7ed3\u675f\u5e76\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5185\u7f6e\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u904d\u5386\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u4f7f\u7528enumerate()\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u53ca\u5176\u7d22\u5f15\uff0c\u4f7f\u7528zip()\u51fd\u6570\u53ef\u4ee5\u5e76\u884c\u904d\u5386\u591a\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528enumerate()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528enumerate()\u51fd\u6570\u904d\u5386\u5217\u8868\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 index, item in enumerate(my_list):<\/p>\n<p>    print(f&quot;Index: {index}, Item: {item}&quot;)<\/p>\n<p>print(&quot;Reached the end of the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528enumerate()\u51fd\u6570\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u53ca\u5176\u7d22\u5f15\uff0c\u5e76\u5728\u5faa\u73af\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528zip()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528zip()\u51fd\u6570\u5e76\u884c\u904d\u5386\u4e24\u4e2a\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]<\/p>\n<p>for item1, item2 in zip(list1, list2):<\/p>\n<p>    print(f&quot;Item1: {item1}, Item2: {item2}&quot;)<\/p>\n<p>print(&quot;Reached the end of the lists.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528zip()\u51fd\u6570\u5e76\u884c\u904d\u5386\u4e24\u4e2a\u5217\u8868\uff0c\u5e76\u5728\u5faa\u73af\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528itertools\u6a21\u5757<\/h2>\n<\/p>\n<p><p>itertools\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u8fed\u4ee3\u7684\u5de5\u5177\u51fd\u6570\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u904d\u5386\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n()\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u5217\u8868\u8fde\u63a5\u8d77\u6765\u8fdb\u884c\u904d\u5386\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528itertools.chain()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528itertools.chain()\u51fd\u6570\u8fde\u63a5\u5e76\u904d\u5386\u4e24\u4e2a\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]<\/p>\n<p>for item in itertools.chain(list1, list2):<\/p>\n<p>    print(item)<\/p>\n<p>print(&quot;Reached the end of the lists.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528itertools.chain()\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u5728\u5faa\u73af\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528itertools.islice()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>itertools.islice()\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5217\u8868\u7684\u90e8\u5206\u5207\u7247\u904d\u5386\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528itertools.islice()\u51fd\u6570\u904d\u5386\u5217\u8868\u524d3\u4e2a\u5143\u7d20\u7684\u793a\u4f8b\uff1a<\/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 item in itertools.islice(my_list, 3):<\/p>\n<p>    print(item)<\/p>\n<p>print(&quot;Reached the end of the slice.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528itertools.islice()\u51fd\u6570\u904d\u5386\u5217\u8868\u7684\u524d3\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u5faa\u73af\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5207\u7247\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u5217\u8868\u7684\u540c\u65f6\u8fdb\u884c\u4e00\u4e9b\u7b80\u5355\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u5217\u8868\u5e76\u751f\u6210\u65b0\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>squared_list = [item2 for item in my_list]<\/p>\n<p>print(squared_list)<\/p>\n<p>print(&quot;Reached the end of the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u5217\u8868\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u64cd\u4f5c\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5e76\u5728\u64cd\u4f5c\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u4f7f\u7528\u9012\u5f52<\/h2>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6280\u672f\uff0c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u81ea\u8eab\u8c03\u7528\u6765\u5b9e\u73b0\u904d\u5386\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u9012\u5f52\u904d\u5386\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_traverse(lst, index=0):<\/p>\n<p>    if index &gt;= len(lst):<\/p>\n<p>        print(&quot;Reached the end of the list.&quot;)<\/p>\n<p>        return<\/p>\n<p>    print(lst[index])<\/p>\n<p>    recursive_traverse(lst, index + 1)<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>recursive_traverse(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570recursive_traverse\uff0c\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u81ea\u8eab\u6765\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u9012\u5f52\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u4f7f\u7528\u751f\u6210\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u751f\u6210\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u5217\u8868\u7684\u540c\u65f6\u8fdb\u884c\u4e00\u4e9b\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u751f\u6210\u8868\u8fbe\u5f0f\u904d\u5386\u5217\u8868\u5e76\u751f\u6210\u65b0\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>squared_generator = (item2 for item in my_list)<\/p>\n<p>for item in squared_generator:<\/p>\n<p>    print(item)<\/p>\n<p>print(&quot;Reached the end of the generator.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u751f\u6210\u8868\u8fbe\u5f0f\u904d\u5386\u5217\u8868\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u64cd\u4f5c\uff0c\u751f\u6210\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u5e76\u5728\u64cd\u4f5c\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u751f\u6210\u5668\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u4f7f\u7528deque<\/h2>\n<\/p>\n<p><p>collections\u6a21\u5757\u4e2d\u7684deque\uff08\u53cc\u7aef\u961f\u5217\uff09\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u904d\u5386\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528deque\u904d\u5386\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>dq = deque(my_list)<\/p>\n<p>while dq:<\/p>\n<p>    print(dq.popleft())<\/p>\n<p>print(&quot;Reached the end of the deque.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528deque\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u53cc\u7aef\u961f\u5217\uff0c\u5e76\u901a\u8fc7popleft()\u65b9\u6cd5\u4ece\u5de6\u7aef\u5f39\u51fa\u5143\u7d20\u8fdb\u884c\u904d\u5386\uff0c\u5f53deque\u4e3a\u7a7a\u65f6\uff0c\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e00\u3001\u4f7f\u7528map\u51fd\u6570<\/h2>\n<\/p>\n<p><p>map\u51fd\u6570\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528map\u51fd\u6570\u904d\u5386\u5217\u8868\u5e76\u8fdb\u884c\u5e73\u65b9\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>squared_iterator = map(lambda x: x2, my_list)<\/p>\n<p>for item in squared_iterator:<\/p>\n<p>    print(item)<\/p>\n<p>print(&quot;Reached the end of the iterator.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528map\u51fd\u6570\u5c06\u5e73\u65b9\u64cd\u4f5c\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5e76\u5728\u64cd\u4f5c\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u8fed\u4ee3\u5668\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e8c\u3001\u4f7f\u7528filter\u51fd\u6570<\/h2>\n<\/p>\n<p><p>filter\u51fd\u6570\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u8fc7\u6ee4\u6761\u4ef6\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528filter\u51fd\u6570\u904d\u5386\u5217\u8868\u5e76\u8fc7\u6ee4\u5947\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>even_iterator = filter(lambda x: x % 2 == 0, my_list)<\/p>\n<p>for item in even_iterator:<\/p>\n<p>    print(item)<\/p>\n<p>print(&quot;Reached the end of the iterator.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528filter\u51fd\u6570\u5c06\u8fc7\u6ee4\u6761\u4ef6\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5e76\u5728\u64cd\u4f5c\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u8fed\u4ee3\u5668\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e09\u3001\u4f7f\u7528reduce\u51fd\u6570<\/h2>\n<\/p>\n<p><p>functools\u6a21\u5757\u4e2d\u7684reduce\u51fd\u6570\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u4e8c\u5143\u64cd\u4f5c\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5355\u4e00\u7684\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528reduce\u51fd\u6570\u904d\u5386\u5217\u8868\u5e76\u8ba1\u7b97\u7d2f\u79ef\u548c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>total_sum = reduce(lambda x, y: x + y, my_list)<\/p>\n<p>print(total_sum)<\/p>\n<p>print(&quot;Reached the end of the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528reduce\u51fd\u6570\u5c06\u7d2f\u79ef\u548c\u64cd\u4f5c\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u8ba1\u7b97\u6700\u7ec8\u7684\u7d2f\u79ef\u548c\uff0c\u5e76\u5728\u64cd\u4f5c\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u5217\u8868\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u56db\u3001\u4f7f\u7528\u9012\u5f52\u751f\u6210\u5668<\/h2>\n<\/p>\n<p><p>\u9012\u5f52\u751f\u6210\u5668\u662f\u4e00\u79cd\u7ed3\u5408\u9012\u5f52\u548c\u751f\u6210\u5668\u7684\u6280\u672f\uff0c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u9010\u4e2a\u751f\u6210\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u9012\u5f52\u751f\u6210\u5668\u904d\u5386\u5217\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_generator(lst, index=0):<\/p>\n<p>    if index &gt;= len(lst):<\/p>\n<p>        return<\/p>\n<p>    yield lst[index]<\/p>\n<p>    yield from recursive_generator(lst, index + 1)<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>gen = recursive_generator(my_list)<\/p>\n<p>for item in gen:<\/p>\n<p>    print(item)<\/p>\n<p>print(&quot;Reached the end of the generator.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u751f\u6210\u5668\u51fd\u6570recursive_generator\uff0c\u901a\u8fc7yield\u5173\u952e\u5b57\u9010\u4e2a\u751f\u6210\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u5728\u751f\u6210\u7ed3\u675f\u65f6\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\uff0c\u8868\u793a\u751f\u6210\u5668\u5df2\u7ecf\u904d\u5386\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5404\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u5224\u65ad\u5217\u8868\u662f\u5426\u5230\u8fbe\u672b\u5c3e\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6765\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>if not my_list:<\/code>\u6765\u5224\u65ad<code>my_list<\/code>\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u5c06\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<p><strong>\u5728\u904d\u5386\u5217\u8868\u65f6\u5982\u4f55\u5224\u65ad\u662f\u5426\u5230\u8fbe\u672b\u5c3e\uff1f<\/strong><br \/>\u5728\u904d\u5386\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u7ed3\u5408<code>enumerate()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7d22\u5f15\uff0c\u4ece\u800c\u5224\u65ad\u662f\u5426\u5230\u8fbe\u5217\u8868\u672b\u5c3e\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>i<\/code>\u662f\u5f53\u524d\u7d22\u5f15\uff0c\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83<code>i<\/code>\u4e0e<code>len(my_list) - 1<\/code>\u6765\u5224\u65ad\u662f\u5426\u4e3a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u68c0\u67e5\u5217\u8868\u672b\u5c3e\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528Python\u7684\u5207\u7247\u529f\u80fd\u6765\u7b80\u5316\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>my_list[:-1]<\/code>\u53ef\u4ee5\u5f97\u5230\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\uff0c\u9664\u4e86\u6700\u540e\u4e00\u4e2a\u3002\u5728\u5904\u7406\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u8fd9\u4e2a\u5207\u7247\u8fdb\u884c\u64cd\u4f5c\uff0c\u4ece\u800c\u907f\u514d\u624b\u52a8\u68c0\u67e5\u662f\u5426\u8fbe\u5230\u5217\u8868\u672b\u5c3e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2alist\u662f\u5426\u5230\u8fbe\u672b\u5c3e\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5217\u8868\u3001\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u3001\u5f02\u5e38\u5904\u7406\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u904d\u5386 [&hellip;]","protected":false},"author":3,"featured_media":1161808,"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\/1161801"}],"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=1161801"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161801\/revisions"}],"predecessor-version":[{"id":1161813,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161801\/revisions\/1161813"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161808"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}