''' filter(fn,lsd) fn是函数 lsd是序列 功能:把传入的函数依次作用于序列的每个元素,根据返回的是True 还是Fals决定是否保留该元素 ''' listNum = [1,2,3,4,5,6,7,8,9] #筛选偶数 def getDouble(num): #保留偶数 if num%2 ==0: return True return False # Python2.x 中返回的是过滤后的列表, 而 Python3 中返回到是一个 filter 类。 listDouble = filter(getDouble,listNum) #可以看到结果 print(type(listDouble)) #需要转换过滤后得到的对象为数列或者自己需要的集合类型; print(list(listDouble))
python filter使用demo
猜你喜欢
转载自blog.csdn.net/weixin_40938748/article/details/85255291
今日推荐
周排行