def is_odd(x): return x % 2 == 1 ret = filter(is_odd, [1,4,6,7,9]) print(ret) for i in ret: print(i)
def is_str(s): return type(s) == str ttt = filter(is_str, [1, ‘hello‘]) print(ttt) for i in ttt: print(i)
from math import sqrt def fun(x): res = sqrt(x) return res % 1 == 0 ret = filter(fun,range(1,101)) for i in ret: print(i)
原文:https://www.cnblogs.com/kaiyuan111/p/10513424.html