将 xy 转换为 x*y? [英] Convert xy to x*y?

查看:42
本文介绍了将 xy 转换为 x*y?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,其中用户以 xy2x2ab 的形式输入输入,但 sympy 无法明白了,我想把它们分别转换成 x * y, 2 * x, 2 * a * b

I am working on a project where user enters the input in the form of xy or 2x or 2ab but sympy is not able to understand that, i want to convert them into x * y, 2 * x, 2 * a * b respectively

推荐答案

sympy 实际上能够理解这一点.您需要使用 parsing 来帮助它:

sympy is actually able to understand that. You need to use parsing to help for it:

from sympy.parsing.sympy_parser import parse_expr
from sympy.parsing.sympy_parser import standard_transformations,\
implicit_multiplication_application
transformations = (standard_transformations +
    (implicit_multiplication_application,))
print(parse_expr("xy", transformations=transformations))

输出:xy

这篇关于将 xy 转换为 x*y?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆