python filter内置函数_filter()内置函数

本文详细介绍了Python内置的filter()函数,如何通过函数或None操作符过滤可迭代对象,展示了使用示例及应用,帮助理解数据筛选关键技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值