[英]Interpreting a Python function from JSON data
I am trying to determine the equivalence of two simple functions passed to python via json like so: 我试图确定通过json传递给python的两个简单函数的等效性,如下所示:
PHP: PHP:
$data = array("2*x", "x*2");
$result = shell_exec('python /path/check.py ' . escapeshellarg(json_encode($data)));
Python: 蟒蛇:
import sys, json
from sympy import *
try:
data = json.loads(sys.argv[1])
except:
sys.exit(1)
x = Symbol('x')
response = data[0]
answer = data[1]
result = response==answer
print json.dumps(result)
My assumption is that result returns false because the response and answer are being interpreted as strings. 我的假设是结果返回false,因为响应和答案被解释为字符串。 How can I compare these two functions as if I set the variables in python like so:
我如何比较这两个函数,就像我在python中设置变量一样:
response = 2*x
answer = x*2
您需要将字符串转换为SymPy表达式:
sympify(answer) == sympify(response)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.