{"id":1055440,"date":"2024-12-31T14:50:45","date_gmt":"2024-12-31T06:50:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1055440.html"},"modified":"2024-12-31T14:50:48","modified_gmt":"2024-12-31T06:50:48","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e5%88%97%e8%a1%a8%e9%87%8c%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1055440.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u904d\u5386\u5217\u8868\u91cc\u7684\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/06650672-169c-493b-8cff-dce544da9014.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u904d\u5386\u5217\u8868\u91cc\u7684\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u904d\u5386\u5217\u8868\u91cc\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b8c\u6210\uff0c\u4f8b\u5982\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u3001enumerate\u51fd\u6570\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528for\u5faa\u73af\u6765\u904d\u5386\u5217\u8868\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\u6570\u636e<\/h2>\n<\/p>\n<p><p>For\u5faa\u73af\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u904d\u5386\u5217\u8868\u7684\u65b9\u6cd5\u3002for\u5faa\u73af\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>for fruit in fruits:<\/p>\n<p>    print(fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor\u5faa\u73af\u4f1a\u4f9d\u6b21\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u8d4b\u503c\u7ed9\u53d8\u91cf<code>fruit<\/code>\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>apple<\/p>\n<p>banana<\/p>\n<p>cherry<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528while\u5faa\u73af\u904d\u5386\u5217\u8868\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u867d\u7136for\u5faa\u73af\u662f\u904d\u5386\u5217\u8868\u7684\u6700\u5e38\u7528\u65b9\u6cd5\uff0c\u4f46while\u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u6765\u904d\u5386\u5217\u8868\u3002\u4f7f\u7528while\u5faa\u73af\u65f6\u9700\u8981\u624b\u52a8\u7ba1\u7406\u5217\u8868\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u521d\u59cb\u5316\u7d22\u5f15<\/strong><\/h2>\n<p>index = 0<\/p>\n<h2><strong>\u4f7f\u7528while\u5faa\u73af\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>while index &lt; len(fruits):<\/p>\n<p>    print(fruits[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\u4f7f\u7528while\u5faa\u73af\u904d\u5386\u5217\u8868\u3002\u9996\u5148\u521d\u59cb\u5316\u7d22\u5f15<code>index<\/code>\u4e3a0\uff0c\u7136\u540e\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u6253\u5370\u5f53\u524d\u7d22\u5f15\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u7d22\u5f15\u9012\u589e1\u3002\u5faa\u73af\u7ee7\u7eed\u76f4\u5230\u7d22\u5f15\u8d85\u8fc7\u5217\u8868\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u904d\u5386\u5217\u8868\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u904d\u5386\u5217\u8868\u5e76\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u5e94\u7528\u67d0\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>[print(fruit) for fruit in fruits]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u904d\u5386\u5217\u8868\uff0c\u5e76\u5728\u6bcf\u6b21\u904d\u5386\u65f6\u6253\u5370\u5143\u7d20\u3002\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u5728\u529f\u80fd\u4e0a\u7b49\u540c\u4e8efor\u5faa\u73af\uff0c\u4f46\u5176\u8bed\u6cd5\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528enumerate\u51fd\u6570\u904d\u5386\u5217\u8868\u6570\u636e<\/h2>\n<\/p>\n<p><p><strong>\u4f7f\u7528enumerate\u51fd\u6570\u904d\u5386\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u548c\u5143\u7d20\u672c\u8eab\u3002\u8fd9\u5728\u9700\u8981\u7d22\u5f15\u548c\u5143\u7d20\u540c\u65f6\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u4f7f\u7528enumerate\u51fd\u6570\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>for index, fruit in enumerate(fruits):<\/p>\n<p>    print(f&#39;Index: {index}, Fruit: {fruit}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528enumerate\u51fd\u6570\u904d\u5386\u5217\u8868\uff0c\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u548c\u5143\u7d20\uff0c\u5e76\u8f93\u51fa\u5b83\u4eec\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>Index: 0, Fruit: apple<\/p>\n<p>Index: 1, Fruit: banana<\/p>\n<p>Index: 2, Fruit: cherry<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5217\u8868\u6570\u636e<\/h2>\n<\/p>\n<p><p><strong>Python\u4e2d\u7684\u8fed\u4ee3\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u8bbf\u95ee\u5bb9\u5668\u5143\u7d20\u7684\u7edf\u4e00\u63a5\u53e3\uff0c\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5217\u8868\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u83b7\u53d6\u8fed\u4ee3\u5668<\/strong><\/h2>\n<p>iterator = iter(fruits)<\/p>\n<h2><strong>\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        fruit = next(iterator)<\/p>\n<p>        print(fruit)<\/p>\n<p>    except StopIteration:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u5217\u8868\u7684\u8fed\u4ee3\u5668\uff0c\u7136\u540e\u4f7f\u7528next\u51fd\u6570\u9010\u4e2a\u83b7\u53d6\u5143\u7d20\u3002\u8fed\u4ee3\u5668\u904d\u5386\u5217\u8868\u65f6\uff0c\u5f53\u6240\u6709\u5143\u7d20\u90fd\u88ab\u8bbf\u95ee\u5b8c\u6bd5\uff0c\u4f1a\u5f15\u53d1StopIteration\u5f02\u5e38\uff0c\u6b64\u65f6\u8df3\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528map\u51fd\u6570\u904d\u5386\u5217\u8868\u6570\u636e<\/h2>\n<\/p>\n<p><p><strong>map\u51fd\u6570\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u4f7f\u7528map\u51fd\u6570\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>def print_fruit(fruit):<\/p>\n<p>    print(fruit)<\/p>\n<p>map(print_fruit, fruits)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6253\u5370\u5143\u7d20\u7684\u51fd\u6570<code>print_fruit<\/code>\uff0c\u7136\u540e\u4f7f\u7528map\u51fd\u6570\u5c06\u5176\u5e94\u7528\u4e8e\u6bcf\u4e2a\u5217\u8868\u5143\u7d20\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cmap\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u56e0\u6b64\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u624b\u52a8\u904d\u5386\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528zip\u51fd\u6570\u904d\u5386\u591a\u4e2a\u5217\u8868<\/h2>\n<\/p>\n<p><p><strong>zip\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u5217\u8868\u201c\u6253\u5305\u201d\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u540c\u65f6\u904d\u5386\u5b83\u4eec\u3002\u8fd9\u5728\u9700\u8981\u5e76\u884c\u64cd\u4f5c\u591a\u4e2a\u5217\u8868\u65f6\u975e\u5e38\u6709\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>colors = [&#39;red&#39;, &#39;yellow&#39;, &#39;red&#39;]<\/p>\n<h2><strong>\u4f7f\u7528zip\u51fd\u6570\u904d\u5386\u591a\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>for fruit, color in zip(fruits, colors):<\/p>\n<p>    print(f&#39;{fruit} is {color}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528zip\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u201c\u6253\u5305\u201d\u5728\u4e00\u8d77\uff0c\u7136\u540e\u5728for\u5faa\u73af\u4e2d\u540c\u65f6\u904d\u5386\u5b83\u4eec\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>apple is red<\/p>\n<p>banana is yellow<\/p>\n<p>cherry is red<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u4f7f\u7528collections.deque\u904d\u5386\u5217\u8868\u6570\u636e<\/h2>\n<\/p>\n<p><p><strong>collections\u6a21\u5757\u4e2d\u7684deque\uff08\u53cc\u7aef\u961f\u5217\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\u7528\u4e8e\u904d\u5386\u548c\u64cd\u4f5c\u5217\u8868\u3002deque\u652f\u6301\u7ebf\u7a0b\u5b89\u5168\u548c\u9ad8\u6548\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3adeque<\/strong><\/h2>\n<p>deque_fruits = deque(fruits)<\/p>\n<h2><strong>\u4f7f\u7528deque\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>while deque_fruits:<\/p>\n<p>    print(deque_fruits.popleft())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5217\u8868\u8f6c\u6362\u4e3adeque\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528popleft\u65b9\u6cd5\u904d\u5386\u5e76\u79fb\u9664\u6bcf\u4e2a\u5143\u7d20\u3002deque\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u53cc\u7aef\u64cd\u4f5c\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u4f7f\u7528itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u904d\u5386\u591a\u4e2a\u5217\u8868<\/h2>\n<\/p>\n<p><p><strong>itertools\u6a21\u5757\u4e2d\u7684chain\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u5217\u8868\u4e32\u8054\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u4f9d\u6b21\u904d\u5386\u5b83\u4eec\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import chain<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>vegetables = [&#39;carrot&#39;, &#39;broccoli&#39;, &#39;spinach&#39;]<\/p>\n<h2><strong>\u4f7f\u7528itertools.chain\u904d\u5386\u591a\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>for item in chain(fruits, vegetables):<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528chain\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u4e32\u8054\u5728\u4e00\u8d77\uff0c\u7136\u540e\u5728for\u5faa\u73af\u4e2d\u4f9d\u6b21\u904d\u5386\u5b83\u4eec\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>apple<\/p>\n<p>banana<\/p>\n<p>cherry<\/p>\n<p>carrot<\/p>\n<p>broccoli<\/p>\n<p>spinach<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u3001\u4f7f\u7528numpy\u904d\u5386\u5217\u8868\u6570\u636e<\/h2>\n<\/p>\n<p><p><strong>numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5c3d\u7ba1\u4e3b\u8981\u7528\u4e8e\u6570\u7ec4\u64cd\u4f5c\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u5217\u8868\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3anumpy\u6570\u7ec4<\/strong><\/h2>\n<p>np_fruits = np.array(fruits)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>for fruit in np_fruits:<\/p>\n<p>    print(fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5217\u8868\u8f6c\u6362\u4e3anumpy\u6570\u7ec4\uff0c\u7136\u540e\u5728for\u5faa\u73af\u4e2d\u904d\u5386\u6570\u7ec4\u3002numpy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u904d\u5386\u5217\u8868\u6570\u636e\uff0c\u5305\u62ecfor\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u3001enumerate\u51fd\u6570\u3001\u8fed\u4ee3\u5668\u3001map\u51fd\u6570\u3001zip\u51fd\u6570\u3001collections.deque\u3001itertools.chain\u548cnumpy\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>for\u5faa\u73af\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u904d\u5386\u4efb\u52a1\u3002<\/strong><\/li>\n<li><strong>while\u5faa\u73af\u9002\u7528\u4e8e\u9700\u8981\u624b\u52a8\u7ba1\u7406\u7d22\u5f15\u7684\u573a\u666f\u3002<\/strong><\/li>\n<li><strong>\u5217\u8868\u89e3\u6790\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u904d\u5386\u65b9\u5f0f\u3002<\/strong><\/li>\n<li><strong>enumerate\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u548c\u5143\u7d20\u672c\u8eab\u3002<\/strong><\/li>\n<li><strong>\u8fed\u4ee3\u5668\u548cmap\u51fd\u6570\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u904d\u5386\u65b9\u5f0f\u3002<\/strong><\/li>\n<li><strong>zip\u51fd\u6570\u9002\u7528\u4e8e\u5e76\u884c\u904d\u5386\u591a\u4e2a\u5217\u8868\u3002<\/strong><\/li>\n<li><strong>collections.deque\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u53cc\u7aef\u64cd\u4f5c\u80fd\u529b\u3002<\/strong><\/li>\n<li><strong>itertools.chain\u53ef\u4ee5\u5c06\u591a\u4e2a\u5217\u8868\u4e32\u8054\u5728\u4e00\u8d77\u904d\u5386\u3002<\/strong><\/li>\n<li><strong>numpy\u9002\u7528\u4e8e\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002<\/strong><\/li>\n<\/ul>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u6765\u904d\u5386\u5217\u8868\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u904d\u5386\u5217\u8868\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff0c\u8fd9\u6837\u53ef\u4ee5\u9010\u4e00\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u8fdb\u884c\u904d\u5386\uff0c\u6216\u8005\u5229\u7528\u5217\u8868\u7684\u5185\u7f6e\u51fd\u6570\u5982<code>enumerate()<\/code>\u6765\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002\u5bf9\u4e8e\u9700\u8981\u540c\u65f6\u64cd\u4f5c\u591a\u4e2a\u5217\u8868\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u5c06\u591a\u4e2a\u5217\u8868\u5408\u5e76\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u904d\u5386\u5217\u8868\u65f6\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570\u662f\u83b7\u53d6\u5143\u7d20\u7d22\u5f15\u7684\u6709\u6548\u65b9\u6cd5\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u548c\u503c\u7684\u5143\u7ec4\uff0c\u5141\u8bb8\u5728\u904d\u5386\u65f6\u540c\u65f6\u8bbf\u95ee\u5143\u7d20\u7684\u7d22\u5f15\u548c\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<code>for index, value in enumerate(my_list):<\/code>\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u5230\u6bcf\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u548c\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u904d\u5386\u5217\u8868\u65f6\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff1f<\/strong><br \/>\u5728\u904d\u5386\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408<code>if<\/code>\u8bed\u53e5\u6765\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u5728<code>for<\/code>\u5faa\u73af\u4e2d\u6dfb\u52a0\u4e00\u4e2a<code>if<\/code>\u6761\u4ef6\uff0c\u4ee5\u4fbf\u5bf9\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u904d\u5386\u4e00\u4e2a\u6570\u5b57\u5217\u8868\u5e76\u6253\u5370\u51fa\u5176\u4e2d\u7684\u5076\u6570\uff1a<code>for num in my_list: if num % 2 == 0: print(num)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u904d\u5386\u5217\u8868\u91cc\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b8c\u6210\uff0c\u4f8b\u5982\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u3001enum [&hellip;]","protected":false},"author":3,"featured_media":1055452,"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\/1055440"}],"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=1055440"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1055440\/revisions"}],"predecessor-version":[{"id":1055457,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1055440\/revisions\/1055457"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1055452"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1055440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1055440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1055440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}