1

I have a few python strings parsed from a file where there are several hundred conditions that i need to evaluate. How would you experts out there recommend I evaluate these conditions? For example...

["20", "<=", "17.5"] # false
["15", ">=", "18.5"] # false
["20", "==", "20"] # true
["beta", "==", "beta"] # true
["beta", "!=", "beta"] # false

I wasn't sure if there were any tricks to resolving these equations, or if i should do some sort of if else like...

op = parts[1]
if op == '<=':
    return op[0] <= op[2]
elif op == '>=':
    return op[0] >= op[2]
elif op == '==':
    return op[0] == op[2]
elif op == '!=':
    return op[0] != op[2]

JokerMartini
  • 5,674
  • 9
  • 83
  • 193

1 Answers1

-1

You could use the eval() function:

parts = ["20", "<=", "17.5"]
print(eval(' '.join(parts)))  # False
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360