Python callback any () function

The Python standard library defines an any() function, which

Returns True if any iterability element is true. If iterability is empty, return False.

It only checks if items are evaluated to True . That I want this to be possible allows you to specify a callback to determine if the item matches the count:

 any([1, 2, 'joe'], lambda e: isinstance(e, int) and e > 0) 
+43
python callback functional-programming any
Jan 6 '10 at
source share
8 answers

What 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 
+88
Jan 6 '10 at
source share

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. 

In fact, the concept of any function is derived from Lisp, or you can tell from the approach to programming functions. There is another function that is directly opposite to it: 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 good when used properly.

+19
Jan 06
source share

Yo should use a "generator expression", that is, a language construct that can consume iterators and apply the filter and expressions then on one line:

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

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

 any (e for e in [1, 2, 'joe'] if isinstance(e, int) and e > 0) 
+7
Jan 06 '10 at 11:45
source share

Slight improvement in Antoine R's response

 >>> 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 
+6
Mar 14 '14 at 2:20
source share

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

 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 that at least I first defined it with a function parameter, as that would more closely match existing built-in functions such as map () and filter ():

 def any_lambda(function, iterable): return any(function(i) for i in iterable) 
+5
Nov 08 '13 at 20:43
source share

the filter can work, plus it returns you the corresponding elements

 >>> filter(lambda e: isinstance(e, int) and e > 0, [1,2,'joe']) [1, 2] 
+2
Jan 6 '10 at 16:48
source share

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

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

But it's better to use a generator expression, because it will not build the entire list twice.

+2
Feb 09 '15 at 16:02
source share

If you really want to embed 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 need to wrap the unnamed lambda and make sure it gets called on every pass by adding ()

The advantage is that you can still take advantage of the short circuit to evaluate anyone when you press the first int

0
Aug 01 '17 at 22:22
source share



All Articles