filter(function,iterable)
- 过滤可迭代对象的元素,返回一个迭代器
- function一个具有一个参数的函数,返回bool
-
#初始运行逻辑
movie_people = ["alex_sb","sb_wupeiqi_sb","linhaifeng","sb_yuanhao_sb"]
def sb_show(n):
return n.endswith("sb")
#---->lambda n:n.endswith("sb") 这里的lambda 函数与def 函数运行结果一致
def filter_test(func,array):
ret = []
for p in array:
if not func(p):
ret.append(p)
return ret
res = filter_test(lambda n:n.endswith("sb"),movie_people)
print(res)
#filter 函数
movie_people = ["alex_sb","sb_wupeiqi_sb","linhaifeng","sb_yuanhao_sb"]
print(list(filter(lambda n:not n.endswith("sb"),movie_people)))
#输出结果
['linhaifeng']
['linhaifeng']