{"id":934228,"date":"2024-12-26T18:31:43","date_gmt":"2024-12-26T10:31:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/934228.html"},"modified":"2024-12-26T18:31:44","modified_gmt":"2024-12-26T10:31:44","slug":"python%e5%a6%82%e4%bd%95%e5%be%aa%e7%8e%aflist","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/934228.html","title":{"rendered":"python\u5982\u4f55\u5faa\u73aflist"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071334\/02cf69b1-54ea-46eb-a5a1-9136a3798a5a.webp\" alt=\"python\u5982\u4f55\u5faa\u73aflist\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5faa\u73aflist\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ecfor\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528for\u5faa\u73af\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/strong> \u901a\u8fc7for\u5faa\u73af\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u51e0\u79cd\u5e38\u89c1\u7684\u5faa\u73af\u5217\u8868\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001FOR\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cfor\u5faa\u73af\u662f\u6700\u5e38\u7528\u7684\u904d\u5386\u5217\u8868\u7684\u65b9\u6cd5\u3002\u5176\u8bed\u6cd5\u7b80\u5355\uff0c\u4e14\u80fd\u76f4\u89c2\u5730\u8868\u793a\u51fa\u5bf9\u6bcf\u4e2a\u5143\u7d20\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u5728for\u5faa\u73af\u4e2d\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>for element in my_list:<\/code>\u8bed\u53e5\u4f1a\u4f9d\u6b21\u5c06<code>my_list<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u7ed9<code>element<\/code>\u53d8\u91cf\uff0c\u968f\u540e\u5728\u5faa\u73af\u4f53\u4e2d\u5bf9\u5143\u7d20\u8fdb\u884c\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u7d22\u5f15<\/li>\n<\/ol>\n<p><p>\u6709\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8bbf\u95ee\u5143\u7d20\u7684\u7d22\u5f15\u3002\u53ef\u4ee5\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/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 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><p><code>enumerate(my_list)<\/code>\u8fd4\u56de\u4e00\u4e2a\u679a\u4e3e\u5bf9\u8c61\uff0c\u5b83\u751f\u6210\u5305\u542b\u5217\u8868\u5143\u7d20\u7d22\u5f15\u548c\u503c\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001WHILE\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u867d\u7136while\u5faa\u73af\u4e0d\u5982for\u5faa\u73af\u76f4\u89c2\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4ecd\u7136\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5f53\u60a8\u9700\u8981\u66f4\u590d\u6742\u7684\u5faa\u73af\u63a7\u5236\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u901a\u8fc7\u624b\u52a8\u7ba1\u7406\u7d22\u5f15\u6765\u904d\u5386\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40]<\/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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u7d22\u5f15\u53d8\u91cf<code>index<\/code>\uff0c\u7136\u540e\u5728while\u5faa\u73af\u4e2d\u68c0\u67e5\u5176\u662f\u5426\u5c0f\u4e8e\u5217\u8868\u7684\u957f\u5ea6\u3002\u5982\u679c\u662f\uff0c\u5219\u6253\u5370\u51fa\u5f53\u524d\u7d22\u5f15\u5bf9\u5e94\u7684\u5143\u7d20\u5e76\u589e\u52a0\u7d22\u5f15\u503c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u7075\u6d3b\u63a7\u5236<\/li>\n<\/ol>\n<p><p>while\u5faa\u73af\u5141\u8bb8\u66f4\u590d\u6742\u7684\u6761\u4ef6\u63a7\u5236\uff0c\u4f8b\u5982\u63d0\u524d\u9000\u51fa\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [3, 6, 9, 12]<\/p>\n<p>index = 0<\/p>\n<p>while index &lt; len(my_list):<\/p>\n<p>    if my_list[index] &gt; 10:<\/p>\n<p>        break<\/p>\n<p>    print(my_list[index])<\/p>\n<p>    index += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u5faa\u73af\u4f1a\u5728\u9047\u5230\u5927\u4e8e10\u7684\u5143\u7d20\u65f6\u63d0\u524d\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38Pythonic\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u5728\u4e00\u4e2a\u7b80\u6d01\u7684\u884c\u5185\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u867d\u7136\u5b83\u4e0d\u662f\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u5faa\u73af\uff0c\u4f46\u5e38\u7528\u4e8e\u5728\u904d\u5386\u5217\u8868\u65f6\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4]<\/p>\n<p>squared_list = [x  2 for x in my_list]<\/p>\n<p>print(squared_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u539f\u59cb\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u503c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u6761\u4ef6\u8bed\u53e5<\/li>\n<\/ol>\n<p><p>\u60a8\u8fd8\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u6dfb\u52a0\u6761\u4ef6\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [5, 10, 15, 20]<\/p>\n<p>filtered_list = [x for x in my_list if x &gt; 10]<\/p>\n<p>print(filtered_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u4ec5\u5305\u542b\u5927\u4e8e10\u7684\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528ITERTOOLS\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684<code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u7528\u4e8e\u9ad8\u6548\u8fed\u4ee3\u7684\u5de5\u5177\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u8fed\u4ee3\u9700\u6c42\uff0c\u8be5\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><code>cycle<\/code>\u51fd\u6570<\/li>\n<\/ol>\n<p><p><code>itertools.cycle<\/code>\u51fd\u6570\u4f1a\u65e0\u9650\u5faa\u73af\u5730\u904d\u5386\u5217\u8868\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>counter = 0<\/p>\n<p>for fruit in itertools.cycle(my_list):<\/p>\n<p>    if counter &gt; 5:<\/p>\n<p>        break<\/p>\n<p>    print(fruit)<\/p>\n<p>    counter += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u793a\u4f8b\u4f1a\u91cd\u590d\u6253\u5370\u5217\u8868\u5143\u7d20\uff0c\u76f4\u5230\u8ba1\u6570\u5668\u8d85\u8fc75\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><code>islice<\/code>\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>islice<\/code>\u6765\u9650\u5236\u8fed\u4ee3\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import islice<\/p>\n<p>my_list = [&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;]<\/p>\n<p>for color in islice(itertools.cycle(my_list), 5):<\/p>\n<p>    print(color)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>islice<\/code>\u51fd\u6570\u5728\u8fd9\u91cc\u9650\u5236\u4e86\u8fed\u4ee3\u6b21\u6570\uff0c\u5373\u4fbf\u662f\u5bf9\u65e0\u9650\u8fed\u4ee3\u7684\u5bf9\u8c61\u4e5f\u9002\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u9012\u5f52\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u867d\u7136\u9012\u5f52\u4e0d\u5e38\u7528\u4e8e\u904d\u5386\u5217\u8868\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u5b83\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u9012\u5f52<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def print_list_recursively(lst, index=0):<\/p>\n<p>    if index &lt; len(lst):<\/p>\n<p>        print(lst[index])<\/p>\n<p>        print_list_recursively(lst, index + 1)<\/p>\n<p>my_list = [100, 200, 300]<\/p>\n<p>print_list_recursively(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u9012\u5f52\u51fd\u6570\u4f1a\u4f9d\u6b21\u6253\u5370\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u6ce8\u610f\u4e8b\u9879<\/li>\n<\/ol>\n<p><p>\u8bf7\u6ce8\u610f\uff0c\u9012\u5f52\u65b9\u6cd5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6808\u6ea2\u51fa\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u8f83\u5927\u7684\u5217\u8868\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u9700\u8981\u5c0f\u5fc3\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u904d\u5386\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0cfor\u5faa\u73af\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u5176\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u800c\u5728\u5904\u7406\u66f4\u590d\u6742\u7684\u903b\u8f91\u65f6\uff0cwhile\u5faa\u73af\u548c<code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002\u4e86\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u6709\u52a9\u4e8e\u60a8\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5217\u8868\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u904d\u5386\u4e00\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u904d\u5386\u5217\u8868\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff0c\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\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5217\u8868\u7684\u7d22\u5f15\u4e5f\u53ef\u4ee5\u904d\u5386\u5217\u8868\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(len(my_list)):\n    print(my_list[i])\n<\/code><\/pre>\n<p>\u8fd9\u5728\u9700\u8981\u8bbf\u95ee\u5143\u7d20\u7d22\u5f15\u7684\u573a\u666f\u4e0b\u7279\u522b\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5faa\u73af\u4e2d\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728\u5faa\u73af\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u4fee\u6539\u3001\u8ba1\u7b97\u6216\u6761\u4ef6\u5224\u65ad\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u6570\u5b57\u5e73\u65b9\uff1a  <\/p>\n<pre><code class=\"language-python\">squared_list = [x**2 for x in my_list]\nprint(squared_list)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u7528\u6cd5\u5728\u5904\u7406\u6570\u636e\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u5229\u7528\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u751f\u6210\u65b0\u5217\u8868\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u904d\u5386\u5217\u8868\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff0c<code>while<\/code>\u5faa\u73af\u4e5f\u662f\u904d\u5386\u5217\u8868\u7684\u6709\u6548\u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u624b\u52a8\u63a7\u5236\u7d22\u5f15\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">index = 0\nwhile index &lt; len(my_list):\n    print(my_list[index])\n    index += 1\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u63a7\u5236\uff0c\u9002\u5408\u5728\u5faa\u73af\u4e2d\u9700\u8981\u590d\u6742\u903b\u8f91\u65f6\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5faa\u73aflist\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ecfor\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":934230,"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\/934228"}],"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=934228"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934228\/revisions"}],"predecessor-version":[{"id":934231,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934228\/revisions\/934231"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/934230"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=934228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=934228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=934228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}