内建对面是否有过滤器()? [英] Is there a filter() opposite builtin?

查看:110
本文介绍了内建对面是否有过滤器()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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.

这篇关于内建对面是否有过滤器()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆