uNxe - 5 months ago 8x

Python Question

I want to take an expression and split it by symbols. For example

`rate = symbols('0')`

def add_rate(self, new_rate):

self.rate = self.rate + symbols(new_rate)

add_rate('-k\*A\*E')

So the whole thing is

`0 + -k\*A\*E`

`k`

`0.01`

`subs(symbol('k'), 0.01)`

`0 + -0.01\*A\*E`

`-k\*A\*E`

Maybe do you know how to change

`k`

`0.01`

Answer

Solusion is using the sympy.parsing.sympy_parser opportunities.

```
from sympy.parsing.sympy_parser import *
rate = symbols('0')
def add_rate(self, new_rate):
self.rate = self.rate + parse_expr(new_rate.rate_string)
```

Source (Stackoverflow)

Comments