本文介绍了内建对面是否有过滤器()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中是否有一个与 filter 相反的函数?即将项目保存在回调函数中,回调函数返回 False for?找不到任何东西。
Is there a function in Python that does the opposite of filter? I.e. keeps the items in the iterable that the callback returns False for? Couldn't find anything.
推荐答案不,没有内置的反函数 filter ),因为您可以简单地反转测试。只需添加不是:
No, there is no built-in inverse function for filter(), because you could simply invert the test. Just add not:
positive = filter(lambda v: some_test(v), values) negative = filter(lambda v: not some_test(v), values)itertools 模块确实有 itertools.ifilterfalse() ,这是相当多的,因为反转布尔测试非常简单。 itertools 版本始终作为生成器运行。
The itertools module does have itertools.ifilterfalse(), which is rather redundant because inverting a boolean test is so simple. The itertools version always operates as a generator.
更多推荐
内建对面是否有过滤器()?
发布评论