This seems like it should be a simple one, but I'm struggling to come up with an elegant way of achieving it.
Ultimately I have two lists:
- One of arbitrary length
n
containing Boolean values, e.g.conditions = [True, True]
- One of length
n - 1
containing strings corresponding to logical operations, e.g.operations = ['&']
What I would like to do is map the string &
to the logical and
operation, and apply this to the conditions
variable; True and True
- yielding True
.
I can achieve the mapping:
OPERATOR_MAP = {
'&': operator.and_,
'|': operator.or_
}
Leaving me with:
conditions = [True, True]
operations = [<built-in function and_>]
The only way I can envisage applying the operations
to conditions
is to iterate, but I think this could result in violating the operator precedence a downstream user would expect. Is there a neat way of solving this that doesn't involve handling and
then or
precedence manually?