{"id":977872,"date":"2024-12-27T06:33:11","date_gmt":"2024-12-26T22:33:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977872.html"},"modified":"2024-12-27T06:33:14","modified_gmt":"2024-12-26T22:33:14","slug":"python%e5%a6%82%e4%bd%95%e5%a4%a7%e4%ba%8e%e5%85%83%e7%b4%a0%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/977872.html","title":{"rendered":"python\u5982\u4f55\u5927\u4e8e\u5143\u7d20\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203126\/1fdcb18b-7797-4f8c-84a9-8ac0379ce539.webp\" alt=\"python\u5982\u4f55\u5927\u4e8e\u5143\u7d20\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u3001\u5faa\u73af\u904d\u5386\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u5f0f\u6765\u627e\u5230\u5927\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20<\/strong>\uff0c<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u548c\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u3002\u901a\u8fc7for\u5faa\u73af\uff0c\u60a8\u53ef\u4ee5\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u4f7f\u7528if\u8bed\u53e5\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5927\u4e8e\u76ee\u6807\u503c\uff1b\u5217\u8868\u63a8\u5bfc\u5f0f\u5219\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4ee3\u7801\u66f4\u4e3a\u7b80\u6d01\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u3002<\/p>\n<\/p>\n<p><p><strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u4e14\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\u3002<\/strong> \u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f <code>[expression for item in iterable if condition]<\/code>\u3002\u5728\u8fd9\u4e2a\u8bed\u6cd5\u7ed3\u6784\u4e2d\uff0c<code>expression<\/code> \u662f\u5bf9 <code>item<\/code> \u7684\u5904\u7406\u8fc7\u7a0b\uff0c<code>iterable<\/code> \u662f\u8981\u904d\u5386\u7684\u5e8f\u5217\uff0c<code>condition<\/code> \u662f\u4e00\u4e2a\u53ef\u9009\u7684\u8fc7\u6ee4\u6761\u4ef6\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u8fc7\u6ee4\u6389\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u7279\u522b\u662f\u5728\u5bf9\u5217\u8868\u8fdb\u884c\u7b80\u5355\u7684\u7b5b\u9009\u548c\u8f6c\u6362\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5728Python\u4e2d\u67e5\u627e\u5927\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20\u7684\u591a\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528FOR\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cfor\u5faa\u73af\u662f\u4e00\u79cd\u57fa\u672c\u4e14\u5e38\u7528\u7684\u8fed\u4ee3\u5de5\u5177\u3002\u901a\u8fc7for\u5faa\u73af\uff0c\u60a8\u53ef\u4ee5\u904d\u5386\u4e00\u4e2a\u5217\u8868\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u5faa\u73af\u4f53\u4e2d\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5224\u65ad\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u67e5\u627e\u5927\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20\uff0c\u9700\u8981\u4e24\u4e2a\u6b65\u9aa4\uff1a\u904d\u5386\u5217\u8868\u548c\u5224\u65ad\u5143\u7d20\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]<\/p>\n<p>threshold = 5<\/p>\n<p>greater_than_threshold = []<\/p>\n<p>for number in numbers:<\/p>\n<p>    if number &gt; threshold:<\/p>\n<p>        greater_than_threshold.append(number)<\/p>\n<p>print(greater_than_threshold)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>greater_than_threshold<\/code> \u5217\u8868\u5c06\u5b58\u50a8\u6240\u6709\u5927\u4e8e5\u7684\u5143\u7d20\u3002\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6e05\u6670\u5730\u770b\u5230\u6bcf\u4e2a\u5143\u7d20\u7684\u5904\u7406\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f18\u5316\u548c\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u5927\u7684\u6570\u636e\u96c6\uff0cfor\u5faa\u73af\u53ef\u80fd\u4e0d\u662f\u6700\u4f18\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u96c6\uff0cfor\u5faa\u73af\u7684\u6027\u80fd\u5f71\u54cd\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\u3002\u4e3a\u4e86\u4f18\u5316\u4ee3\u7801\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c<\/strong>\uff1a\u5728\u5faa\u73af\u4f53\u4e2d\uff0c\u5c3d\u91cf\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u548c\u64cd\u4f5c\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/li>\n<li><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong>\uff1a\u5982\u679c\u7ed3\u679c\u5217\u8868\u4e0d\u9700\u8981\u5728\u5185\u5b58\u4e2d\u4fdd\u5b58\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u66ff\u4ee3\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u7684\u4e00\u79cd\u7b80\u6d01\u4e14\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u5b83\u7684\u8bed\u6cd5\u7b80\u6d01\u800c\u76f4\u89c2\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5217\u8868\u8fc7\u6ee4\u548c\u8f6c\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u67e5\u627e\u5927\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20\uff0c\u53ea\u9700\u4e00\u884c\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]<\/p>\n<p>threshold = 5<\/p>\n<p>greater_than_threshold = [number for number in numbers if number &gt; threshold]<\/p>\n<p>print(greater_than_threshold)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5927\u4e8e\u6307\u5b9a\u503c\u7684\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2. \u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4e3b\u8981\u4f18\u70b9\u5728\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002\u76f8\u6bd4\u4e8efor\u5faa\u73af\uff0c\u5b83\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\uff0c\u4f7f\u4ee3\u7801\u66f4\u4e3a\u7b80\u6d01\u3002\u6b64\u5916\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u6267\u884c\u6548\u7387\u901a\u5e38\u8f83\u9ad8\uff0c\u56e0\u4e3a\u5b83\u5728\u5e95\u5c42\u8fdb\u884c\u4e86\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528FILTER\u51fd\u6570<\/h3>\n<\/p>\n<p><p>filter\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u8fc7\u6ee4\u5e8f\u5217\u3002\u5b83\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u5e8f\u5217\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fc7\u6ee4\u540e\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528filter\u51fd\u6570\u67e5\u627e\u5927\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5224\u65ad\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]<\/p>\n<p>threshold = 5<\/p>\n<p>def is_greater_than_threshold(number):<\/p>\n<p>    return number &gt; threshold<\/p>\n<p>greater_than_threshold = list(filter(is_greater_than_threshold, numbers))<\/p>\n<p>print(greater_than_threshold)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528lambda\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u7b80\u5316\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528lambda\u51fd\u6570\u66ff\u4ee3\u663e\u5f0f\u5b9a\u4e49\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">greater_than_threshold = list(filter(lambda number: number &gt; threshold, numbers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u540c\u65f6\u4fdd\u6301\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528NUMPY\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u5904\u7406\u6570\u7ec4\u7684\u529f\u80fd\u3002\u4f7f\u7528NumPy\uff0c\u60a8\u53ef\u4ee5\u9ad8\u6548\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u7b5b\u9009\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u5e03\u5c14\u7d22\u5f15\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u7b5b\u9009\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([1, 3, 5, 7, 9, 2, 4, 6, 8, 10])<\/p>\n<p>threshold = 5<\/p>\n<p>greater_than_threshold = numbers[numbers &gt; threshold]<\/p>\n<p>print(greater_than_threshold)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. NumPy\u7684\u4f18\u52bf<\/h4>\n<\/p>\n<p><p>NumPy\u7684\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u5176\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u80fd\u529b\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0cNumPy\u7684\u6027\u80fd\u8fdc\u8fdc\u8d85\u8fc7\u539f\u751f\u7684Python\u5217\u8868\u64cd\u4f5c\u3002\u540c\u65f6\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\uff0c\u65b9\u4fbf\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528PANDAS\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5f3a\u5927\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86DataFrame\u548cSeries\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u7b5b\u9009\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Series<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u5b58\u50a8\u5728Pandas\u7684Series\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u8fdb\u884c\u7b5b\u9009\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>numbers = pd.Series([1, 3, 5, 7, 9, 2, 4, 6, 8, 10])<\/p>\n<p>threshold = 5<\/p>\n<p>greater_than_threshold = numbers[numbers &gt; threshold]<\/p>\n<p>print(greater_than_threshold)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528DataFrame<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u5b58\u50a8\u5728DataFrame\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408DataFrame\u7684\u6761\u4ef6\u9009\u62e9\u529f\u80fd\u8fdb\u884c\u7b5b\u9009\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;numbers&#39;: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]})<\/p>\n<p>threshold = 5<\/p>\n<p>greater_than_threshold = df[df[&#39;numbers&#39;] &gt; threshold]<\/p>\n<p>print(greater_than_threshold)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u67e5\u627e\u5927\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5217\u8868\u64cd\u4f5c\uff0cfor\u5faa\u73af\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1b\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u548cPandas\u7b49\u5e93\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u89c4\u6a21\u3001\u64cd\u4f5c\u590d\u6742\u6027\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u7b49\u56e0\u7d20\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5efa\u8bae\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u7279\u70b9\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u4ee5\u8fbe\u5230\u6700\u4f73\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u627e\u5927\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8fc7\u6ee4\u51fd\u6570\u6765\u67e5\u627e\u5927\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5217\u8868\u548c\u4e00\u4e2a\u9608\u503c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u83b7\u53d6\u6240\u6709\u5927\u4e8e\u9608\u503c\u7684\u5143\u7d20\uff1a<\/p>\n<pre><code class=\"language-python\">my_list = [1, 5, 8, 10, 15]\nthreshold = 7\nresult = [x for x in my_list if x &gt; threshold]\nprint(result)  # \u8f93\u51fa: [8, 10, 15]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u7b5b\u9009\u5927\u4e8e\u67d0\u4e2a\u503c\u7684\u5143\u7d20\uff1f<\/strong><br \/>Python\u7684<code>filter()<\/code>\u51fd\u6570\u975e\u5e38\u9002\u5408\u8fd9\u4e2a\u76ee\u7684\u3002\u4f60\u53ef\u4ee5\u7ed3\u5408\u4e00\u4e2alambda\u51fd\u6570\u6765\u7b5b\u9009\u51fa\u5927\u4e8e\u67d0\u4e2a\u503c\u7684\u5143\u7d20\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">my_list = [3, 6, 9, 12, 15]\nthreshold = 8\nresult = list(filter(lambda x: x &gt; threshold, my_list))\nprint(result)  # \u8f93\u51fa: [9, 12, 15]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u5982\u4f55\u63d0\u9ad8\u67e5\u627e\u5927\u4e8e\u5143\u7d20\u503c\u7684\u6548\u7387\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u8003\u8651\u4f7f\u7528NumPy\u5e93\u3002NumPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u80fd\u591f\u5feb\u901f\u7b5b\u9009\u51fa\u5927\u4e8e\u67d0\u4e2a\u503c\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.array([1, 5, 8, 10, 15])\nthreshold = 7\nresult = data[data &gt; threshold]\nprint(result)  # \u8f93\u51fa: [ 8 10 15]\n<\/code><\/pre>\n<p>\u4f7f\u7528NumPy\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u3001\u5faa\u73af\u904d\u5386\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u5f0f\u6765\u627e\u5230\u5927\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528for [&hellip;]","protected":false},"author":3,"featured_media":977887,"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\/977872"}],"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=977872"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977872\/revisions"}],"predecessor-version":[{"id":977888,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977872\/revisions\/977888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/977887"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}