{"id":1107284,"date":"2025-01-08T16:48:11","date_gmt":"2025-01-08T08:48:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1107284.html"},"modified":"2025-01-08T16:48:14","modified_gmt":"2025-01-08T08:48:14","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e7%bb%99%e5%ae%9a%e5%88%97%e8%a1%a8%e4%b8%ad%e5%b0%8f%e4%ba%8e%e6%9f%90%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1107284.html","title":{"rendered":"python\u5982\u4f55\u6c42\u7ed9\u5b9a\u5217\u8868\u4e2d\u5c0f\u4e8e\u67d0\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071352\/cba6a6ff-2b4a-4704-81fd-8f64680f6bf0.webp\" alt=\"python\u5982\u4f55\u6c42\u7ed9\u5b9a\u5217\u8868\u4e2d\u5c0f\u4e8e\u67d0\u4e2a\u6570\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<\/p>\n<p><strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u3001for\u5faa\u73af\u7b49\u65b9\u6cd5\u6765\u6c42\u7ed9\u5b9a\u5217\u8868\u4e2d\u5c0f\u4e8e\u67d0\u4e2a\u6570\u7684\u5143\u7d20<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u80fd\u591f\u5feb\u901f\u751f\u6210\u6240\u9700\u7684\u5b50\u5217\u8868\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u5355\u6613\u61c2\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u8f83\u9ad8\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u5217\u8868\u7b5b\u9009\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868 <code>nums<\/code> \u548c\u4e00\u4e2a\u76ee\u6807\u6570 <code>x<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u83b7\u53d6\u5c0f\u4e8e <code>x<\/code> \u7684\u6240\u6709\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nums = [10, 20, 30, 5, 15, 25]<\/p>\n<p>x = 20<\/p>\n<p>result = [num for num in nums if num &lt; x]<\/p>\n<p>print(result)  # \u8f93\u51fa: [10, 5, 15]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u8fbe\u5f0f\u6765\u5b9a\u4e49\u5217\u8868\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u4f7f\u7528\u4e00\u4e2a\u53ef\u9009\u7684\u8fc7\u6ee4\u6761\u4ef6\u6765\u9009\u62e9\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[expression for item in iterable if condition]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>expression<\/code> \u662f\u65b0\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u7684\u8868\u8fbe\u5f0f\u3002<\/li>\n<li><code>item<\/code> \u662f\u4ece <code>iterable<\/code> \u4e2d\u53d6\u51fa\u7684\u5143\u7d20\u3002<\/li>\n<li><code>condition<\/code> \u662f\u4e00\u4e2a\u53ef\u9009\u7684\u8fc7\u6ee4\u6761\u4ef6\uff0c\u53ea\u6709\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u5143\u7d20\u624d\u4f1a\u88ab\u5305\u542b\u5728\u65b0\u5217\u8868\u4e2d\u3002<\/li>\n<\/ul>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868 <code>nums<\/code> \u548c\u4e00\u4e2a\u76ee\u6807\u6570 <code>x<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u83b7\u53d6\u5c0f\u4e8e <code>x<\/code> \u7684\u6240\u6709\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nums = [10, 20, 30, 5, 15, 25]<\/p>\n<p>x = 20<\/p>\n<p>result = [num for num in nums if num &lt; x]<\/p>\n<p>print(result)  # \u8f93\u51fa: [10, 5, 15]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\u3002\u5b83\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20 <code>num<\/code> \u4e0e <code>x<\/code> \u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c <code>num<\/code> \u5c0f\u4e8e <code>x<\/code>\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001filter\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>filter<\/code> \u51fd\u6570\u662f Python \u5185\u7f6e\u7684\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u8fc7\u6ee4\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p><code>filter<\/code> \u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filter(function, iterable)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>function<\/code> \u662f\u4e00\u4e2a\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5b9a\u4e49\u8fc7\u6ee4\u6761\u4ef6\u3002<\/li>\n<li><code>iterable<\/code> \u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>filter<\/code> \u51fd\u6570\u548c\u4e00\u4e2a\u7b80\u5355\u7684\u533f\u540d\u51fd\u6570\uff08lambda\uff09\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nums = [10, 20, 30, 5, 15, 25]<\/p>\n<p>x = 20<\/p>\n<p>result = list(filter(lambda num: num &lt; x, nums))<\/p>\n<p>print(result)  # \u8f93\u51fa: [10, 5, 15]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>filter<\/code> \u51fd\u6570\u4f1a\u8c03\u7528\u533f\u540d\u51fd\u6570 <code>lambda num: num &lt; x<\/code> \u6765\u7b5b\u9009 <code>nums<\/code> \u4e2d\u5c0f\u4e8e <code>x<\/code> \u7684\u5143\u7d20\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001for\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u662f\u4e00\u79cd\u8f83\u4e3a\u4f20\u7edf\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u540c\u6837\u6709\u6548\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p><code>for<\/code> \u5faa\u73af\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for item in iterable:<\/p>\n<p>    if condition:<\/p>\n<p>        # do something<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6765\u5b9e\u73b0\u540c\u6837\u7684\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nums = [10, 20, 30, 5, 15, 25]<\/p>\n<p>x = 20<\/p>\n<p>result = []<\/p>\n<p>for num in nums:<\/p>\n<p>    if num &lt; x:<\/p>\n<p>        result.append(num)<\/p>\n<p>print(result)  # \u8f93\u51fa: [10, 5, 15]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>for<\/code> \u5faa\u73af\u4f1a\u904d\u5386 <code>nums<\/code> \u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5c0f\u4e8e <code>x<\/code> \u7684\u5143\u7d20\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><p>NumPy \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u4f7f\u7528 NumPy \u53ef\u4ee5\u4f7f\u7b5b\u9009\u64cd\u4f5c\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5NumPy<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5 NumPy\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 NumPy \u6570\u7ec4\u548c\u5e03\u5c14\u7d22\u5f15\u6765\u5b9e\u73b0\u7b5b\u9009\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>nums = np.array([10, 20, 30, 5, 15, 25])<\/p>\n<p>x = 20<\/p>\n<p>result = nums[nums &lt; x]<\/p>\n<p>print(result)  # \u8f93\u51fa: [10  5 15]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5217\u8868 <code>nums<\/code> \u8f6c\u6362\u4e3a NumPy \u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15 <code>nums &lt; x<\/code> \u6765\u7b5b\u9009\u51fa\u6240\u6709\u5c0f\u4e8e <code>x<\/code> \u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528Pandas\u5e93<\/h2>\n<\/p>\n<p><p>Pandas \u662f\u4e00\u4e2a\u6570\u636e\u5206\u6790\u5e93\uff0c\u652f\u6301\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\u3002\u4f7f\u7528 Pandas \u53ef\u4ee5\u4f7f\u7b5b\u9009\u64cd\u4f5c\u66f4\u52a0\u65b9\u4fbf\u548c\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5Pandas<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5 Pandas\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 Pandas \u7684 <code>Series<\/code> \u5bf9\u8c61\u548c\u5e03\u5c14\u7d22\u5f15\u6765\u5b9e\u73b0\u7b5b\u9009\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>nums = pd.Series([10, 20, 30, 5, 15, 25])<\/p>\n<p>x = 20<\/p>\n<p>result = nums[nums &lt; x]<\/p>\n<p>print(result)  # \u8f93\u51fa: [0    10<\/p>\n<p>              #       3     5<\/p>\n<p>              #       4    15<\/p>\n<p>              #       dtype: int64]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5217\u8868 <code>nums<\/code> \u8f6c\u6362\u4e3a Pandas <code>Series<\/code> \u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15 <code>nums &lt; x<\/code> \u6765\u7b5b\u9009\u51fa\u6240\u6709\u5c0f\u4e8e <code>x<\/code> \u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528itertools\u5e93<\/h2>\n<\/p>\n<p><p><code>itertools<\/code> \u662f\u4e00\u4e2a\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u6784\u5efa\u548c\u64cd\u4f5c\u8fed\u4ee3\u5668\u7684\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>itertools.filterfalse<\/code> \u51fd\u6570\u6765\u7b5b\u9009\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5itertools<\/h3>\n<\/p>\n<p><p><code>itertools<\/code> \u662f Python \u7684\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>itertools.filterfalse<\/code> \u51fd\u6570\u6765\u7b5b\u9009\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>nums = [10, 20, 30, 5, 15, 25]<\/p>\n<p>x = 20<\/p>\n<p>result = list(itertools.filterfalse(lambda num: num &gt;= x, nums))<\/p>\n<p>print(result)  # \u8f93\u51fa: [10, 5, 15]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>itertools.filterfalse<\/code> \u51fd\u6570\u4f1a\u8c03\u7528\u533f\u540d\u51fd\u6570 <code>lambda num: num &gt;= x<\/code> \u6765\u7b5b\u9009 <code>nums<\/code> \u4e2d\u5c0f\u4e8e <code>x<\/code> \u7684\u5143\u7d20\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\uff0c\u4f7f\u4ee3\u7801\u66f4\u5177\u590d\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def function_name(parameters):<\/p>\n<p>    # function body<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u7b5b\u9009\u5c0f\u4e8e <code>x<\/code> \u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def filter_less_than(nums, x):<\/p>\n<p>    return [num for num in nums if num &lt; x]<\/p>\n<p>nums = [10, 20, 30, 5, 15, 25]<\/p>\n<p>x = 20<\/p>\n<p>result = filter_less_than(nums, x)<\/p>\n<p>print(result)  # \u8f93\u51fa: [10, 5, 15]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>filter_less_than<\/code> \u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5217\u8868 <code>nums<\/code> \u548c\u4e00\u4e2a\u76ee\u6807\u6570 <code>x<\/code>\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5c0f\u4e8e <code>x<\/code> \u7684\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6c42\u7ed9\u5b9a\u5217\u8868\u4e2d\u5c0f\u4e8e\u67d0\u4e2a\u6570\u7684\u5143\u7d20\uff0c\u5305\u62ec\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u3001for\u5faa\u73af\u3001NumPy\u3001Pandas\u3001itertools \u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong> \u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u7b5b\u9009\u64cd\u4f5c\uff1b<strong>filter\u51fd\u6570<\/strong> \u63d0\u4f9b\u4e86\u4e00\u79cd\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\u7684\u7b5b\u9009\u65b9\u5f0f\uff1b<strong>for\u5faa\u73af<\/strong> \u662f\u4e00\u79cd\u4f20\u7edf\u4e14\u6613\u4e8e\u7406\u89e3\u7684\u65b9\u6cd5\uff1b<strong>NumPy<\/strong> \u548c <strong>Pandas<\/strong> \u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u548c\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\uff1b<strong>itertools<\/strong> \u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u8fed\u4ee3\u5668\u5de5\u5177\uff1b<strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong> \u5219\u4f7f\u4ee3\u7801\u66f4\u5177\u590d\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u7b5b\u9009\u64cd\u4f5c\uff0c\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\u4f7f\u7528Python\u7b5b\u9009\u51fa\u5c0f\u4e8e\u7279\u5b9a\u503c\u7684\u5217\u8868\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005filter\u51fd\u6570\u6765\u7b5b\u9009\u51fa\u5c0f\u4e8e\u67d0\u4e2a\u7279\u5b9a\u503c\u7684\u5143\u7d20\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u8bed\u6cd5\u6bd4\u8f83\u7b80\u6d01\uff0c\u4f8b\u5982\uff1a<code>[x for x in my_list if x &lt; threshold]<\/code>\uff0c\u5176\u4e2d<code>my_list<\/code>\u662f\u4f60\u7684\u539f\u59cb\u5217\u8868\uff0c<code>threshold<\/code>\u662f\u4f60\u8bbe\u5b9a\u7684\u9608\u503c\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u9ad8\u6548\uff0c\u800c\u4e14\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u8981\u7edf\u8ba1\u5c0f\u4e8e\u67d0\u4e2a\u6570\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5185\u7f6e\u7684<code>len()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>len([x for x in my_list if x &lt; threshold])<\/code>\u53ef\u4ee5\u76f4\u63a5\u5f97\u5230\u5c0f\u4e8e\u9608\u503c\u7684\u5143\u7d20\u6570\u91cf\u3002\u8fd9\u6837\u53ef\u4ee5\u5feb\u901f\u5f97\u5230\u6240\u9700\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u800c\u65e0\u9700\u989d\u5916\u7684\u5faa\u73af\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u5143\u7d20\u7684\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6765\u786e\u4fdd\u53ea\u5bf9\u6570\u5b57\u8fdb\u884c\u6bd4\u8f83\u3002\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u5143\u7d20\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a<code>[x for x in my_list if isinstance(x, (int, float)) and x &lt; threshold]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u80fd\u6709\u6548\u907f\u514d\u56e0\u7c7b\u578b\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1f<\/strong><br \/>\u5bf9\u4e8e\u521d\u5b66\u8005\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684for\u5faa\u73af\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">result = []\nfor x in my_list:\n    if x &lt; threshold:\n        result.append(x)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u76f4\u89c2\u6613\u61c2\uff0c\u5c24\u5176\u9002\u5408\u521a\u63a5\u89e6\u7f16\u7a0b\u7684\u7528\u6237\uff0c\u540c\u65f6\u4e5f\u80fd\u5e2e\u52a9\u4ed6\u4eec\u7406\u89e3\u57fa\u672c\u7684\u63a7\u5236\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u3001for\u5faa\u73af\u7b49\u65b9\u6cd5\u6765\u6c42\u7ed9\u5b9a\u5217\u8868\u4e2d\u5c0f\u4e8e\u67d0\u4e2a\u6570\u7684\u5143\u7d20 [&hellip;]","protected":false},"author":3,"featured_media":1107292,"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\/1107284"}],"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=1107284"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107284\/revisions"}],"predecessor-version":[{"id":1107296,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1107284\/revisions\/1107296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1107292"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1107284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1107284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1107284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}