{"id":1068158,"date":"2024-12-31T16:40:53","date_gmt":"2024-12-31T08:40:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068158.html"},"modified":"2024-12-31T16:40:56","modified_gmt":"2024-12-31T08:40:56","slug":"python%e4%b8%adfor%e5%be%aa%e7%8e%af%e5%a6%82%e4%bd%95%e9%9a%94%e4%b8%80%e5%8f%96%e4%b8%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068158.html","title":{"rendered":"python\u4e2dfor\u5faa\u73af\u5982\u4f55\u9694\u4e00\u53d6\u4e00"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/cdbb6809-79cb-4b21-b36e-718696989228.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2dfor\u5faa\u73af\u5982\u4f55\u9694\u4e00\u53d6\u4e00\" \/><\/p>\n<blockquote>\n<p><p>search(&quot;python\u4e2dfor\u5faa\u73af\u5982\u4f55\u9694\u4e00\u53d6\u4e00&quot;)<\/p>\n<\/p>\n<\/blockquote>\n<p><p><strong>\u5728 Python \u4e2d\uff0c\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u5b9e\u73b0\u201c\u9694\u4e00\u53d6\u4e00\u201d\u64cd\u4f5c\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 <code>range()<\/code> \u51fd\u6570\u8bbe\u5b9a\u6b65\u957f\u3001\u5229\u7528\u5217\u8868\u5207\u7247\u3001\u4ee5\u53ca\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u901a\u8fc7 <code>range()<\/code> \u51fd\u6570\u8bbe\u5b9a\u6b65\u957f<\/strong> \u662f\u6700\u76f4\u89c2\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528 <code>range()<\/code> \u51fd\u6570\u5b9e\u73b0\u9694\u4e00\u53d6\u4e00<\/strong><\/p>\n<\/p>\n<p><p><code>range()<\/code> \u51fd\u6570\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u7684\u6570\u5b57\u5e8f\u5217\u3002\u901a\u8fc7\u8bbe\u5b9a\u6b65\u957f\u53c2\u6570 <code>step<\/code>\uff0c\u53ef\u4ee5\u63a7\u5236\u5faa\u73af\u7684\u589e\u91cf\uff0c\u4ece\u800c\u5b9e\u73b0\u9694\u4e00\u53d6\u4e00\u7684\u6548\u679c\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">range(start, stop, step)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>start<\/code>\uff1a\u5e8f\u5217\u7684\u8d77\u59cb\u503c\uff0c\u9ed8\u8ba4\u4e3a 0\u3002<\/li>\n<li><code>stop<\/code>\uff1a\u5e8f\u5217\u7684\u7ed3\u675f\u503c\uff08\u4e0d\u5305\u542b\u8be5\u503c\uff09\u3002<\/li>\n<li><code>step<\/code>\uff1a\u6b65\u957f\uff0c\u9ed8\u8ba4\u4e3a 1\u3002<\/li>\n<\/ul>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868 <code>my_list<\/code>\uff0c\u5e0c\u671b\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50, 60]<\/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><p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>10<\/p>\n<p>30<\/p>\n<p>50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>range(0, len(my_list), 2)<\/code> \u751f\u6210\u4e86\u4ece 0 \u5230 <code>len(my_list)<\/code> \u7684\u7d22\u5f15\u5e8f\u5217\uff0c\u6b65\u957f\u4e3a 2\u3002\u5faa\u73af\u904d\u5386\u8fd9\u4e9b\u7d22\u5f15\uff0c\u5e76\u6253\u5370\u5bf9\u5e94\u7684\u5217\u8868\u5143\u7d20\uff0c\u5b9e\u73b0\u4e86\u9694\u4e00\u53d6\u4e00\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u5207\u7247\u5b9e\u73b0\u9694\u4e00\u53d6\u4e00<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u5207\u7247\u662f Python \u4e2d\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5141\u8bb8\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u3001\u7ed3\u675f\u548c\u6b65\u957f\u6765\u83b7\u53d6\u5217\u8868\u7684\u5b50\u96c6\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list[start:stop:step]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>start<\/code>\uff1a\u5207\u7247\u7684\u8d77\u59cb\u7d22\u5f15\uff0c\u9ed8\u8ba4\u4e3a 0\u3002<\/li>\n<li><code>stop<\/code>\uff1a\u5207\u7247\u7684\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u542b\u8be5\u7d22\u5f15\u5904\u7684\u5143\u7d20\uff09\u3002<\/li>\n<li><code>step<\/code>\uff1a\u6b65\u957f\uff0c\u9ed8\u8ba4\u4e3a 1\u3002<\/li>\n<\/ul>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50, 60]<\/p>\n<p>sub_list = my_list[::2]<\/p>\n<p>for item in sub_list:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>10<\/p>\n<p>30<\/p>\n<p>50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_list[::2]<\/code> \u8fd4\u56de\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5305\u542b\u539f\u5217\u8868\u4e2d\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u7684\u5b50\u96c6\u3002\u7136\u540e\uff0c\u901a\u8fc7 <code>for<\/code> \u5faa\u73af\u904d\u5386\u8fd9\u4e2a\u5b50\u5217\u8868\uff0c\u5b9e\u73b0\u4e86\u9694\u4e00\u53d6\u4e00\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u5b9e\u73b0\u9694\u4e00\u53d6\u4e00<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5728 <code>for<\/code> \u5faa\u73af\u4e2d\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u6765\u5b9e\u73b0\u9694\u4e00\u53d6\u4e00\u7684\u6548\u679c\u3002\u901a\u8fc7 <code>enumerate()<\/code> \u51fd\u6570\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5e76\u6839\u636e\u7d22\u5f15\u7684\u5947\u5076\u6027\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50, 60]<\/p>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    if index % 2 == 0:<\/p>\n<p>        print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>10<\/p>\n<p>30<\/p>\n<p>50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>enumerate(my_list)<\/code> \u8fd4\u56de\u6bcf\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002\u901a\u8fc7\u5224\u65ad\u7d22\u5f15\u662f\u5426\u4e3a\u5076\u6570\uff0c\u51b3\u5b9a\u662f\u5426\u6253\u5370\u8be5\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u9694\u4e00\u53d6\u4e00\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6027\u80fd\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6027\u80fd\u8003\u8651\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406<\/strong>\uff1a\u5bf9\u4e8e\u5927\u578b\u5217\u8868\uff0c\u4f7f\u7528 <code>range()<\/code> \u51fd\u6570\u6216\u5217\u8868\u5207\u7247\u7684\u65b9\u6cd5\u66f4\u4e3a\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4eec\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u590d\u6742\u6761\u4ef6\u8fc7\u6ee4<\/strong>\uff1a\u5982\u679c\u9700\u8981\u6839\u636e\u590d\u6742\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\uff0c\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u7684\u65b9\u6cd5\u66f4\u4e3a\u7075\u6d3b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p><strong>\u6027\u80fd\u6d4b\u8bd5\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>large_list = list(range(1000000))<\/p>\n<h2><strong>\u4f7f\u7528 range() \u51fd\u6570<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>for i in range(0, len(large_list), 2):<\/p>\n<p>    _ = large_list[i]<\/p>\n<p>print(&quot;\u4f7f\u7528 range() \u51fd\u6570\u8017\u65f6:&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u5207\u7247<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>sub_list = large_list[::2]<\/p>\n<p>for item in sub_list:<\/p>\n<p>    _ = item<\/p>\n<p>print(&quot;\u4f7f\u7528\u5217\u8868\u5207\u7247\u8017\u65f6:&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u4f7f\u7528\u6761\u4ef6\u5224\u65ad<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>for index, value in enumerate(large_list):<\/p>\n<p>    if index % 2 == 0:<\/p>\n<p>        _ = value<\/p>\n<p>print(&quot;\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u8017\u65f6:&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u53ef\u80fd\u7684\u8f93\u51fa\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>\u4f7f\u7528 range() \u51fd\u6570\u8017\u65f6: 0.05 \u79d2<\/p>\n<p>\u4f7f\u7528\u5217\u8868\u5207\u7247\u8017\u65f6: 0.03 \u79d2<\/p>\n<p>\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u8017\u65f6: 0.10 \u79d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u4e0a\u8ff0\u6d4b\u8bd5\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u4f7f\u7528 <code>range()<\/code> \u51fd\u6570\u548c\u5217\u8868\u5207\u7247\u7684\u65b9\u6cd5\u6027\u80fd\u66f4\u4f18\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u5b9e\u73b0\u9694\u4e00\u53d6\u4e00\u7684\u64cd\u4f5c\u6709\u591a\u79cd\u65b9\u6cd5\u3002<strong>\u901a\u8fc7 <code>range()<\/code> \u51fd\u6570\u8bbe\u5b9a\u6b65\u957f<\/strong> \u662f\u6700\u76f4\u89c2\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002\u5217\u8868\u5207\u7247\u65b9\u6cd5\u540c\u6837\u7b80\u6d01\u9ad8\u6548\uff0c\u800c\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u7684\u65b9\u6cd5\u5219\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u7b5b\u9009\u6761\u4ef6\u7684\u60c5\u51b5\u3002\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>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u53d6\u4e00\u4e2a\u7684\u6548\u679c\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u6765\u5b9e\u73b0\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u53d6\u4e00\u4e2a\u7684\u6548\u679c\u3002\u901a\u8fc7\u4f7f\u7528\u6b65\u957f\u53c2\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u5217\u8868\u6216\u5176\u4ed6\u5e8f\u5217\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list[::2]<\/code> \u5c06\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u679c\u4f60\u9700\u8981\u5728\u5faa\u73af\u4e2d\u5904\u7406\u8fd9\u4e9b\u5143\u7d20\uff0c\u53ef\u4ee5\u7ed3\u5408 <code>for<\/code> \u5faa\u73af\u4e0e\u5207\u7247\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<p><strong>\u4f7f\u7528for\u5faa\u73af\u9694\u53d6\u5143\u7d20\u7684\u6027\u80fd\u5982\u4f55\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528for\u5faa\u73af\u9694\u53d6\u5143\u7d20\u7684\u6027\u80fd\u901a\u5e38\u975e\u5e38\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5f53\u5e8f\u5217\u8f83\u5927\u65f6\u3002Python\u7684\u5207\u7247\u64cd\u4f5c\u5728\u5185\u90e8\u662f\u7ecf\u8fc7\u4f18\u5316\u7684\uff0c\u56e0\u6b64\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5207\u7247\u80fd\u6bd4\u624b\u52a8\u5faa\u73af\u66f4\u5feb\u3002\u7136\u800c\uff0c\u5f53\u5904\u7406\u7684\u5e8f\u5217\u975e\u5e38\u5e9e\u5927\u65f6\uff0c\u8003\u8651\u5185\u5b58\u4f7f\u7528\u548c\u6027\u80fd\u662f\u5f88\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u5b9e\u73b0\u9694\u53d6\u5143\u7d20\u7684\u529f\u80fd\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u5217\u8868\u4e4b\u5916\uff0cPython\u4e2d\u7684\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u548c\u96c6\u5408\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u6216\u76f8\u4f3c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u9694\u53d6\u5143\u7d20\u7684\u529f\u80fd\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u5207\u7247\u8bed\u6cd5\u6765\u63d0\u53d6\u6bcf\u9694\u4e00\u4e2a\u5b57\u7b26\uff0c\u800c\u5bf9\u4e8e\u96c6\u5408\uff0c\u7531\u4e8e\u5176\u65e0\u5e8f\u6027\uff0c\u65e0\u6cd5\u76f4\u63a5\u5e94\u7528\u5207\u7247\uff0c\u4f46\u53ef\u4ee5\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u5143\u7ec4\u540e\u518d\u8fdb\u884c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"search(&quot;python\u4e2dfor\u5faa\u73af\u5982\u4f55\u9694\u4e00\u53d6\u4e00&quot;) \u5728 Python \u4e2d\uff0c\u4f7f\u7528 fo [&hellip;]","protected":false},"author":3,"featured_media":1068164,"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\/1068158"}],"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=1068158"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068158\/revisions"}],"predecessor-version":[{"id":1068167,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068158\/revisions\/1068167"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068164"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}