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
Answer from Antoine P. on Stack Overflowfunctional programming - any() function in Python with a callback - Stack Overflow
How to create a list of different lambda functions.
Lambda function
python - using lambda to verify all elements in list - Stack Overflow
Videos
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
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.
Hi! Just a simple question, I was wondering how to create a list of different lambda functions like so:
[(lambda x: x*i) for i in range(3)]
Ideally, this should return an output like this:
[lambda x: x*0, lambda x: x*1, lambda x: x*2]
However, the actual output is like so:
[lambda x: x*2, lambda x: x*2, lambda x: x*2]
How would I go about producing the desired output? Thanks!
I understand what the lambda function is, its an anonymous function in one line, however why using it, and what really is it? I mean every code I looked at, has it and don't forget map() reduce and filter() function are used with it, what are all these used for and why, I did my research but I still don't understand, (I have a baby's brain ๐ง y'all)
good_foods = ['apple', 'carrot']
junk_foods = ['soda', 'burger']
my_foods = ['banana', 'carrot', 'bread', 'apple', 'soda']
result = all( map( lambda x: x in my_foods, good_foods ) ) and
not( any( map( lambda x: x in junk_foods, my_foods ) ) )
I would solve this problem using any() and all() like so:
result = (all(f in my_foods for f in good_foods) and
not any(f in my_foods for f in junk_foods))
If you really want to use lambda for this, you can do it:
lambda my f: f in my_foods
lambda bad f: f in junk_foods
result = all(my(f) for f in good_foods) and not any(bad(f) for f in my_foods)
But I would do the above with real functions:
def my(f):
return f in my_foods
def bad(f):
return f in junk_foods
result = all(my(f) for f in good_foods) and not any(bad(f) for f in my_foods)
Now, if you really wanted to do this with lambda using map() or reduce(), here is my suggestion:
result = (reduce(lambda x, y: x and y, map(lambda f: f in my_foods, good_foods))
and reduce(lambda x, y: x and y, map(lambda f: f not in junk_foods, my_foods)))
I think the above is slightly improved if we take advantage of the built-in bool.__and__() function, which implements logical and on Booleans, like so:
result = (reduce(bool.__and__, map(lambda f: f in my_foods, good_foods))
and reduce(bool.__and__, map(lambda f: f not in junk_foods, my_foods)))
But here's a solution using filter() if you prefer that. filter() strips out elements that fail a test, so the easiest way to find out if every element passes the test is to see if the resulting list is the same length as the original list.
result = (len(filter(lambda f: f in my_foods, good_foods)) == len(good_foods) and
len(filter(lambda f: f not in junk_foods, my_foods)) == len(my_foods))
Note that any() and all() both have "short-circuit" evaluation; they will be faster than reduce() in cases where not every element in the list really needs to be examined. For example, if the first item in the list is in the junk_foods list, the any() test will immediately complete and the not any(...) will evaluate to False. The reduce() answer would still go through the whole junk_foods list.
Also note that for large lists, you will gain a big speed improvement by using sets. Some of the other answers suggest converting the lists to sets and using set features; this is probably the best way to go.