一。去除所有sb人员
movie_people=[‘sb_alex’,‘sb_lzp’,‘sb_yxm’,‘zxl’,‘wjs’]
1.循环方法
movie_people=['sb_alex','sb_lzp','sb_yxm','zxl','wjs']
ret=[]
for p in movie_people:
if not p.startswith('sb'):
ret.append(p)
print(ret)
2.函数方法
movie_people=['sb_alex','sb_lzp','sb_yxm','zxl','wjs']
def filter_test(x):
ret = []
for p in x:
if not p.startswith('sb'):
ret.append(p)
return ret
print(filter_test(movie_people))
3.高阶函数方法
movie_people=['alex-sb','lzp_sb','yxm_sb','zxl','wjs']
def sb_show(n):#末尾为sb
return n.endswith('sb')
def filtr_test(func,accay):
ret=[]
for p in accay:
if not func(p):
ret.append(p)
return ret
res=filtr_test(sb_show,movie_people)
print(res)
4.终极版本
movie_people=['alex-sb','lzp_sb','yxm_sb','zxl','wjs']
def filter_test(func,accay):
ret=[]
for p in accay:
if not func(p):
ret.append(p)
return ret
res = filter_test(lambda x:x.endswith('sb'),movie_people)
print(res)
五。filter函数总结
movie_people=['alex-sb','lzp_sb','yxm_sb','zxl','wjs']
print(filter(lambda x:x.endswith('sb'),movie_people))
print(list(filter(lambda x:not x.endswith('sb'),movie_people)))
来源:CSDN
作者:政蓬
链接:https://blog.csdn.net/weixin_45147782/article/details/104292889