{"id":1143333,"date":"2025-01-08T22:49:09","date_gmt":"2025-01-08T14:49:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1143333.html"},"modified":"2025-01-08T22:49:11","modified_gmt":"2025-01-08T14:49:11","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba%e5%88%97%e8%a1%a8%e5%89%8dn%e4%b8%aa%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143333.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u53d6\u51fa\u5217\u8868\u524dn\u4e2a\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181012\/b0a45157-213b-4a87-85e8-37419d96072e.webp\" alt=\"python\u4e2d\u5982\u4f55\u53d6\u51fa\u5217\u8868\u524dn\u4e2a\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53d6\u51fa\u5217\u8868\u524dn\u4e2a\u5143\u7d20<\/strong>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c<strong>\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528itertools\u6a21\u5757<\/strong>\u7b49\u65b9\u6cd5\u90fd\u662f\u5e38\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5217\u8868\u5207\u7247\u64cd\u4f5c\u975e\u5e38\u5f3a\u5927\u4e14\u7b80\u6d01\uff0c\u80fd\u591f\u5feb\u901f\u3001\u76f4\u63a5\u5730\u83b7\u53d6\u5217\u8868\u7684\u524dn\u4e2a\u5143\u7d20\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868<code>my_list<\/code>\uff0c\u6211\u4eec\u53ea\u9700\u4f7f\u7528<code>my_list[:n]<\/code>\u8fd9\u79cd\u7b80\u5355\u7684\u8bed\u6cd5\u5373\u53ef\u83b7\u53d6\u524dn\u4e2a\u5143\u7d20\u3002\u5207\u7247\u64cd\u4f5c\u4e0d\u4ec5\u5728\u6027\u80fd\u4e0a\u4f18\u5f02\uff0c\u800c\u4e14\u4ee3\u7801\u4e5f\u975e\u5e38\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u5207\u7247\u662fPython\u4e2d\u83b7\u53d6\u5b50\u5217\u8868\u7684\u4e00\u79cd\u5feb\u6377\u65b9\u5f0f\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868<code>my_list<\/code>\uff0c\u8981\u53d6\u51fa\u524dn\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_list[:n]<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>n = 3<\/p>\n<p>result = my_list[:n]<\/p>\n<p>print(result)  # \u8f93\u51fa [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5207\u7247\u64cd\u4f5c\u5728Python\u5185\u90e8\u662f\u4ee5C\u8bed\u8a00\u5b9e\u73b0\u7684\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u6267\u884c\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5217\u8868\u5207\u7247\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u66f4\u52a0\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>n = 3<\/p>\n<p>result = []<\/p>\n<p>for i in range(n):<\/p>\n<p>    result.append(my_list[i])<\/p>\n<p>print(result)  # \u8f93\u51fa [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7565\u663e\u5197\u957f\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u4f8b\u5982\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528itertools\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u6548\u7684\u51fd\u6570\u5de5\u5177\uff0c<code>islice<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u8fed\u4ee3\u5668\u7684\u524dn\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import islice<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>n = 3<\/p>\n<p>result = list(islice(my_list, n))<\/p>\n<p>print(result)  # \u8f93\u51fa [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>itertools.islice<\/code>\u53ef\u4ee5\u6709\u6548\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u56e0\u4e3a\u5b83\u662f\u60f0\u6027\u6c42\u503c\u7684\uff0c\u4e0d\u4f1a\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><h4>1\u3001\u5217\u8868\u5207\u7247<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u7b80\u6d01\u3001\u76f4\u63a5\u3001\u6613\u8bfb\u3001\u6267\u884c\u901f\u5ea6\u5feb\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u5bf9\u975e\u5e38\u5927\u7684\u5217\u8868\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u5f00\u9500\u5927\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5faa\u73af<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u4ee3\u7801\u5197\u957f\uff0c\u6267\u884c\u901f\u5ea6\u76f8\u5bf9\u8f83\u6162\u3002<\/p>\n<\/p>\n<p><h4>3\u3001itertools.islice<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u60f0\u6027\u6c42\u503c\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u989d\u5916\u5bfc\u5165\u6a21\u5757\uff0c\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5904\u7406\u7684\u662f\u975e\u5e38\u5927\u89c4\u6a21\u7684\u6570\u636e\uff0c\u5207\u7247\u64cd\u4f5c\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u5f00\u9500\u8fc7\u5927\uff0c\u6b64\u65f6<code>itertools.islice<\/code>\u4f1a\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u8981\u6c42\u975e\u5e38\u9ad8\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u5bf9\u4e0d\u540c\u65b9\u6cd5\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\uff0c\u4ece\u800c\u9009\u62e9\u6700\u4f18\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>setup = &quot;my_list = list(range(1000000)); n = 500&quot;<\/p>\n<p>stmt_slice = &quot;result = my_list[:n]&quot;<\/p>\n<p>stmt_loop = &quot;&quot;&quot;<\/p>\n<p>result = []<\/p>\n<p>for i in range(n):<\/p>\n<p>    result.append(my_list[i])<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>stmt_islice = &quot;&quot;&quot;<\/p>\n<p>from itertools import islice<\/p>\n<p>result = list(islice(my_list, n))<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>print(timeit.timeit(stmt_slice, setup=setup, number=1000))<\/p>\n<p>print(timeit.timeit(stmt_loop, setup=setup, number=1000))<\/p>\n<p>print(timeit.timeit(stmt_islice, setup=setup, number=1000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u91cf\u5316\u4e0d\u540c\u65b9\u6cd5\u7684\u6267\u884c\u65f6\u95f4\uff0c\u4ece\u800c\u9009\u62e9\u6700\u9002\u5408\u5b9e\u9645\u5e94\u7528\u7684\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d6\u51fa\u5217\u8868\u524dn\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c<strong>\u5217\u8868\u5207\u7247\u64cd\u4f5c\u662f\u6700\u4e3a\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u9700\u8981\u66f4\u591a\u7075\u6d3b\u6027\u65f6\uff0c<strong>\u5faa\u73af\u548citertools\u6a21\u5757<\/strong>\u4e5f\u63d0\u4f9b\u4e86\u6709\u529b\u7684\u89e3\u51b3\u65b9\u6848\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u4f18\u5316\u6027\u80fd\u548c\u8d44\u6e90\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u83b7\u53d6\u5217\u8868\u7684\u524dn\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u83b7\u53d6\u5217\u8868\u7684\u524dn\u4e2a\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5217\u8868<code>my_list<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>my_list[:n]<\/code>\u6765\u83b7\u53d6\u524dn\u4e2a\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u5982\u679cn\u8d85\u8fc7\u4e86\u5217\u8868\u7684\u957f\u5ea6\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5f53n\u7684\u503c\u5927\u4e8e\u5217\u8868\u7684\u957f\u5ea6\u65f6\uff0c\u5207\u7247\u64cd\u4f5c\u4e0d\u4f1a\u62a5\u9519\uff0c\u53cd\u800c\u4f1a\u8fd4\u56de\u6574\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>my_list[:n]<\/code>\u4f1a\u8fd4\u56de\u6240\u6709\u5143\u7d20\uff0c\u800c\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u8fd9\u4f7f\u5f97\u5207\u7247\u64cd\u4f5c\u975e\u5e38\u5b89\u5168\u4e14\u7075\u6d3b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u83b7\u53d6\u524dn\u4e2a\u5143\u7d20\u7684\u540c\u65f6\uff0c\u5bf9\u8fd9\u4e9b\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u53ef\u4ee5\u5148\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u63d0\u53d6\u524dn\u4e2a\u5143\u7d20\u3002\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u4f8b\u5982\uff1a<code>sorted(my_list)[:n]<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4f60\u5f97\u5230\u7684\u662f\u524dn\u4e2a\u6700\u5c0f\u6216\u6700\u5927\u7684\u5143\u7d20\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6392\u5e8f\u7684\u987a\u5e8f\u3002<\/p>\n<p><strong>\u5728\u83b7\u53d6\u524dn\u4e2a\u5143\u7d20\u65f6\uff0c\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u5207\u7247\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u83b7\u53d6\u524dn\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff1a<code>[my_list[i] for i in range(min(n, len(my_list)))]<\/code>\uff0c\u8fd9\u4f1a\u786e\u4fdd\u5728n\u5927\u4e8e\u5217\u8868\u957f\u5ea6\u65f6\u4e0d\u4f1a\u8d85\u51fa\u8303\u56f4\u3002\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5bf9\u5143\u7d20\u8fdb\u884c\u989d\u5916\u5904\u7406\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53d6\u51fa\u5217\u8868\u524dn\u4e2a\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528itertools\u6a21\u5757\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1143338,"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\/1143333"}],"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=1143333"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143333\/revisions"}],"predecessor-version":[{"id":1143339,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143333\/revisions\/1143339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1143338"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}