{"id":1005243,"date":"2024-12-27T10:33:59","date_gmt":"2024-12-27T02:33:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1005243.html"},"modified":"2024-12-27T10:34:01","modified_gmt":"2024-12-27T02:34:01","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%ad%90%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1005243.html","title":{"rendered":"Python\u5982\u4f55\u521b\u5efa\u5b50\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081837\/5a3447f6-3e1c-4923-a79d-ed2b816ac759.webp\" alt=\"Python\u5982\u4f55\u521b\u5efa\u5b50\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u5b50\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u5207\u7247\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5185\u7f6e\u51fd\u6570filter\u3002<\/strong> \u5217\u8868\u5207\u7247\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\uff0c\u53ef\u4ee5\u76f4\u63a5\u4ece\u539f\u5217\u8868\u4e2d\u63d0\u53d6\u51fa\u4e00\u4e2a\u5b50\u5217\u8868\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u5141\u8bb8\u901a\u8fc7\u6761\u4ef6\u521b\u5efa\u66f4\u4e3a\u590d\u6742\u7684\u5b50\u5217\u8868\uff0c\u9002\u5408\u9700\u8981\u5bf9\u539f\u5217\u8868\u8fdb\u884c\u8fc7\u6ee4\u6216\u53d8\u6362\u7684\u60c5\u51b5\u3002\u800c\u4f7f\u7528filter\u51fd\u6570\u5219\u53ef\u4ee5\u5728\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u4e0b\u751f\u6210\u5b50\u5217\u8868\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5217\u8868\u5207\u7247<\/p>\n<\/p>\n<p><p>\u5217\u8868\u5207\u7247\u662fPython\u4e2d\u5904\u7406\u5217\u8868\u7684\u57fa\u672c\u529f\u80fd\u4e4b\u4e00\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u6307\u5b9a\u7d22\u5f15\u8303\u56f4\u6765\u63d0\u53d6\u5217\u8868\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u7840\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5217\u8868\u5207\u7247\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>list[start:end]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u62ec\u8be5\u7d22\u5f15\u7684\u5143\u7d20\uff09\u3002\u5982\u679c\u7701\u7565<code>start<\/code>\uff0c\u5219\u4ece\u5217\u8868\u5f00\u5934\u5f00\u59cb\uff1b\u5982\u679c\u7701\u7565<code>end<\/code>\uff0c\u5219\u4e00\u76f4\u5207\u5230\u5217\u8868\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = original_list[1:4]  # \u8f93\u51fa [2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6b65\u957f<\/strong><\/li>\n<\/ol>\n<p><p>\u5207\u7247\u8fd8\u53ef\u4ee5\u6307\u5b9a\u6b65\u957f\uff0c\u901a\u8fc7<code>list[start:end:step]<\/code>\u7684\u8bed\u6cd5\u6765\u5b9e\u73b0\u3002\u6b65\u957f\u51b3\u5b9a\u4e86\u4ece\u8d77\u59cb\u7d22\u5f15\u5230\u7ed3\u675f\u7d22\u5f15\u4e4b\u95f4\u7684\u5143\u7d20\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = original_list[::2]  # \u8f93\u51fa [1, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8d1f\u7d22\u5f15<\/strong><\/li>\n<\/ol>\n<p><p>Python\u7684\u5217\u8868\u652f\u6301\u8d1f\u7d22\u5f15\uff0c\u53ef\u4ee5\u4ece\u5217\u8868\u7684\u672b\u5c3e\u5411\u524d\u8ba1\u6570\uff0c\u8fd9\u5728\u9700\u8981\u53cd\u5411\u63d0\u53d6\u5143\u7d20\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = original_list[-3:]  # \u8f93\u51fa [4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5b50\u5217\u8868\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u6761\u4ef6\u8fc7\u6ee4\u6216\u6620\u5c04\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6761\u4ef6\u8fc7\u6ee4<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u52a0\u5165\u6761\u4ef6\uff0c\u53ef\u4ee5\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = [x for x in original_list if x % 2 == 0]  # \u8f93\u51fa [2, 4, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5143\u7d20\u53d8\u6362<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u8fc7\u6ee4\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u8fd8\u53ef\u4ee5\u5bf9\u5217\u8868\u5143\u7d20\u8fdb\u884c\u53d8\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = [x * 2 for x in original_list]  # \u8f93\u51fa [2, 4, 6, 8, 10, 12]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u591a\u91cd\u6761\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u4f7f\u7528\u591a\u4e2a\u6761\u4ef6\u6765\u7b5b\u9009\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = [x for x in original_list if x % 2 == 0 and x &gt; 2]  # \u8f93\u51fa [4, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528filter\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>filter<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u8fc7\u6ee4\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u7840\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p><code>filter(function, iterable)<\/code>\uff0c\u5176\u4e2d<code>function<\/code>\u662f\u4e00\u4e2a\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\uff0c<code>iterable<\/code>\u662f\u8981\u8fc7\u6ee4\u7684\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = list(filter(lambda x: x % 2 == 0, original_list))  # \u8f93\u51fa [2, 4, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u786e\u5b9a\u8fc7\u6ee4\u6761\u4ef6\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9<code>filter<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_even(n):<\/p>\n<p>    return n % 2 == 0<\/p>\n<p>original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = list(filter(is_even, original_list))  # \u8f93\u51fa [2, 4, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408<\/strong><\/li>\n<\/ol>\n<p><p><code>filter<\/code>\u53ef\u4ee5\u4e0e<code>map<\/code>\u7b49\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = list(map(lambda x: x * 2, filter(lambda x: x % 2 == 0, original_list)))  # \u8f93\u51fa [4, 8, 12]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528itertools\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7684\u8fed\u4ee3\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u590d\u6742\u7684\u5b50\u5217\u8868\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>islice\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p><code>itertools.islice(iterable, start, stop[, step])<\/code>\u53ef\u4ee5\u7528\u4e8e\u4ece\u8fed\u4ee3\u5668\u4e2d\u5207\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import islice<\/p>\n<p>original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = list(islice(original_list, 1, 5, 2))  # \u8f93\u51fa [2, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>compress\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p><code>itertools.compress(data, selectors)<\/code>\u6839\u636e\u9009\u62e9\u5668\u7684\u5e03\u5c14\u503c\u6765\u8fc7\u6ee4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import compress<\/p>\n<p>original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>selectors = [1, 0, 1, 0, 1, 0]<\/p>\n<p>sub_list = list(compress(original_list, selectors))  # \u8f93\u51fa [1, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>filterfalse\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p><code>itertools.filterfalse(predicate, iterable)<\/code>\u7c7b\u4f3c\u4e8e<code>filter<\/code>\uff0c\u4f46\u8fd4\u56de\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import filterfalse<\/p>\n<p>original_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>sub_list = list(filterfalse(lambda x: x % 2 == 0, original_list))  # \u8f93\u51fa [1, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u7ed3\u5408Numpy\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u503c\u5217\u8868\uff0c\u5c24\u5176\u662f\u9700\u8981\u8fdb\u884c\u5927\u91cf\u8ba1\u7b97\u7684\u573a\u5408\uff0c<code>Numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<\/strong><\/li>\n<\/ol>\n<p><p>Numpy\u6570\u7ec4\u652f\u6301\u5e03\u5c14\u7d22\u5f15\uff0c\u53ef\u4ee5\u901a\u8fc7\u5e03\u5c14\u6761\u4ef6\u9009\u62e9\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>original_array = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<p>sub_array = original_array[original_array % 2 == 0]  # \u8f93\u51fa array([2, 4, 6])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528Numpy\u7684\u5207\u7247<\/strong><\/li>\n<\/ol>\n<p><p>\u4e0ePython\u5217\u8868\u7c7b\u4f3c\uff0cNumpy\u6570\u7ec4\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>original_array = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<p>sub_array = original_array[1:4]  # \u8f93\u51fa array([2, 3, 4])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9ad8\u7ea7\u7d22\u5f15<\/strong><\/li>\n<\/ol>\n<p><p>Numpy\u652f\u6301\u9ad8\u7ea7\u7d22\u5f15\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u7d22\u5f15\u5217\u8868\u6765\u63d0\u53d6\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>original_array = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<p>indices = [0, 2, 4]<\/p>\n<p>sub_array = original_array[indices]  # \u8f93\u51fa array([1, 3, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u7ed3\u5408Pandas\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u9886\u57df\uff0cPandas\u5e93\u4ee5\u5176\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u800c\u95fb\u540d\uff0c\u5e38\u7528\u4e8e\u5904\u7406\u6570\u636e\u6846\u4e2d\u7684\u5217\u6216\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u6761\u4ef6\u8fc7\u6ee4<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u6765\u9009\u62e9\u6570\u636e\u6846\u4e2d\u7684\u884c\u6216\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;numbers&#39;: [1, 2, 3, 4, 5, 6]})<\/p>\n<p>sub_df = df[df[&#39;numbers&#39;] % 2 == 0]  # \u8f93\u51fa\u5305\u542b\u5076\u6570\u7684\u884c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528loc\u548ciloc<\/strong><\/li>\n<\/ol>\n<p><p><code>loc<\/code>\u548c<code>iloc<\/code>\u7528\u4e8e\u57fa\u4e8e\u6807\u7b7e\u548c\u6574\u6570\u7d22\u5f15\u9009\u62e9\u884c\u6216\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;numbers&#39;: [1, 2, 3, 4, 5, 6]})<\/p>\n<p>sub_df = df.loc[1:4]  # \u8f93\u51fa\u7b2c2\u5230\u7b2c5\u884c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528query\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p><code>query<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u6765\u7b5b\u9009\u6570\u636e\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;numbers&#39;: [1, 2, 3, 4, 5, 6]})<\/p>\n<p>sub_df = df.query(&#39;numbers % 2 == 0&#39;)  # \u8f93\u51fa\u5305\u542b\u5076\u6570\u7684\u884c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u591a\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u521b\u5efa\u5b50\u5217\u8868\u6216\u5b50\u6570\u636e\u96c6\uff0c\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\u3002\u65e0\u8bba\u662f\u57fa\u7840\u7684\u5217\u8868\u64cd\u4f5c\uff0c\u8fd8\u662f\u501f\u52a9\u9ad8\u7ea7\u5e93\u5982Numpy\u548cPandas\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u96c6\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5b50\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u6765\u521b\u5efa\u5b50\u5217\u8868\u3002\u5207\u7247\u5141\u8bb8\u60a8\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u63d0\u53d6\u51fa\u4e00\u4e2a\u7279\u5b9a\u7684\u90e8\u5206\u3002\u8bed\u6cd5\u4e3a <code>list[start:end]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u5f00\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u62ec\u8be5\u7d22\u5f15\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5217\u8868 <code>my_list = [1, 2, 3, 4, 5]<\/code>\uff0c\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u524d\u4e09\u4e2a\u5143\u7d20\u7684\u5b50\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>sub_list = my_list[0:3]<\/code>\uff0c\u7ed3\u679c\u5c06\u662f <code>[1, 2, 3]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u5b50\u5217\u8868\uff1f<\/strong><br \/>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5b50\u5217\u8868\u3002\u901a\u8fc7\u6761\u4ef6\u7b5b\u9009\uff0c\u60a8\u53ef\u4ee5\u63d0\u53d6\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5217\u8868 <code>numbers = [1, 2, 3, 4, 5, 6]<\/code>\uff0c\u5e76\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u53ea\u5305\u542b\u5076\u6570\u7684\u5b50\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>even_numbers = [num for num in numbers if num % 2 == 0]<\/code>\uff0c\u8fd9\u5c06\u751f\u6210 <code>[2, 4, 6]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5b50\u5217\u8868\u4f1a\u5f71\u54cd\u539f\u59cb\u5217\u8868\u5417\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b50\u5217\u8868\u7684\u521b\u5efa\u65b9\u5f0f\u4e0d\u540c\u4f1a\u5f71\u54cd\u539f\u59cb\u5217\u8868\u3002\u4f7f\u7528\u5207\u7247\u521b\u5efa\u7684\u5b50\u5217\u8868\u662f\u539f\u59cb\u5217\u8868\u7684\u4e00\u4e2a\u62f7\u8d1d\uff0c\u56e0\u6b64\u5bf9\u5b50\u5217\u8868\u7684\u4fee\u6539\u4e0d\u4f1a\u5f71\u54cd\u539f\u59cb\u5217\u8868\u3002\u7136\u800c\uff0c\u5982\u679c\u60a8\u901a\u8fc7\u5f15\u7528\uff08\u4f8b\u5982\u4f7f\u7528 <code>my_list[1:4]<\/code> \u76f4\u63a5\u64cd\u4f5c\uff09\u83b7\u53d6\u4e00\u4e2a\u5b50\u5217\u8868\u5e76\u4fee\u6539\u5176\u4e2d\u7684\u5143\u7d20\uff0c\u539f\u59cb\u5217\u8868\u662f\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u7684\u3002\u5bf9\u539f\u59cb\u5217\u8868\u672c\u8eab\u7684\u64cd\u4f5c\u5c06\u4e0d\u4f1a\u53cd\u6620\u5728\u5b50\u5217\u8868\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u5b50\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u5207\u7247\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5185\u7f6e\u51fd\u6570filter\u3002 \u5217\u8868\u5207\u7247\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1005246,"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\/1005243"}],"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=1005243"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005243\/revisions"}],"predecessor-version":[{"id":1005249,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005243\/revisions\/1005249"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1005246"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1005243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1005243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1005243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}