Python 中带有回调的 any() 函数

发布于 2024-08-17 03:28:58 字数 366 浏览 5 评论 0原文

Python 标准库定义了一个 any() 函数那

如果可迭代的任何元素为 true,则返回 True。如果迭代为空,则返回 False。

它仅检查元素的计算结果是否为 True。我希望它能够指定一个回调来判断某个元素是否符合要求,例如:

any([1, 2, 'joe'], lambda e: isinstance(e, int) and e > 0)

The Python standard library defines an any() function that

Return True if any element of the iterable is true. If the iterable is empty, return False.

It checks only if the elements evaluate to True. What I want it to be able so specify a callback to tell if an element fits the bill like:

any([1, 2, 'joe'], lambda e: isinstance(e, int) and e > 0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

绳情 2024-08-24 03:28:58

怎么样:

>>> any(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
True

当然它也可以与 all() 一起使用:

>>> all(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
False

How about:

>>> any(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
True

It also works with all() of course:

>>> all(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
False
—━☆沉默づ 2024-08-24 03:28:58

当任何条件为 True 时,any 函数返回 True。

>>> any(isinstance(e, int) and e > 0 for e in [0 ,0, 1])
True # Returns True because 1 is greater than 0.


>>> any(isinstance(e, int) and e > 0 for e in [0 ,0, 0])
False # Returns False because not a single condition is True.

实际上,any函数的概念是从Lisp中引入的,或者可以说是从函数编程方法中引入的。还有一个与之相反的函数是all

>>> all(isinstance(e, int) and e > 0 for e in [1, 33, 22])
True # Returns True when all the condition satisfies.

>>> all(isinstance(e, int) and e > 0 for e in [1, 0, 1])
False # Returns False when a single condition fails.

这两个函数如果使用得当的话真的很酷。

any function returns True when any condition is True.

>>> any(isinstance(e, int) and e > 0 for e in [0 ,0, 1])
True # Returns True because 1 is greater than 0.


>>> any(isinstance(e, int) and e > 0 for e in [0 ,0, 0])
False # Returns False because not a single condition is True.

Actually,the concept of any function is brought from Lisp or you can say from the function programming approach. There is another function which is just opposite to it is all

>>> all(isinstance(e, int) and e > 0 for e in [1, 33, 22])
True # Returns True when all the condition satisfies.

>>> all(isinstance(e, int) and e > 0 for e in [1, 0, 1])
False # Returns False when a single condition fails.

These two functions are really cool when used properly.

GRAY°灰色天空 2024-08-24 03:28:58

您应该使用“生成器表达式” - 即可以使用迭代器并在一行上应用过滤器和表达式的语言构造:

例如 (i ** 2 for i in xrange(10))< /code> 是前 10 个自然数(0 到 9)的平方的生成器,

它们还允许使用“if”子句来过滤“for”子句上的 itens,因此对于您的示例,您可以使用:

any (e for e in [1, 2, 'joe'] if isinstance(e, int) and e > 0)

You should use a "generator expression" - that is, a language construct that can consume iterators and apply filter and expressions on then on a single line:

For example (i ** 2 for i in xrange(10)) is a generator for the square of the first 10 natural numbers (0 to 9)

They also allow an "if" clause to filter the itens on the "for" clause, so for your example you can use:

any (e for e in [1, 2, 'joe'] if isinstance(e, int) and e > 0)
靑春怀旧 2024-08-24 03:28:58

对 Antoine P 的答案

>>> any(type(e) is int for e in [1,2,'joe'])
True

For all()略有改进

>>> all(type(e) is int for e in [1,2,'joe'])
False

Slight improvement to Antoine P's answer

>>> any(type(e) is int for e in [1,2,'joe'])
True

For all()

>>> all(type(e) is int for e in [1,2,'joe'])
False
残龙傲雪 2024-08-24 03:28:58

虽然其他人给出了很好的 Pythonic 答案(在大多数情况下我只是使用公认的答案),但我只是想指出,如果你真的喜欢的话,创建自己的实用函数来自己完成此操作是多么容易:

def any_lambda(iterable, function):
  return any(function(i) for i in iterable)

In [1]: any_lambda([1, 2, 'joe'], lambda e: isinstance(e, int) and e > 0
Out[1]: True
In [2]: any_lambda([-1, '2', 'joe'], lambda e: isinstance(e, int) and e > 0)
Out[2]: False

我想我不过,至少首先使用函数参数定义它,因为这会更接近地匹配现有的内置函数,如map()和filter():

def any_lambda(function, iterable):
  return any(function(i) for i in iterable)

While the others gave good Pythonic answers (I'd just use the accepted answer in most cases), I just wanted to point out how easy it is to make your own utility function to do this yourself if you really prefer it:

def any_lambda(iterable, function):
  return any(function(i) for i in iterable)

In [1]: any_lambda([1, 2, 'joe'], lambda e: isinstance(e, int) and e > 0
Out[1]: True
In [2]: any_lambda([-1, '2', 'joe'], lambda e: isinstance(e, int) and e > 0)
Out[2]: False

I think I'd at least define it with the function parameter first though, since that'd more closely match existing built-in functions like map() and filter():

def any_lambda(function, iterable):
  return any(function(i) for i in iterable)
゛时过境迁 2024-08-24 03:28:58

如果您确实想像这样保留 lambda 表示法,则可以使用 anymap 的组合:

any(map(lambda e: isinstance(e, int) and e > 0, [1, 2, 'joe']))

但最好使用生成器表达式,因为它不会构建整个列表两次。

You can use a combination of any and map if you really want to keep your lambda notation like so :

any(map(lambda e: isinstance(e, int) and e > 0, [1, 2, 'joe']))

But it is better to use a generator expression because it will not build the whole list twice.

醉南桥 2024-08-24 03:28:58

过滤器可以工作,而且它会返回匹配的元素

>>> filter(lambda e: isinstance(e, int) and e > 0, [1,2,'joe'])
[1, 2]

filter can work, plus it returns you the matching elements

>>> filter(lambda e: isinstance(e, int) and e > 0, [1,2,'joe'])
[1, 2]
丿*梦醉红颜 2024-08-24 03:28:58

如果您确实想在any()中内联一个lambda,您可以这样做:

>>> any((lambda: isinstance(e, int))() for e in [1,2,'joe'])
True
>>> any((lambda: isinstance(e, int))() for e in ['joe'])
False

您只需包装未命名的lambda并确保在每次传递时通过附加()<来调用它/code>

这里的优点是,当你击中第一个 int 时,你仍然可以利用短路 any 的评估

If you really want to inline a lambda in any() you can do this:

>>> any((lambda: isinstance(e, int))() for e in [1,2,'joe'])
True
>>> any((lambda: isinstance(e, int))() for e in ['joe'])
False

You just have to wrap up the unnamed lambda and ensure it is invoked on each pass by appending the ()

The advantage here is that you still get to take advantage of short circuiting the evaluation of any when you hit the first int

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文