内建对面是否有过滤器()?

编程入门 行业动态 更新时间:2024-10-10 09:15:22
本文介绍了内建对面是否有过滤器()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

内建对面是否有过滤器()?

本文发布于:2023-10-12 04:12:18,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:滤器   有过   内建

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!