filter()函数
filter()函数是个过滤器,它的作用就是在海量的数据里提取出有用的信息
>>> help(filter)
Help on class filter in module builtins:class filter(object)
| 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.
|
| Methods defined here:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __iter__(self, /)
| Implement iter(self).
|
| __next__(self, /)
| Implement next(self).
|
| __reduce__(...)
| Return state information for pickling.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.filter()函数有两个参数,第一个参数是一个函数也可以是None,第二个参数是可迭代对象iterable,如果第一个参数是
函数,则将第二个参数可迭代对象的每个元素作为函数的参数进行计算,把返回为True的值筛选出来;如果第一个参数是
None,则直接将第二个参数中为True的值筛选出来。
例:
>>> list1=filter(None,[1,0,2,True,False])
>>> list(list1)
[1, 2, True]
>>> list(filter(lambda x:x%2,range(10)))
[1, 3, 5, 7, 9]
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。