How to get the number of characters in a SymPy expression

I am trying to get the number of characters in a SymPy expression, for example:

a = sympify('1/4*x+y+c+1/2')

      

The number, for example, should be 3

. All I have come up with so far is

a.args.__len__()

      

However, this also allows for constant factors of 1/4 and 1/2.

Any ideas?

+3


source to share


1 answer


You need to use a method to get a list with all characters in the expression. atoms



In [32]: a.atoms(Symbol)
Out[32]: {c, x, y}

      

+4


source







All Articles