繁体   English   中英

如何在 python 中的符号后拆分字符串?

[英]How do you split a string after a symbol in python?

import sympy
equation = input('Enter an equation: ')
a = equation.split(('=') or ('<') or ('>') or ('<=') or ('>=') or ('==') or ('!='))[0:2]
b = sympify(a[0])
c = sympify(a[1])
d = simplify(b - c)
print('This is the equation simplified: ' + str(d))

当出现其中一个符号 (=,<,>,>=,<=,==,,=) 时,我想将方程分成两部分。 但在此代码中,它仅在“=”符号是符号时才有效。

我认为你的代码应该是这样的:

import re        #<--- add this
import sympy


equation = input('Enter an equation: ')
a = re.split(r'[=|<|>|<=|>=|==]', equation)[0:2]   #<--- only change this

b = sympify(a[0])
c = sympify(a[1])
d = simplify(b - c)
print('This is the equation simplified: ' + str(d))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM