filter()为python内置函数
如下
filter()函数有两个参数:
第一个,自定函数名,必须的
第二个,需要过滤的列,也是必须的
>>> def is_odd(x):
return x%2==1
>>> filter(is_odd,[1,4,5,6,7,8])
<filter object at 0x104157b70>
>>> result=filter(is_odd,[1,4,5,6,7,8])
>>> next(result)
1
>>> next(result)
5
>>> next(result)
7
注意事项
python2和python3 的filter函数是不一样的,python3的返回的是可迭代对象。