{"id":2828,"date":"2025-06-01T17:15:07","date_gmt":"2025-06-04T09:40:06","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2828"},"modified":"2025-06-04T17:15:07","modified_gmt":"2025-06-04T09:40:06","slug":"%e4%bd%bf%e7%94%a8python%e7%9a%84filter%e5%87%bd%e6%95%b8%e7%af%a9%e9%81%b8%e5%ba%8f%e5%88%97%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e7%9a%84filter%e5%87%bd%e6%95%b8%e7%af%a9%e9%81%b8%e5%ba%8f%e5%88%97%e5%85%83%e7%b4%a0\/","title":{"rendered":"\u6700\u65b0 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u6df1\u5165\u4e86\u89e3 filter() \u51fd\u6578\u7684\u4f7f\u7528\u8207\u61c9\u7528\uff082025 \u66f4\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, filter(), Python \u6559\u5b78, Python \u7a0b\u5f0f\u8a2d\u8a08, filter \u51fd\u6578\u4f7f\u7528\"><\/p>\n<h1>\u6700\u65b0 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u6df1\u5165\u4e86\u89e3 filter() \u51fd\u6578\u7684\u4f7f\u7528\u8207\u61c9\u7528\uff082025 \u66f4\u65b0\uff09<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`filter()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u7528\u65bc\u7be9\u9078\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002\u5b83\u5c07\u6307\u5b9a\u7684\u51fd\u6578\u61c9\u7528\u65bc\u53ef\u8fed\u4ee3\u5c0d\u8c61\u7684\u6bcf\u500b\u5143\u7d20\uff0c\u4e26\u8fd4\u56de\u4f7f\u51fd\u6578\u8fd4\u56de\u503c\u70ba True \u7684\u5143\u7d20\u96c6\u5408\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39 `filter()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u3001\u9032\u968e\u61c9\u7528\u4ee5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u4f60\u66f4\u6709\u6548\u5730\u4f7f\u7528 Python\u3002<\/p>\n<h2>filter() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`filter()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfilter(function, iterable)<br \/>\n&#8220;`<\/p>\n<p>&#8211; **function**\uff1a\u4e00\u500b\u8fd4\u56de\u5e03\u6797\u503c\u7684\u51fd\u6578\u3002<br \/>\n&#8211; **iterable**\uff1a\u53ef\u8fed\u4ee3\u5c0d\u8c61\uff0c\u5982\u5217\u8868\u3001\u5143\u7d44\u6216\u5b57\u5178\u3002<\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u5305\u542b\u6578\u5b57\u7684\u5217\u8868\uff1a<\/p>\n<p>&#8220;`python<br \/>\nnumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n&#8220;`<\/p>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `filter()` \u51fd\u6578\u4f86\u7be9\u9078\u51fa\u5217\u8868\u4e2d\u7684\u5076\u6578\uff1a<\/p>\n<p>&#8220;`python<br \/>\neven_numbers = list(filter(lambda x: x % 2 == 0, numbers))<br \/>\nprint(even_numbers)<br \/>\n&#8220;`<\/p>\n<p>\u8f38\u51fa\u7d50\u679c\u5c07\u6703\u662f\uff1a<\/p>\n<p>&#8220;`python<br \/>\n[2, 4, 6, 8, 10]\n&#8220;`<\/p>\n<h2>\u4f7f\u7528 filter() \u51fd\u6578\u7684\u9032\u968e\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u6578\u5b57\u7be9\u9078\uff0c`filter()` \u51fd\u6578\u9084\u53ef\u4ee5\u7528\u65bc\u7be9\u9078\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u6211\u5011\u6709\u4e00\u500b\u5b57\u5178\uff0c\u60f3\u8981\u7be9\u9078\u51fa\u7279\u5b9a\u7684\u9375\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_dict = {<br \/>\n    &#8216;name&#8217;: &#8216;John&#8217;,<br \/>\n    &#8216;age&#8217;: 25,<br \/>\n    &#8216;job&#8217;: &#8216;programmer&#8217;<br \/>\n}<\/p>\n<p>filtered_dict = dict(filter(lambda item: item[0] in [&#8216;name&#8217;, &#8216;age&#8217;], my_dict.items()))<br \/>\nprint(filtered_dict)<br \/>\n&#8220;`<\/p>\n<p>\u8f38\u51fa\u7d50\u679c\u70ba\uff1a<\/p>\n<p>&#8220;`python<br \/>\n{&#8216;name&#8217;: &#8216;John&#8217;, &#8216;age&#8217;: 25}<br \/>\n&#8220;`<\/p>\n<p>\u6b64\u5916\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u4f7f\u7528 `filter()` \u51fd\u6578\u4f86\u7be9\u9078\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_string = &#8216;Hello World!&#8217;<br \/>\nfiltered_string = &#8221;.join(filter(lambda x: x in &#8216;aeiou&#8217;, my_string))<br \/>\nprint(filtered_string)<br \/>\n&#8220;`<\/p>\n<p>\u8f38\u51fa\u7d50\u679c\u5c07\u662f\uff1a<\/p>\n<p>&#8220;`python<br \/>\neoo<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 `filter()` \u51fd\u6578\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<p>&#8211; \u78ba\u4fdd\u50b3\u5165\u7684\u51fd\u6578\u8fd4\u56de\u5e03\u6797\u503c\uff0c\u5426\u5247\u6703\u5c0e\u81f4\u7be9\u9078\u7d50\u679c\u4e0d\u6b63\u78ba\u3002<br \/>\n&#8211; \u78ba\u8a8d\u53ef\u8fed\u4ee3\u5c0d\u8c61\u7684\u985e\u578b\uff0c`filter()` \u51fd\u6578\u50c5\u63a5\u53d7\u53ef\u8fed\u4ee3\u5c0d\u8c61\u3002<\/p>\n<p>\u6700\u4f73\u5be6\u8e10\u5efa\u8b70\uff1a<\/p>\n<p>&#8211; \u4f7f\u7528\u547d\u540d\u51fd\u6578\u66ff\u4ee3 `lambda` \u8868\u9054\u5f0f\uff0c\u9019\u6a23\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u3002<br \/>\n&#8211; \u7d50\u5408 `list()`\u3001`set()` \u6216 `tuple()` \u7b49\u51fd\u6578\u4f86\u7372\u5f97\u671f\u671b\u7684\u6578\u64da\u985e\u578b\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`filter()` \u51fd\u6578\u53ef\u8207\u5176\u4ed6 Python \u51fd\u6578\u548c\u5eab\u7d50\u5408\u4f7f\u7528\uff0c\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u6578\u64da\u8655\u7406\u3002\u4f8b\u5982\uff0c\u5229\u7528 `map()` \u51fd\u6578\u5c0d\u7be9\u9078\u5f8c\u7684\u6578\u64da\u9032\u884c\u8f49\u63db\uff0c\u6216\u8005\u8207 Pandas \u7d50\u5408\u9032\u884c\u6578\u64da\u5206\u6790\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u6578\u64da\u8655\u7406\u6280\u5de7\uff0c\u8acb\u53c3\u8003 [vocus.cc \u4e0a\u7684 Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc) \u6216 [miner.tw \u7684\u76f8\u95dc\u8cc7\u6e90](https:\/\/miner.tw)\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Python \u7684 `filter()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u4f60\u5feb\u901f\u7be9\u9078\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002\u7121\u8ad6\u662f\u8655\u7406\u6578\u5b57\u3001\u5b57\u5178\u9084\u662f\u5b57\u7b26\u4e32\uff0c\u638c\u63e1 `filter()` \u51fd\u6578\u7684\u4f7f\u7528\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u4f60\u7684 Python \u7a0b\u5f0f\u8a2d\u8a08\u6280\u5de7\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: filter() \u51fd\u6578\u80fd\u5426\u8207\u5176\u4ed6\u51fd\u6578\u7d50\u5408\u4f7f\u7528\uff1f**<br \/>\nA1: \u662f\u7684\uff0c`filter()` \u51fd\u6578\u53ef\u4ee5\u8207 `map()` \u51fd\u6578\u7b49\u5176\u4ed6\u51fd\u6578\u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u6578\u64da\u8655\u7406\u3002<\/p>\n<p>**Q2: \u4f7f\u7528 filter() \u51fd\u6578\u6642\uff0c\u6709\u4ec0\u9ebc\u6ce8\u610f\u4e8b\u9805\uff1f**<br \/>\nA2: \u78ba\u4fdd\u50b3\u5165\u7684\u51fd\u6578\u8fd4\u56de\u5e03\u6797\u503c\uff0c\u4e26\u6ce8\u610f\u53ef\u8fed\u4ee3\u5c0d\u8c61\u7684\u985e\u578b\u3002<\/p>\n<p>**Q3: filter() \u51fd\u6578\u7684\u6027\u80fd\u5982\u4f55\uff1f**<br \/>\nA3: `filter()` \u51fd\u6578\u5728\u7be9\u9078\u6578\u64da\u6642\u901a\u5e38\u6bd4\u50b3\u7d71\u7684 for \u5faa\u74b0\u66f4\u9ad8\u6548\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u578b\u6578\u64da\u96c6\u6642\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Python\u7684Filter()\u51fd\u6578\u5c0d\u5e8f\u5217\u5143\u7d20\u9032\u884c\u7be9\u9078\uff0c\u53ea\u4fdd\u7559\u51fd\u6578\u8fd4\u56de\u503c\u70baTrue\u7684\u5143\u7d20\uff0c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\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-2828","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-JC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2828","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=2828"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2828\/revisions"}],"predecessor-version":[{"id":2829,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2828\/revisions\/2829"}],"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=2828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}