filter函数:遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下 # 例子:条件筛选 name =['m_xiaoli','zhangfei','m_xiaoma','m_wangyun'] ret = [] def test(name): for i in name: if not i.startswith("m"): ret.append(i) return ret print(test(name)) ['zhangfei'] def test1(array):
前戏 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'
场景模拟:我想判断某个列表里面的某个元素怎么怎么样 基础方法,如果需要判断多次则重复代码 ret = [] move_peole = ["alex","sb_wupeiqi","yangtuo","sb_yuanhao"] for p in move_peole: if not p.startswith("sb"): ret.append(p) print(ret) 函数方法利用参数更换来多次代入执行,判断
fileter功能主要使用在需要对数据进行多种操作,并对数据进行过滤的操作. 普通函数实现: movie = ['sb_alex', 'wupei', 'tiger', 'goosb','xxfd','sb_xxx'] def fileter_peope(func,array): ret=[] for i in array: if not func(i): ret.append(i) return ret res = fileter_peope(lambda n:n.startswith("sb