How to get all literals (including negative literals) out of a sympy logic object/expression?

Say I have this expression:

Input: (p & ~q & q)

I know it will simplify to False, but just for the sake of easier understanding. I want to obtain this:

Output: [p, ~q, q]

Can I do it without converting the expression to a string and string parsing?

Upvotes: 0

Views: 129

Answers (2)

smichr
smichr

Reputation: 19115

A more general approach is to request Booleans and filter for Symbol and Not

>>> from sympy import Symbol, Not, Boolean, Or, And
>>> eq=Or(And(x,~y),z)
>>> [i for i in eq.atoms(Boolean) if isinstance(i, (Symbol,Not))]
[x, ~y, z, y]

Upvotes: 2

I've found the answer.

Using ".args" as in:

(p & q & ~q).args

gave me the exact list that I needed.

Upvotes: 0

Related Questions