Reputation: 129
Is there any way to compare the list elements and return the outcome value ? Below is the python snippet, where it recieves two values and returns the value.
def logical_or_decision(a,b):
return a or b
value = logical_or_decision(1,0)
print value
I need to make it generic & scalable to more than 2 elements.How can i do it for more than 2 elements ?
Upvotes: 4
Views: 2848
Reputation: 82949
From your comment:
say i've a list of [0,-1] , it should return -1. if [0,0,-1] , it should return -1
While most suggest using any
and all
, this does not seem to be what you actually want:
>>> lst1 = [0, False, [], -1, ""]
>>> lst2 = [4, True, "", 0]
>>> any(lst1)
True
>>> all(lst2)
False
Instead, you can use the reduce
builtin (or in Python 3: functools.reduce
) together with an accordant lambda
function, applying or
or and
to the operands, to get the first "truthy" or "falsy" value in the list:
>>> reduce(lambda a, b: a or b, lst1)
-1
>>> reduce(lambda a, b: a and b, lst2)
''
Also, operator.or_
and and_
won't work, as those are bitwise |
and &
instead of logical or
and and
.
>>> reduce(operator.or_, lst1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'int' and 'list'
>>> lst = [1, 2, 4]
>>> reduce(operator.or_, lst)
7
Upvotes: 0
Reputation: 5286
There are two possible interpretations of the question:
OR: any
AND: all
l1 = [True, False, False, True]
t1 = (True, True, True)
t2 = (False, False, False, False)
any(l1) # True
all(l1) # False
any(t1) # True
all(t1) # True
any(t2) # False
all(t2) # False
In this case the functions used are the same but you need to use the map and zip function to wrap them:
l = [True, True, False]
t = (True, False, False)
list(map(any, zip(l, t))) # [True, True, False]
tuple(map(all, zip(l, t))) # (True, False, False)
NOTE: I've used lists and tuples to prove that it can be done with different array-like structures. The list and tuple wrapper in the second example is Python3 as map returns an iterator instead of a list and that would give a non-human-readable answer.
Upvotes: 0
Reputation: 12895
best solution ^^above^^:
any([True, False, True])
# returns True
any
is good because "short-circuits" (like "boolean fast evaluation" it doesn't iterate till the end).
If you want things alike but manually and eager - see reduce:
from operator import or_
from functools import reduce
reduce(or_, [True, False, True])
# returns True
Upvotes: 3
Reputation: 14549
There's a built-in function that does this: any
.
>>> any([0,0,1,0])
True
>>> any([0,0,0])
False
Upvotes: 4
Reputation: 9825
You may use reduce
to do it:
def logical_or_decision(*args):
return reduce(
lambda a, b: a or b,
args,
False
)
print logical_or_decision(1, 1, 1, 0) # False
Of course you could use any
or all
(for logical AND
), but reduce
could provide you the general way to build such operations (not for OR
or AND
).
Upvotes: 2
Reputation: 23003
The builtin function any()
would be the correct way to go about this:
def logical_or_decision(*args):
return any(args)
value = logical_or_decision(1, 0, 0, 1, 0, 0)
print value
The relevant part from the link to the official documentation above:
Return
True
if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:def any(iterable): for element in iterable: if element: return True return False
Upvotes: 0