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

时间:2021-09-18 22:04:01

The Python standard library defines an any() function that

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

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

如果iterable的任何元素为True,则返回True。如果iterable为空,则返回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)

8 个解决方案

#1


93  

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

#2


19  

any function returns True when any condition is 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. There is another function which is just opposite to it is all

实际上,任何函数的概念都来自Lisp,或者可以从函数编程方法中得到。还有另一个与它完全相反的函数

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

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

#3


7  

Yo 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:

Yo应该使用一个“生成器表达式”——也就是说,一个语言结构,它可以使用迭代器,然后在一行上应用过滤器和表达式:

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

例如(xrange(10)中的i * 2是前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)

#4


6  

Slight improvement to Antoine P's answer

安东尼·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

#5


5  

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:

当其他人给出了很好的python式的答案时(我在大多数情况下只使用公认的答案),我只是想指出,如果你真的喜欢它,那么自己创建实用函数是多么容易:

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)

#6


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]

#7


2  

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

如果你真的想保持你的lambda符号,你可以使用any和map的组合:

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.

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

#8


0  

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

如果您真的想在any()中内联一个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类型时,你仍然可以利用短路来计算任何类型的值

#1


93  

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

#2


19  

any function returns True when any condition is 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. There is another function which is just opposite to it is all

实际上,任何函数的概念都来自Lisp,或者可以从函数编程方法中得到。还有另一个与它完全相反的函数

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

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

#3


7  

Yo 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:

Yo应该使用一个“生成器表达式”——也就是说,一个语言结构,它可以使用迭代器,然后在一行上应用过滤器和表达式:

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

例如(xrange(10)中的i * 2是前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)

#4


6  

Slight improvement to Antoine P's answer

安东尼·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

#5


5  

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:

当其他人给出了很好的python式的答案时(我在大多数情况下只使用公认的答案),我只是想指出,如果你真的喜欢它,那么自己创建实用函数是多么容易:

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)

#6


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]

#7


2  

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

如果你真的想保持你的lambda符号,你可以使用any和map的组合:

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.

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

#8


0  

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

如果您真的想在any()中内联一个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类型时,你仍然可以利用短路来计算任何类型的值