简体   繁体   English

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

[英]any() function in Python with a callback

The Python standard library defines an any() function that Python标准库定义了一个any()函数

Return True if any element of the iterable is true. 如果iterable的任何元素为true,则返回True。 If the iterable is empty, return False. 如果iterable为空,则返回False。

It checks only if the elements evaluate to True . 它仅检查元素是否为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)

How about: 怎么样:

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

It also works with all() of course: 它当然也适用于all()

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

any function returns True when any condition is True. 当任何条件为True时, 任何函数都返回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. 实际上, 任何函数的概念都来自Lisp,或者你可以从函数编程方法中说出来。 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. 如果使用得当,这两个功能真的很酷。

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) 例如(i ** 2 for i in xrange(10))是前10个自然数(0到9)的平方的生成器

They also allow an "if" clause to filter the itens on the "for" clause, so for your example you can use: 它们还允许“if”子句过滤“for”子句中的itens,因此对于您的示例,您可以使用:

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

Slight improvement to Antoine P's answer 对Antoine P的回答略有改进

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

For all() 对于all()

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

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: 虽然其他人给出了很好的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

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(): 我想我至少首先用函数参数定义它,因为它更接近匹配现有的内置函数,如map()和filter():

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

filter can work, plus it returns you the matching elements 过滤器可以工作,加上它返回匹配的元素

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

You can use a combination of any and map if you really want to keep your lambda notation like so : 如果你真的想保持你的lambda符号,你可以使用anymap的组合:

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. 但最好使用生成器表达式,因为它不会构建整个列表两次。

If you really want to inline a lambda in any() you can do this: 如果你真的想在任何()内联一个lambda,你可以这样做:

>>> 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 () 你只需要包装未命名的lambda并确保在每次传递时通过附加()调用它

The advantage here is that you still get to take advantage of short circuiting the evaluation of any when you hit the first int 这里的优点是,当你遇到第一个int时,你仍然可以利用短路评估

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM