[Python] How can I get a list of the symbols in a SymPy expression?

Discussão em 'Python' iniciado por Stack, Setembro 13, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    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.

    Continue reading...

Compartilhe esta Página