filter(function or None, iterable) --> filter object
Return an iterator yielding those items of iterable for which function(item)
is true. If function is None, return the items that are true.
函数有两个参数,第一个可以为一个函数,或者None,第二个参数为一个可迭代对象
保返回真值(非零,非空)返回值为一个filter object
1 ls = [1, 0 -1, 3] 2 3 l =(filter(None,ls)) 4 print(l) 5 6 <filter object at 0x0000023036E7A390>
这个filter object 是个可迭代对象
1 ls = [1, 0 -1, 3] 2 3 def t(x ): 4 if x > 0: 5 return True 6 else: 7 return False 8 9 l =(filter(t,ls)) 10 for i in l: 11 print(i) 12 13 1 14 3