For example, if I run
import sympy x, y, z = sympy.symbols('x:z') f = sympy.exp(x + y) - sympy.sqrt(z)
is there any method of f that I can use to get a list or tuple of sympy.Symbol objects that the expression contains? I'd rather not have to parse srepr(f) or parse downward through f.args.
f
sympy.Symbol
srepr(f)
f.args
In this case, g.args[0].args[1].args[0] gives me Symbol("z"), while g.args[1].args[0].args gives me the tuple (Symbol("x"), Symbol("y")), but obviously these are expression-specific.
g.args[0].args[1].args[0]
Symbol("z")
g.args[1].args[0].args
(Symbol("x"), Symbol("y"))
You can use:
f.free_symbols
which will return a set of all free symbols.
Example:
>>> import sympy >>> x, y, z = sympy.symbols('x:z') >>> f = sympy.exp(x + y) - sympy.sqrt(z) >>> f.free_symbols set([x, z, y])
2.1m questions
2.1m answers
60 comments
57.0k users