﻿{"id":1467,"date":"2020-06-10T12:30:17","date_gmt":"2020-06-10T04:30:17","guid":{"rendered":"https:\/\/www.tansoo.cn\/?p=1467"},"modified":"2020-06-10T12:30:17","modified_gmt":"2020-06-10T04:30:17","slug":"phpbb-35","status":"publish","type":"post","link":"https:\/\/www.tansoo.cn\/?p=1467","title":{"rendered":"python\u4e2dfilter\u7684\u7528\u6cd5"},"content":{"rendered":"<p><span style=\"font-size:150%;line-height:116%\"><strong class=\"text-strong\"><span style=\"color:#FF0000\">Filter<\/span><\/strong><\/span><br \/>\n\u987e\u540d\u601d\u4e49\uff0cfilter\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u7531\u6240\u6709\u7b26\u5408\u8981\u6c42\u7684\u5143\u7d20\u6240\u6784\u6210\u7684\u5217\u8868\uff0c\u7b26\u5408\u8981\u6c42\u5373\u51fd\u6570\u6620\u5c04\u5230\u8be5\u5143\u7d20\u65f6\u8fd4\u56de\u503c\u4e3aTrue. <\/p>\n<p><strong class=\"text-strong\">\u8bed\u6cd5<\/strong><br \/>\n\u4ee5\u4e0b\u662f filter() \u65b9\u6cd5\u7684\u8bed\u6cd5:<span style=\"color:#0000FF\">filter(function, iterable)<\/span><br \/>\n<strong class=\"text-strong\">\u53c2\u6570<\/strong><\/p>\n<ul>\n<li>function &#8212; \u5224\u65ad\u51fd\u6570\u3002<\/li>\n<\/ul>\n<ul>\n<li>iterable &#8212; \u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u8fd4\u56de\u5217\u8868\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u662f\u4e00\u4e2a\u7b80\u77ed\u7684\u4f8b\u5b50\uff1a<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">number_list <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">range<\/span><span style=\"color: black;\">&#40;<\/span>&#8211;<span style=\"color: #ff4500;\">5<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">5<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">less_than_zero <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">filter<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff7700;font-weight:bold;\">lambda<\/span> x: x <span style=\"color: #66cc66;\">&lt;<\/span> <span style=\"color: #ff4500;\">0<\/span><span style=\"color: #66cc66;\">,<\/span> number_list<span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">print<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #008000;\">list<\/span><span style=\"color: black;\">&#40;<\/span>less_than_zero<span style=\"color: black;\">&#41;<\/span><span style=\"color: black;\">&#41;<\/span> &nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># \u8bd1\u8005\u6ce8\uff1a\u4e0a\u9762print\u65f6\uff0c\u52a0\u4e86list\u8f6c\u6362\uff0c\u662f\u4e3a\u4e86python2\/3\u7684\u517c\u5bb9\u6027<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># &nbsp; &nbsp; &nbsp; &nbsp;\u5728python2\u4e2dfilter\u76f4\u63a5\u8fd4\u56de\u5217\u8868\uff0c\u4f46\u5728python3\u4e2d\u8fd4\u56de\u8fed\u4ee3\u5668<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># &nbsp; &nbsp; &nbsp; &nbsp;\u56e0\u6b64\u4e3a\u4e86\u517c\u5bb9python3, \u9700\u8981list\u8f6c\u6362\u4e00\u4e0b<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># Output: [-5, -4, -3, -2, -1]<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u8fd9\u4e2afilter\u7c7b\u4f3c\u4e8e\u4e00\u4e2afor\u5faa\u73af\uff0c\u4f46\u5b83\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u5e76\u4e14\u66f4\u5feb\u3002<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\">#!\/usr\/bin\/python<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># -*- coding: UTF-8 -*-<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">def<\/span> is_odd<span style=\"color: black;\">&#40;<\/span>n<span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">return<\/span> n % <span style=\"color: #ff4500;\">2<\/span> <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #ff4500;\">1<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">newlist <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">filter<\/span><span style=\"color: black;\">&#40;<\/span>is_odd<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: black;\">&#91;<\/span><span style=\"color: #ff4500;\">1<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">3<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">4<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">5<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">6<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">7<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">8<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">9<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">10<\/span><span style=\"color: black;\">&#93;<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">print<\/span><span style=\"color: black;\">&#40;<\/span>newlist<span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\">#\u8f93\u51fa\u7ed3\u679c \uff1a [1, 3, 5, 7, 9]<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u6ce8\u610f\uff1a\u5982\u679cmap\u548cfilter\u5bf9\u4f60\u6765\u8bf4\u770b\u8d77\u6765\u5e76\u4e0d\u4f18\u96c5\u7684\u8bdd\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u770b\u770b\u53e6\u5916\u4e00\u7ae0\uff1a\u5217\u8868\/\u5b57\u5178\/\u5143\u7ec4\u63a8\u5bfc\u5f0f\u3002<\/p>\n<p>\u8bd1\u8005\u6ce8\uff1a\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u63a8\u5bfc\u5f0f\u7684\u53ef\u8bfb\u6027\u66f4\u597d<\/p>\n<p><small>\u539f\u6587\u4f5c\u8005: BG6RSH | <a href=\"https:\/\/bbs.tansoo.cn\/viewtopic.php?t=35\" target=\"_blank\" rel=\"noopener\">\u67e5\u770b\u539f\u5e16<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Filter \u987e\u540d\u601d\u4e49\uff0cfilter\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u7531\u6240\u6709\u7b26\u5408\u8981\u6c42 &hellip; <a href=\"https:\/\/www.tansoo.cn\/?p=1467\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-1467","post","type-post","status-publish","format-standard","hentry","category-105"],"_links":{"self":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1467"}],"version-history":[{"count":0,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1467\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}