Search code examples
pythonstringsympyinequality

How can Python solve inequalities from a String?


I use the sympy's solveset to solve inequalities in Python. For example, if I want to solve (10000 / x) - 1 < 0, I can do this:

x = sympy.Symbol('x')
print(sympy.solveset((10000 / x) - 1 < 0, x, sympy.S.Reals))

What can I do to ask Python to solve a inequality, except that it is enclosed in quotes? For instance, "x**2 > 4, x". I tried to search it up, however, all I have found where how to solve "hard-coded" inequalities.


Solution

  • You need to parse the string. Scipy has a parsing module that should do the trick:

    https://docs.sympy.org/latest/modules/parsing.html