1

I am getting data in form of dictionary from data base as:

in_dict = {"x":2,"y":"x+2","z":"y+2"}

and I am getting my maths equation in form of string as :

eqn = "(x+2)*y/z"

What is the easiest method to substitute values in equation and to give final solution?

Note: One should be aware that values in dictionary come randomly.

Sohaib Farooqi
  • 5,457
  • 4
  • 31
  • 43
Sahil Soni
  • 67
  • 2
  • 7
  • Possible duplicate: https://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string – Mr. T Dec 29 '17 at 05:48
  • 1
    What would the answer to this be? You havn't posted an expected output? – RoadRunner Dec 29 '17 at 05:51
  • People rely on the example structure that `x` is a constant and `y` and `z` are interpreted as expressions of `x`. Is this always the case? – Mr. T Dec 29 '17 at 06:02
  • No, the equation can have any number of variables and and different variables are dependent on different variables. – Sahil Soni Dec 29 '17 at 11:10

4 Answers4

2

Assuming you have only three variables,

x = d["x"]
y = eval(d["y"]) # as it is a string
z = eval(d["z"]) # as it is a string

You can use eval:

x = eval("(x+2)*y/z")

[EDIT 2] You can use itemgetter for unpacking:

>>> from operator import itemgetter
>>> stuff = {'a': '1', 'b': 'a+1', 'c': '3', 'd': '4'}
>>> a, b, c = list(map(eval, itemgetter('a', 'b', 'c')(stuff))) # for automatic evaluation, but all must be string
>>> a, b, c
(1, 2, 3)
Ganesh Kathiresan
  • 2,068
  • 2
  • 22
  • 33
2

You can use Sympy to solve these equations. Install it if you don't already have it using pip install sympy

import sympy

in_dict = {"x":2,"y":"x+2","z":"y+2"}
x, y, z = sympy.symbols('x y z')
sympy.sympify("(x+2)*y/z").evalf(subs={x:in_dict['x'], y:in_dict['y'], z:in_dict['z']})

This will give you output as: 2.66666666666667

Sohaib Farooqi
  • 5,457
  • 4
  • 31
  • 43
2

Building off of the previous sympy answer, here is an approach which is dynamic with respect to the size and content of the in_dict variables. The biggest step is to map the sympy symbols (python vars of x, y, z) to their symbolic representations that get passed through your in_dict

import sympy

def eval_eqn(eqn,in_dict):
    subs = {sympy.symbols(key):item for key,item in in_dict.items()}
    ans = sympy.simplify(eqn).evalf(subs = subs)

    return ans

The original inputs:

in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "(x+2)*y/z"
print(eval_eqn(eqn,in_dict))

2.66666666666667

A different set of inputs:

in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print(eval_eqn(eqn,in_dict))

776.000000000000

  • what if the dict values have data frame column values. say I have a column df[bikes] it has multiple values. when we assign to the dict 'a', will the function work? – Ram dr Jan 18 '21 at 10:47
1

Sample code:

from __future__ import division
foo = {"x":2,"y":"x+2","z":"y+2"}
x = "(x+2)*y/z"
x = ("(x+2)*y/z"
     .replace('z', '({})'.format(foo['z']))
     .replace('y', '({})'.format(foo['y']))
     .replace('x', '({})'.format(foo['x'])))
print('x = {}'.format(x))
print('x = {}'.format(eval(x)))

Sample output:

x = ((2)+2)*((2)+2)/(((2)+2)+2)
x = 2.66666666667
Sharad
  • 9,282
  • 3
  • 19
  • 36