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

2024-05-13 17:14:50 发布

您现在位置: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))

当其中一个符号(,<;,>;,>;,>;=,<;=,==,!=)出现时,我想将方程分成两部分,但在本代码中,它仅在“=”符号为符号时才起作用


Tags: orimportltgtaninput符号simplify
1条回答
网友
1楼 · 发布于 2024-05-13 17:14:50

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

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))

相关问题 更多 >