Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在多项式表达式中拆分项而不使用诸如Symphy之类的外部库_Python_Python 3.x_Math_Polynomials_Polynomial Math - Fatal编程技术网

Python 在多项式表达式中拆分项而不使用诸如Symphy之类的外部库

Python 在多项式表达式中拆分项而不使用诸如Symphy之类的外部库,python,python-3.x,math,polynomials,polynomial-math,Python,Python 3.x,Math,Polynomials,Polynomial Math,假设我输入了“1+2x+3x^2”或“1-2x+3x^2” 我如何制作一个函数来拆分并列出每个术语,如[1,2x,3x^2]或[1,-2x,3x^2]。 我已经被这个问题困扰了一段时间,现在我正在使用的函数只在“+”处分开,所以要得到像[1,-2x,3x^2]这样的列表,我必须输入 “1+-2x+3x^2” 注意:我不会使用复杂的多项式,而是使用不包含括号或分数的简单多项式 import re s = '1+2x^2-3x^-3' s = re.sub('\^-', 'neg', s) #

假设我输入了
“1+2x+3x^2”
“1-2x+3x^2”

我如何制作一个函数来拆分并列出每个术语,如
[1,2x,3x^2]
[1,-2x,3x^2]
。 我已经被这个问题困扰了一段时间,现在我正在使用的函数只在“+”处分开,所以要得到像
[1,-2x,3x^2]这样的列表,我必须输入
“1+-2x+3x^2”

注意:我不会使用复杂的多项式,而是使用不包含括号或分数的简单多项式

import re

s = '1+2x^2-3x^-3'

s = re.sub('\^-', 'neg', s) # trick to deal the negative powers
print(s)
s=s.replace('-','+-')
s=s.replace('neg','-')
sub = s.split('+')

print(sub)

这将正确地分割负幂变量。

您可以使用负回溯。@Madbook非常正确。