day16 Python filter函数

前戏

movie_people = ["alex","charon","pluto","liu","sb","sb_250"]

ret = []
for i in movie_people:
if not i.startswith("sb"):
ret.append(i) print(ret) 结果:
['alex', 'charon', 'pluto', 'liu']

前五分钟

movie_people = ["alex","charon","pluto","liu","sb","sb_250"]

def filter_test(arrat):
ret = []
for i in arrat:
if not i.startswith("sb"):
ret.append(i)
return ret print(filter_test(movie_people)) 结果:
['alex', 'charon', 'pluto', 'liu']

十分钟

movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
def sb_show(n):
return n.endswith('sb') def filter_test(func,array):
ret=[]
for p in array:
if not func(p):
ret.append(p)
return ret res=filter_test(sb_show,movie_people)
print(res) 结果:
linhaifeng

十五分钟

movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
# def sb_show(n):
# return n.endswith('sb')
#--->lambda n:n.endswith('sb')
# :前面是参数,后面是返回值
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','wupeiqi_sb','linhaifeng','yuanhao_sb']
print(filter(lambda n:not n.endswith('sb'),movie_people)) res=filter(lambda n:not n.endswith('sb'),movie_people)
print(list(res)) print(list(filter(lambda n:not n.endswith('sb'),movie_people))) 结果:
['linhaifeng']
<filter object at 0x7f9aa857b908>
['linhaifeng']
['linhaifeng']
上一篇:Node.js:回调函数


下一篇:jquery插件-表单验证插件-提示信息中文化与定制提示信息