【问题标题】:Interpreting a Python function from JSON data从 JSON 数据解释 Python 函数
【发布时间】:2019-05-01 13:07:29
【问题描述】:

我正在尝试确定通过 json 传递给 python 的两个简单函数的等价性,如下所示:

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)

我的假设是 result 返回 false 因为响应和答案被解释为字符串。我如何比较这两个函数,就像我在 python 中设置变量一样:

response = 2*x
answer = x*2

【问题讨论】:

    标签: python json sympy


    【解决方案1】:

    您需要将字符串转换为 SymPy 表达式:

    sympify(answer) == sympify(response)
    

    【讨论】:

      猜你喜欢
      • 2015-08-03
      • 2015-12-01
      • 1970-01-01
      • 2017-04-07
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      相关资源
      最近更新 更多