Take the 2-minute tour ×
d = ['X + Y = Z', 'X <=Y']
p = [{'Y': 1, 'X': 0, 'Z': 0}, {'Y': 1, 'X': 0, 'Z': 3}, {'Y': 1, 'X': 0, 'Z': 6}, {'Y': 1, 'X': 0, 'Z': 9}, {'Y': 1, 'X': 1, 'Z': 0}, {'Y': 1, 'X': 1, 'Z': 3}]

I need to create create some structure which would store List of expressions, where variables are changed.

I need to know: X, Y, Z current values expressions with changed letters to integers

and it has to be for each dict of values

The problem is to see for what X,Y,Z, all expressions are True

share|improve this question
possible duplicate of Evaluating mathematical expressions in Python –  outis Dec 17 '11 at 19:35
add comment

1 Answer

According the expressions are made by you (so you can trust them), a simple solution is to use eval() like this :

correct_values = []
for value in p:
    #if eval(d[0], value) and eval(d[1], value):   # basic version
    if all(eval(exp, value) for exp in d):       # ehanced version thanks to @isbadawi

but you'll have to correct the expression X + Y = Z is not valid python, X + Y == Z is a valid python expression.

But with the values you gave in example, nothing is matching :(

share|improve this answer
I know eval and i am going to use that.There could be more than 2 expressions so your approach isn't good i think :( And I pasted just several values :) –  matiit Dec 17 '11 at 19:39
It's easy to generalize to more than two: if all(eval(exp, value) for exp in d): –  Ismail Badawi Dec 17 '11 at 19:46
add comment

Your Answer


