{"id":2792,"date":"2025-06-01T17:02:28","date_gmt":"2025-06-04T09:44:28","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2792"},"modified":"2025-06-04T17:02:28","modified_gmt":"2025-06-04T09:44:28","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84filter%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84filter%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 filter() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, filter() \u51fd\u6578, Python \u6559\u5b78, filter \u51fd\u6578\u7bc4\u4f8b\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 filter() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`filter()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4f86\u904e\u6ffe\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u8fd4\u56de\u4e00\u500b\u7531\u7b26\u5408\u689d\u4ef6\u7684\u5143\u7d20\u7d44\u6210\u7684\u65b0\u5e8f\u5217\u3002\u9019\u500b\u51fd\u6578\u63a5\u53d7\u5169\u500b\u53c3\u6578\uff1a\u4e00\u500b\u662f\u7528\u4f86\u6aa2\u67e5\u689d\u4ef6\u7684\u51fd\u6578\uff0c\u53e6\u4e00\u500b\u662f\u8981\u904e\u6ffe\u7684\u5e8f\u5217\u3002\u900f\u904e `filter()`\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u7be9\u9078\u51fa\u7b26\u5408\u7279\u5b9a\u689d\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<p>### filter() \u51fd\u6578\u57fa\u672c\u7528\u6cd5<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7bc4\u4f8b\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u6578\u5b57\u5e8f\u5217\uff0c\u60f3\u8981\u7be9\u9078\u51fa\u6240\u6709\u5927\u65bc 0 \u7684\u6578\u5b57\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef is_positive(x):<br \/>\n    return x > 0<\/p>\n<p>nums = [-3, -2, 0, 1, 2, 3]\n<p>positive_nums = filter(is_positive, nums)<\/p>\n<p>print(list(positive_nums))<br \/>\n# \u8f38\u51fa: [1, 2, 3]\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u51fd\u6578 `is_positive`\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u53c3\u6578 `x`\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u5e03\u723e\u503c\uff0c\u4ee5\u5224\u65b7 `x` \u662f\u5426\u5927\u65bc 0\u3002\u63a5\u8457\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u5305\u542b\u6b63\u6578\u548c\u8ca0\u6578\u7684\u5217\u8868 `nums`\uff0c\u4e26\u4f7f\u7528 `filter()` \u51fd\u6578\u4f86\u904e\u6ffe\u51fa\u5927\u65bc 0 \u7684\u6578\u5b57\u3002\u6700\u5f8c\uff0c\u6211\u5011\u5c07\u7d50\u679c\u8f49\u63db\u70ba\u5217\u8868\u4e26\u6253\u5370\u51fa\u4f86\u3002<\/p>\n<p>### \u4f7f\u7528 filter() \u51fd\u6578\u8655\u7406\u5b57\u7b26\u4e32<\/p>\n<p>`filter()` \u51fd\u6578\u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u6578\u5b57\u5e8f\u5217\uff0c\u4e5f\u53ef\u4ee5\u7528\u65bc\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u5229\u7528\u5b83\u4f86\u7be9\u9078\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5beb\u5b57\u6bcd\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef is_upper(c):<br \/>\n    return c.isupper()<\/p>\n<p>s = &#8216;Hello World&#8217;<\/p>\n<p>upper_chars = filter(is_upper, s)<\/p>\n<p>print(list(upper_chars))<br \/>\n# \u8f38\u51fa: [&#8216;H&#8217;, &#8216;W&#8217;]\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u51fd\u6578 `is_upper`\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u5b57\u7b26 `c`\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u5e03\u723e\u503c\uff0c\u5224\u65b7 `c` \u662f\u5426\u70ba\u5927\u5beb\u5b57\u6bcd\u3002\u63a5\u8457\uff0c\u6211\u5011\u5c07\u5b57\u7b26\u4e32 `s` \u4e2d\u7684\u5927\u5beb\u5b57\u6bcd\u7be9\u9078\u51fa\u4f86\u4e26\u986f\u793a\u5176\u7d50\u679c\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `filter()` \u51fd\u6578\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<p>1. **\u50b3\u5165\u7684\u51fd\u6578\u8fd4\u56de\u503c\u4e0d\u6b63\u78ba**\uff1a\u78ba\u4fdd\u4f60\u7684\u51fd\u6578\u8fd4\u56de\u5e03\u723e\u503c\u3002<br \/>\n2. **\u5e8f\u5217\u985e\u578b\u4e0d\u6b63\u78ba**\uff1a`filter()` \u51fd\u6578\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u53ef\u8fed\u4ee3\u5c0d\u8c61\uff0c\u5982\u5217\u8868\u3001\u5143\u7d44\u548c\u5b57\u7b26\u4e32\u3002<br \/>\n3. **\u7d50\u679c\u672a\u8f49\u63db\u70ba\u5217\u8868**\uff1a`filter()` \u51fd\u6578\u8fd4\u56de\u7684\u662f\u4e00\u500b\u8fed\u4ee3\u5668\uff0c\u82e5\u60f3\u986f\u793a\u7d50\u679c\uff0c\u9700\u8f49\u63db\u70ba\u5217\u8868\u6216\u5176\u4ed6\u6578\u64da\u7d50\u69cb\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`filter()` \u51fd\u6578\u5728\u6578\u64da\u8655\u7406\u548c\u5206\u6790\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u6578\u64da\u6e05\u7406\u904e\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u7528\u4f86\u79fb\u9664\u4e0d\u7b26\u5408\u6a19\u6e96\u7684\u6578\u64da\u9805\u76ee\u3002\u7d50\u5408\u5176\u4ed6\u51fd\u6578\u5982 `map()` \u548c `reduce()`\uff0c\u53ef\u4ee5\u5be6\u73fe\u66f4\u5fa9\u96dc\u7684\u6578\u64da\u8655\u7406\u6d41\u7a0b\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5c0d\u65bc\u66f4\u6df1\u5165\u7684 Python \u6559\u5b78\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/6056b35cfd7d232f1fbf61d9) \uff0c\u88e1\u9762\u8a73\u7d30\u4ecb\u7d39\u4e86 Python \u4e2d\u7684\u5404\u7a2e\u51fd\u6578\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: \u5982\u4f55\u4f7f\u7528 lambda \u51fd\u6578\u8207 filter() \u7d50\u5408\uff1f**<br \/>\nA1: \u4f60\u53ef\u4ee5\u4f7f\u7528 lambda \u51fd\u6578\u4f86\u7c21\u5316\u4ee3\u78bc\uff0c\u4f8b\u5982\uff1a`positive_nums = filter(lambda x: x > 0, nums)`\u3002<\/p>\n<p>**Q2: filter() \u51fd\u6578\u80fd\u5426\u8655\u7406\u81ea\u5b9a\u7fa9\u5c0d\u8c61\uff1f**<br \/>\nA2: \u53ef\u4ee5\uff0c\u53ea\u8981\u4f60\u7684\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u6709\u76f8\u61c9\u7684\u689d\u4ef6\u51fd\u6578\uff0c`filter()` \u5c31\u80fd\u5920\u6b63\u5e38\u904b\u4f5c\u3002<\/p>\n<p>**Q3: \u4f7f\u7528 filter() \u6642\u5982\u4f55\u63d0\u9ad8\u6027\u80fd\uff1f**<br \/>\nA3: \u5c0d\u65bc\u5927\u578b\u6578\u64da\u96c6\uff0c\u8003\u616e\u4f7f\u7528\u751f\u6210\u5668\u800c\u975e\u5217\u8868\uff0c\u4ee5\u7bc0\u7701\u5167\u5b58\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u4e2d\u7684 `filter()` \u51fd\u6578\u662f\u4e00\u500b\u9748\u6d3b\u4e14\u5f37\u5927\u7684\u5de5\u5177\uff0c\u9069\u7528\u65bc\u5404\u7a2e\u5e8f\u5217\u7684\u904e\u6ffe\u9700\u6c42\u3002\u900f\u904e\u9069\u7576\u7684\u51fd\u6578\u8a2d\u8a08\uff0c\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u7be9\u9078\u51fa\u6240\u9700\u7684\u5143\u7d20\uff0c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u7c21\u6f54\u6027\u8207\u53ef\u8b80\u6027\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>filter()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u5167\u5efa\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u904e\u6ffe\u5e8f\u5217\uff0c\u7be9\u9078\u51fa\u7b26\u5408\u689d\u4ef6\u7684\u5143\u7d20\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u904e\u6ffe\u5f8c\u7684\u53ef\u8fed\u4ee3\u5c0d\u8c61\u3002\u672c\u6587\u5c07\u4ecb\u7d39filter()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4f86\u8655\u7406Python\u4e2d\u7684\u8cc7\u6599\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2792","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-J2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2792"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2792\/revisions"}],"predecessor-version":[{"id":2793,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2792\/revisions\/2793"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}