Reputation: 11
I am using Sympy to process randomly generated expressions which may contain the boolean operators 'and', 'or', and 'not'.
'and' and 'or' work well:
a = 0
b = 1
a and b
0
a or b
1
But 'not' introduces a 2nd term 'False' in addition to the desired value:
a, not b
(0, False)
When processed by Sympy (where 'data' (below) provides realworld values to substitute for the variables a and b):
algo_raw = 'a, not b'
algo_sym = sympy.sympify(algo_raw)
algo_sym.subs(data)
It chokes on 'False'.
I need to disable the 2nd term 'False' such that I get only the desired output '0'.
Upvotes: 1
Views: 59
Reputation: 91620
If you use the operators &
, |
, and ~
for and, or, and not, respectively, you will get a symbolic boolean expression. I also recommend using sympy.true
and sympy.false
instead of 1 and 0.
Upvotes: 0
Reputation: 184320
a, not b
doesn't do what you think it does. You are actually asking for, and correctly receiving, a tuple of two items containing:
a
not b
As the result shows, a
is 0
and not b
is False
, 1
being truthy and the not
of truthy being False
.
The fact that a
happens to be the same value as the result you want doesn't mean it's giving you the result you want as the first item of the tuple and you just need to throw away the second item! That would be equivalent to just writing a
.
What you want to do, I assume, is write your condition as a and not b
.
Upvotes: 1