{"id":1004875,"date":"2024-12-27T10:30:31","date_gmt":"2024-12-27T02:30:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004875.html"},"modified":"2024-12-27T10:30:35","modified_gmt":"2024-12-27T02:30:35","slug":"python%e5%a6%82%e4%bd%95%e7%ad%9b%e9%80%89%e5%88%97%e8%a1%a8%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1004875.html","title":{"rendered":"python\u5982\u4f55\u7b5b\u9009\u5217\u8868\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081638\/5b037fde-9162-4fcf-9c24-e5d510746266.webp\" alt=\"python\u5982\u4f55\u7b5b\u9009\u5217\u8868\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7b5b\u9009\u5217\u8868\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u3001lambda\u8868\u8fbe\u5f0f\u7b49\u65b9\u5f0f\u5b9e\u73b0<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u4e3a\u5e38\u89c1\u4e14\u7b80\u6d01\u7684\u65b9\u5f0f\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u7b5b\u9009\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u8bfb\u6027\u8f83\u9ad8\u3002\u6b64\u5916\uff0cfilter\u51fd\u6570\u7ed3\u5408lambda\u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u5217\u8868\u6570\u636e\u7b5b\u9009\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u521b\u5efa\u5217\u8868\u7684\u4e00\u79cd\u7b80\u6d01\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5217\u8868\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7b5b\u9009\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u4e3a <code>[expression for item in iterable if condition]<\/code>\u3002\u8868\u8fbe\u5f0f <code>expression<\/code> \u662f\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u7684\u64cd\u4f5c\uff0c\u800c <code>condition<\/code> \u5219\u662f\u7b5b\u9009\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u5927\u4e8e5\u7684\u6570\u5b57<\/p>\n<p>numbers = [1, 2, 3, 6, 8, 9, 10]<\/p>\n<p>filtered_numbers = [num for num in numbers if num &gt; 5]<\/p>\n<p>print(filtered_numbers)  # \u8f93\u51fa: [6, 8, 9, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>num &gt; 5<\/code> \u662f\u7b5b\u9009\u6761\u4ef6\uff0c\u53ea\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u624d\u4f1a\u88ab\u5305\u542b\u5728\u65b0\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u5176\u4ed6\u64cd\u4f5c<\/strong><\/li>\n<\/ol>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u7b5b\u9009\uff0c\u8fd8\u53ef\u4ee5\u5728\u7b5b\u9009\u7684\u540c\u65f6\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u5c06\u5217\u8868\u4e2d\u5927\u4e8e5\u7684\u6570\u5b57\u7ffb\u500d<\/p>\n<p>numbers = [1, 2, 3, 6, 8, 9, 10]<\/p>\n<p>modified_numbers = [num * 2 for num in numbers if num &gt; 5]<\/p>\n<p>print(modified_numbers)  # \u8f93\u51fa: [12, 16, 18, 20]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>num * 2<\/code> \u662f\u5bf9\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u8fdb\u884c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001filter\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>filter<\/code> \u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u7528\u4e8e\u8fc7\u6ee4\u5e8f\u5217\u7684\u51fd\u6570\u3002\u5b83\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u5e8f\u5217\u3002\u51fd\u6570\u4f1a\u88ab\u5e94\u7528\u5230\u5e8f\u5217\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u8fd4\u56deTrue\u7684\u5143\u7d20\u4f1a\u88ab\u4fdd\u7559\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u5927\u4e8e5\u7684\u6570\u5b57<\/p>\n<p>numbers = [1, 2, 3, 6, 8, 9, 10]<\/p>\n<p>filtered_numbers = list(filter(lambda x: x &gt; 5, numbers))<\/p>\n<p>print(filtered_numbers)  # \u8f93\u51fa: [6, 8, 9, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>lambda x: x &gt; 5<\/code> \u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u7528\u4e8e\u7b5b\u9009\u6761\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9 <code>filter<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u7b5b\u9009\u51fd\u6570<\/p>\n<p>def is_greater_than_five(x):<\/p>\n<p>    return x &gt; 5<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u7b5b\u9009<\/strong><\/h2>\n<p>numbers = [1, 2, 3, 6, 8, 9, 10]<\/p>\n<p>filtered_numbers = list(filter(is_greater_than_five, numbers))<\/p>\n<p>print(filtered_numbers)  # \u8f93\u51fa: [6, 8, 9, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u7b5b\u9009\u6761\u4ef6\u8f83\u4e3a\u590d\u6742\u65f6\u663e\u5f97\u66f4\u4e3a\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u9ad8\u7ea7\u7b5b\u9009<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\u6216\u9700\u8981\u8fdb\u884c\u590d\u6742\u7b5b\u9009\u7684\u573a\u666f\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u4e3a\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>NumPy\u6570\u7ec4\u7b5b\u9009<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u7684\u6570\u7ec4\u64cd\u4f5c\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>numbers = np.array([1, 2, 3, 6, 8, 9, 10])<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u5927\u4e8e5\u7684\u5143\u7d20<\/strong><\/h2>\n<p>filtered_numbers = numbers[numbers &gt; 5]<\/p>\n<p>print(filtered_numbers)  # \u8f93\u51fa: [6 8 9 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u591a\u6761\u4ef6\u7b5b\u9009<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u652f\u6301\u591a\u6761\u4ef6\u7b5b\u9009\uff0c\u8fd9\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>numbers = np.array([1, 2, 3, 6, 8, 9, 10])<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u5927\u4e8e5\u4e14\u662f\u5076\u6570\u7684\u5143\u7d20<\/strong><\/h2>\n<p>filtered_numbers = numbers[(numbers &gt; 5) &amp; (numbers % 2 == 0)]<\/p>\n<p>print(filtered_numbers)  # \u8f93\u51fa: [6 8 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001Pandas\u5e93\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u9002\u5408\u5bf9\u7ed3\u6784\u5316\u6570\u636e\u8fdb\u884c\u7b5b\u9009\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>Pandas\u63d0\u4f9b\u4e86 <code>Series<\/code> \u548c <code>DataFrame<\/code> \u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u7b5b\u9009\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;numbers&#39;: [1, 2, 3, 6, 8, 9, 10]})<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u5927\u4e8e5\u7684\u884c<\/strong><\/h2>\n<p>filtered_df = df[df[&#39;numbers&#39;] &gt; 5]<\/p>\n<p>print(filtered_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u590d\u6742\u6761\u4ef6\u7b5b\u9009<\/strong><\/li>\n<\/ol>\n<p><p>Pandas\u652f\u6301\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u8fdb\u884c\u590d\u6742\u6761\u4ef6\u7684\u7b5b\u9009\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;numbers&#39;: [1, 2, 3, 6, 8, 9, 10]})<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u5927\u4e8e5\u4e14\u662f\u5076\u6570\u7684\u884c<\/strong><\/h2>\n<p>filtered_df = df[(df[&#39;numbers&#39;] &gt; 5) &amp; (df[&#39;numbers&#39;] % 2 == 0)]<\/p>\n<p>print(filtered_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5404\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u5bf9\u5217\u8868\u8fdb\u884c\u7b5b\u9009\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7b5b\u9009\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u6216\u5faa\u73af\u7ed3\u6784\u6765\u7b5b\u9009\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u65b9\u4fbf\u5730\u7b5b\u9009\u51fa\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5982\u7b5b\u9009\u51fa\u6240\u6709\u5927\u4e8e10\u7684\u6570\u5b57\uff1a<code>filtered_list = [x for x in original_list if x &gt; 10]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u7b5b\u9009\u4efb\u52a1\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u7b5b\u9009\u65b9\u6cd5\uff1f<\/strong><br \/>\u5e38\u7528\u7684\u7b5b\u9009\u65b9\u6cd5\u6709\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u548clambda\u8868\u8fbe\u5f0f\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u5141\u8bb8\u60a8\u5feb\u901f\u6784\u5efa\u65b0\u5217\u8868\uff0c\u800cfilter\u51fd\u6570\u53ef\u4ee5\u4e0elambda\u7ed3\u5408\u4f7f\u7528\uff0c\u7075\u6d3b\u5904\u7406\u590d\u6742\u6761\u4ef6\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5faa\u73af\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7b5b\u9009\uff0c\u4f46\u901a\u5e38\u4e0d\u5982\u524d\u4e24\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u5bf9\u8c61\u7684\u5217\u8868\u7b5b\u9009\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u5bf9\u8c61\u7684\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u6765\u8fdb\u884c\u7b5b\u9009\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5bf9\u8c61\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u7b5b\u9009\u51fa\u7279\u5b9a\u6761\u4ef6\u7684\u5bf9\u8c61\uff1a<code>filtered_objects = [obj for obj in objects_list if obj.attribute == &#39;value&#39;]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u7b5b\u9009\u590d\u6742\u6570\u636e\u7ed3\u6784\u53d8\u5f97\u7b80\u5355\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u5bf9\u8c61\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7b5b\u9009\u5217\u8868\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u3001lambda\u8868\u8fbe\u5f0f\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5217\u8868 [&hellip;]","protected":false},"author":3,"featured_media":1004890,"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\/1004875"}],"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=1004875"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004875\/revisions"}],"predecessor-version":[{"id":1004892,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004875\/revisions\/1004892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004890"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}