{"id":1013908,"date":"2024-12-27T11:51:53","date_gmt":"2024-12-27T03:51:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1013908.html"},"modified":"2024-12-27T11:51:55","modified_gmt":"2024-12-27T03:51:55","slug":"python%e4%b8%ad%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e5%88%86%e5%89%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1013908.html","title":{"rendered":"python\u4e2d\u5217\u8868\u5982\u4f55\u5206\u5272"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094153\/082d26a5-508f-4d54-8e36-74b14c8a6eea.webp\" alt=\"python\u4e2d\u5217\u8868\u5982\u4f55\u5206\u5272\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u3001\u5217\u8868\u89e3\u6790\u3001itertools\u6a21\u5757\u7b49\u65b9\u6cd5\u8fdb\u884c\u5217\u8868\u5206\u5272\u3002\u5207\u7247\u662f\u4e00\u79cd\u5e38\u7528\u4e14\u7b80\u4fbf\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5b50\u5217\u8868\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u7d22\u5f15\u6765\u83b7\u53d6\u5217\u8868\u7684\u4e00\u90e8\u5206\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code>\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u83b7\u53d6\u4ece\u7b2c\u4e8c\u4e2a\u5143\u7d20\u5230\u7b2c\u4e94\u4e2a\u5143\u7d20\u7684\u5b50\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\uff1a<code>sub_list = my_list[1:5]<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u76f4\u89c2\uff0c\u800c\u4e14\u975e\u5e38\u7075\u6d3b\uff0c\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u6b65\u957f\u53c2\u6570\u8fdb\u4e00\u6b65\u63a7\u5236\u5207\u7247\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u5217\u8868\u5207\u7247<\/p>\n<\/p>\n<p><p>\u5217\u8868\u5207\u7247\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u4f60\u901a\u8fc7\u6307\u5b9a\u5f00\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\u6765\u83b7\u53d6\u5217\u8868\u7684\u4e00\u90e8\u5206\u3002\u5207\u7247\u64cd\u4f5c\u7b26<code>:<\/code>\u7684\u57fa\u672c\u5f62\u5f0f\u4e3a<code>list[start:end:step]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u5f00\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff0c<code>step<\/code>\u662f\u6b65\u957f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u662f\u901a\u8fc7\u7d22\u5f15\u6765\u83b7\u53d6\u5217\u8868\u7684\u4e00\u90e8\u5206\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u8d1f\u6570\u7d22\u5f15\u8868\u793a\u4ece\u5217\u8868\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\u3002<code>list[start:end]<\/code>\u8868\u793a\u4ece<code>start<\/code>\u5f00\u59cb\uff0c\u5230<code>end<\/code>\u7ed3\u675f\uff08\u4e0d\u5305\u62ec<code>end<\/code>\uff09\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>sub_list = my_list[2:5]  # [3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6b65\u957f\u7684\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u8fd8\u53ef\u4ee5\u5305\u62ec\u6b65\u957f\u53c2\u6570\uff0c\u8868\u793a\u6bcf\u9694\u591a\u5c11\u4e2a\u5143\u7d20\u53d6\u4e00\u4e2a\u503c\u3002<code>list[start:end:step]<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>sub_list = my_list[::2]  # [1, 3, 5, 7, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u4ece\u4e00\u4e2a\u5217\u8868\u751f\u6210\u53e6\u4e00\u4e2a\u5217\u8868\u3002\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\u548c\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u5217\u8868\u5206\u5272\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u5217\u8868\u89e3\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5b50\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>sub_list = [x for x in my_list if x &gt; 5]  # [6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408\u6761\u4ef6\u7684\u5217\u8868\u89e3\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u751f\u6210\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u5b50\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>sub_list = [x for x in my_list if x % 2 == 0]  # [2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>itertools<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u64cd\u4f5c\u8fed\u4ee3\u5668\uff0c\u5176\u4e2d<code>islice<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5207\u7247\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>islice<\/code>\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>islice<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u4ece\u8fed\u4ee3\u5668\u4e2d\u63d0\u53d6\u5b50\u5e8f\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import islice<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>sub_list = list(islice(my_list, 2, 5))  # [3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6279\u91cf\u5206\u5272\u5217\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>itertools<\/code>\u53ef\u4ee5\u5c06\u5217\u8868\u5206\u5272\u6210\u591a\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import zip_longest<\/p>\n<p>def chunks(iterable, size):<\/p>\n<p>    it = iter(iterable)<\/p>\n<p>    return zip_longest(*[it] * size)<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>for chunk in chunks(my_list, 3):<\/p>\n<p>    print(list(chunk))  # [1, 2, 3], [4, 5, 6], [7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5229\u7528NumPy\u8fdb\u884c\u9ad8\u7ea7\u5206\u5272<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u5305\u62ec\u6570\u7ec4\u5207\u7247\u548c\u5206\u5272\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>NumPy\u6570\u7ec4\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u5207\u7247\u529f\u80fd\u7c7b\u4f3c\u4e8ePython\u5217\u8868\uff0c\u4f46\u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<\/p>\n<p>sub_array = my_array[2:5]  # array([3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>np.split<\/code>\u8fdb\u884c\u5206\u5272<\/strong><\/p>\n<\/p>\n<p><p><code>np.split<\/code>\u51fd\u6570\u5141\u8bb8\u5c06\u6570\u7ec4\u5206\u5272\u6210\u591a\u4e2a\u5b50\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<\/p>\n<p>sub_arrays = np.array_split(my_array, 3)<\/p>\n<p>for arr in sub_arrays:<\/p>\n<p>    print(arr)  # array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9e\u73b0\u7075\u6d3b\u5206\u5272<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u5185\u7f6e\u7684\u529f\u80fd\u53ef\u80fd\u65e0\u6cd5\u5b8c\u5168\u6ee1\u8db3\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5206\u5272\u903b\u8f91\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u56fa\u5b9a\u5927\u5c0f\u5757\u5206\u5272<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06\u5217\u8868\u5206\u5272\u6210\u56fa\u5b9a\u5927\u5c0f\u7684\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def split_list(my_list, chunk_size):<\/p>\n<p>    return [my_list[i:i + chunk_size] for i in range(0, len(my_list), chunk_size)]<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>print(split_list(my_list, 3))  # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6839\u636e\u6761\u4ef6\u5206\u5272<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\uff0c\u5c06\u5217\u8868\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def split_by_condition(my_list, condition):<\/p>\n<p>    true_list = [x for x in my_list if condition(x)]<\/p>\n<p>    false_list = [x for x in my_list if not condition(x)]<\/p>\n<p>    return true_list, false_list<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>even_list, odd_list = split_by_condition(my_list, lambda x: x % 2 == 0)<\/p>\n<p>print(even_list)  # [2, 4, 6, 8]<\/p>\n<p>print(odd_list)  # [1, 3, 5, 7, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u5730\u5728Python\u4e2d\u8fdb\u884c\u5217\u8868\u5206\u5272\u3002\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e00\u4e2a\u5217\u8868\u5206\u5272\u6210\u591a\u4e2a\u5b50\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u5206\u5272\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4, 5, 6]<\/code>\uff0c\u5e76\u5e0c\u671b\u6bcf\u4e24\u4e2a\u5143\u7d20\u4e3a\u4e00\u7ec4\u5206\u5272\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u5207\u7247\uff1a<code>[my_list[i:i + 2] for i in range(0, len(my_list), 2)]<\/code>\u3002\u8fd9\u6837\u5c31\u80fd\u5f97\u5230<code>[[1, 2], [3, 4], [5, 6]]<\/code>\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u6ca1\u6709\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5206\u5272\u5217\u8868\uff1f<\/strong><br \/>\u786e\u5b9e\u6709\uff0cPython\u7684<code>more-itertools<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>chunked<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u5217\u8868\u5206\u5272\u6210\u6307\u5b9a\u5927\u5c0f\u7684\u5757\u3002\u4f7f\u7528\u65b9\u6cd5\u4e3a<code>from more_itertools import chunked<\/code>\uff0c\u7136\u540e\u53ef\u4ee5\u8c03\u7528<code>list(chunked(my_list, n))<\/code>\uff0c\u5176\u4e2d<code>n<\/code>\u662f\u4f60\u5e0c\u671b\u7684\u6bcf\u4e2a\u5b50\u5217\u8868\u7684\u5927\u5c0f\u3002<\/p>\n<p><strong>\u5206\u5272\u5217\u8868\u65f6\u5982\u4f55\u5904\u7406\u5269\u4f59\u5143\u7d20\uff1f<\/strong><br \/>\u5728\u5206\u5272\u5217\u8868\u65f6\uff0c\u5982\u679c\u5217\u8868\u7684\u957f\u5ea6\u4e0d\u662f\u6307\u5b9a\u5757\u5927\u5c0f\u7684\u6574\u6570\u500d\uff0c\u5269\u4f59\u7684\u5143\u7d20\u53ef\u4ee5\u9009\u62e9\u653e\u5728\u6700\u540e\u4e00\u4e2a\u5b50\u5217\u8868\u4e2d\u3002\u4f7f\u7528\u5207\u7247\u6216<code>more-itertools<\/code>\u5e93\u7684\u529f\u80fd\uff0c\u5269\u4f59\u5143\u7d20\u4f1a\u81ea\u52a8\u88ab\u5305\u542b\u5728\u6700\u540e\u7684\u5b50\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5217\u8868<code>[1, 2, 3, 4, 5]<\/code>\u548c\u5757\u5927\u5c0f\u4e3a2\uff0c\u7ed3\u679c\u5c06\u662f<code>[[1, 2], [3, 4], [5]]<\/code>\uff0c\u5176\u4e2d<code>5<\/code>\u4f5c\u4e3a\u5355\u72ec\u7684\u5143\u7d20\u88ab\u4fdd\u7559\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u3001\u5217\u8868\u89e3\u6790\u3001itertools\u6a21\u5757\u7b49\u65b9\u6cd5\u8fdb\u884c\u5217\u8868\u5206\u5272\u3002\u5207\u7247\u662f\u4e00\u79cd\u5e38\u7528\u4e14\u7b80\u4fbf\u7684\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1013914,"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\/1013908"}],"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=1013908"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013908\/revisions"}],"predecessor-version":[{"id":1013917,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013908\/revisions\/1013917"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1013914"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1013908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1013908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1013908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}