Python中多项式系数的删除和替换
我在使用python的多项式列表函数时遇到了一些问题 例如,如果我编写多项式Python中多项式系数的删除和替换,python,list,math,coefficients,Python,List,Math,Coefficients,我在使用python的多项式列表函数时遇到了一些问题 例如,如果我编写多项式p1=[0,0,0,1,1],我将得到输出1*x^4+1*x^3+0*x^2+0*x+0 我想对此进行调整,以便: 带有系数1的术语不带系数,例如“1x^3”应写成“x^3” 系数为0的术语完全不应写入,例如“x^4+x^3+0*x^2+0*x+0”应简化为“x^4+x^3” python中是否有用于此的命令 提前谢谢 /亚历克斯 //代码 def polynomial_to_string(p_list):
p1=[0,0,0,1,1]
,我将得到输出1*x^4+1*x^3+0*x^2+0*x+0
我想对此进行调整,以便:
- 带有系数1的术语不带系数,例如
应写成“1x^3”
“x^3”
- 系数为0的术语完全不应写入,例如
应简化为“x^4+x^3+0*x^2+0*x+0”
“x^4+x^3”
def polynomial_to_string(p_list):
terms = []
degree = 0
for coeff in p_list:
if degree == 0:
terms.append(str(coeff))
elif degree == 1:
terms.append(str(coeff) + 'x')
else:
term = str(coeff) + 'x^' + str(degree)
terms.append(term)
degree += 1
terms.reverse()
final_string = ' + '.join(terms)
return final_string
下面是一个单线性函数,用于将列表转换为具有正确系数条件的多项式
p = [0, 0, 0, 1, 1]
s = ' + '.join('%d*x^%d' % (pi, i) if pi != 1 else 'x^%d' % i for i, pi in enumerate(p) if pi != 0)
要按相反顺序打印(高倍优先):
你也可以这样做
def unity(num):
if num==1:return('')
elif num=='':return('.1')
return num
coeffs = [3,2,0,1,6] #6x^4 + 1x^3 + 0x^2 + 2x + 1
variables = ['x^4','x^3','x^2','x','']
output = ' + '.join([str(unity(i))+unity(j) for i,j in zip(coeffs[::-1],variables) if i])
print(output)
>>>'6x^4 + x^3 + 2x + 3.1'
这里有另一种处理标志的方法:
>>> def getsign(n):
... return '-' if n<0 else '+'
...
>>>
>>> def polynom(l):
... pl = ['' if j==0 else '{}x^{}'.format(getsign(j),i) if j==1 or j==-1 else '{}{}x^{}'.format(getsign(j),abs(j),i) for i,j in enumerate(l)]
... return ''.join([str(l[0])]+pl) if l[0]!=0 else ''.join(pl)
...
>>> print polynom([0, 0, 0, -1, -2, 1, 7])
-x^3-2x^4+x^5+7x^6
def getsign(n):
... 如果n>>
>>>def多项式(l):
... pl=[''if j==0 else'{}x^{}'。格式(getsign(j),i)if j==1或j=-1 else'{}{}x^{}。格式(getsign(j),abs(j),i)表示枚举(l)中的i,j)]
... 返回“”。如果l[0]=0其他“”。加入(pl)
...
>>>打印多项式([0,0,0,-1,-2,1,7])
-x^3-2x^4+x^5+7x^6
我使用了
枚举
来完成程度
技巧,避免了简单流程指令中不需要的东西。希望它不像其他解决方案那样神秘;-)
对您的代码进行最小的修改,使其按需要工作,不过我建议您理解Gerges Dib的第一个答案
def polynomial_to_string(p_list):
terms = []
degree = 0
for coeff in p_list:
if coeff > 0:
if coeff == 1:
coeff = ''
if degree == 0:
terms.append(str(coeff))
elif degree == 1:
terms.append(str(coeff) + 'x')
else:
term = str(coeff) + 'x^' + str(degree)
terms.append(term)
degree += 1
terms.reverse()
final_string = ' + '.join(terms)
return final_string
这可能有帮助(不完全是一行):
上述函数的更具代表性的形式:
def polynonmial_equation(coefficients):
degree = len(coefficients) - 1
temp = "".join(map(lambda x: "" if x[1] == 0 else
[" - ", " + "][x[1] > 0] +
[str(abs(x[1])) + "*", ""][abs(x[1]) == 1] +
"x^" + str(degree - x[0]),
enumerate(reversed(coefficients)))).strip()
return temp if temp.startswith('-') else temp[1:]
print(polynonmial_equation([0, 0, 0, 1, 1]))
print(polynonmial_equation([0, 0, 0, 1, -1]))
print(polynonmial_equation([0, 0, 0, -1, -1]))
print(polynonmial_equation([0, 0, 0, -1, 1]))
print()
print(polynonmial_equation([0, 0, 0, 1, 3]))
print(polynonmial_equation([0, 0, 0, 1, -3]))
print(polynonmial_equation([0, 0, 0, -1, -3]))
print(polynonmial_equation([0, 0, 0, -1, 3]))
print()
print(polynonmial_equation([1, 2, 3, 4, 5]))
print(polynonmial_equation([-1, 2, -3, 4, -5]))
print(polynonmial_equation([-1, 2, 0, 4, -5]))
print(polynonmial_equation([0, 0, 6, -1, -3]))
print(polynonmial_equation([0, -3, 4, -1, 0]))
以及输出:
x^4 + x^3
- x^4 + x^3
- x^4 - x^3
x^4 - x^3
3*x^4 + x^3
- 3*x^4 + x^3
- 3*x^4 - x^3
3*x^4 - x^3
5*x^4 + 4*x^3 + 3*x^2 + 2*x^1 + x^0
- 5*x^4 + 4*x^3 - 3*x^2 + 2*x^1 - x^0
- 5*x^4 + 4*x^3 + 2*x^1 - x^0
- 3*x^4 - x^3 + 6*x^2
- x^3 + 4*x^2 - 3*x^1
发布你想要的代码adjust@AlexanderWest请在问题中贴上那个密码。很难将其视为独立的评论。感谢您的回复。案子解决了!答案的一个不同之处在于你的问题模棱两可。如果负系数有一个加号然后是减号,如在
x^2+-3x
中,或者只有一个减号,如在x^2-3x
中,在加入之前,可能会反转结果([[]:-1]
),因此高次项优先。(+1)谢谢!这对我帮助很大!我想你想要的是{}{}x^{}格式(getsign(j),abs(j),I)
而不是{}x^{}.格式(getsign(j,j,I)
。否则,负系数会出现两个连续的负号,它们不是-1
@MarkDickinson是的,非常感谢,我错过了,还更正了我的答案。谢谢!这对我帮助很大!非常感谢。这对我帮助很大!非常感谢。这对我帮助很大!非常感谢。这对我帮助很大!
def polynomial_to_string(p_list):
terms = []
degree = 0
for coeff in p_list:
if coeff > 0:
if coeff == 1:
coeff = ''
if degree == 0:
terms.append(str(coeff))
elif degree == 1:
terms.append(str(coeff) + 'x')
else:
term = str(coeff) + 'x^' + str(degree)
terms.append(term)
degree += 1
terms.reverse()
final_string = ' + '.join(terms)
return final_string
def polynonmial_equation(coefficients):
degree = len(coefficients) - 1
temp = "".join(map(lambda x: "" if x[1] == 0 else [" - ", " + "][x[1]> 0] + [str(abs(x[1])) + "*", ""][abs(x[1]) == 1] + "x^" + str(degree -x[0]), enumerate(reversed(coefficients)))).strip()
return temp if temp.startswith('-') else temp[1:]
def polynonmial_equation(coefficients):
degree = len(coefficients) - 1
temp = "".join(map(lambda x: "" if x[1] == 0 else
[" - ", " + "][x[1] > 0] +
[str(abs(x[1])) + "*", ""][abs(x[1]) == 1] +
"x^" + str(degree - x[0]),
enumerate(reversed(coefficients)))).strip()
return temp if temp.startswith('-') else temp[1:]
print(polynonmial_equation([0, 0, 0, 1, 1]))
print(polynonmial_equation([0, 0, 0, 1, -1]))
print(polynonmial_equation([0, 0, 0, -1, -1]))
print(polynonmial_equation([0, 0, 0, -1, 1]))
print()
print(polynonmial_equation([0, 0, 0, 1, 3]))
print(polynonmial_equation([0, 0, 0, 1, -3]))
print(polynonmial_equation([0, 0, 0, -1, -3]))
print(polynonmial_equation([0, 0, 0, -1, 3]))
print()
print(polynonmial_equation([1, 2, 3, 4, 5]))
print(polynonmial_equation([-1, 2, -3, 4, -5]))
print(polynonmial_equation([-1, 2, 0, 4, -5]))
print(polynonmial_equation([0, 0, 6, -1, -3]))
print(polynonmial_equation([0, -3, 4, -1, 0]))
x^4 + x^3
- x^4 + x^3
- x^4 - x^3
x^4 - x^3
3*x^4 + x^3
- 3*x^4 + x^3
- 3*x^4 - x^3
3*x^4 - x^3
5*x^4 + 4*x^3 + 3*x^2 + 2*x^1 + x^0
- 5*x^4 + 4*x^3 - 3*x^2 + 2*x^1 - x^0
- 5*x^4 + 4*x^3 + 2*x^1 - x^0
- 3*x^4 - x^3 + 6*x^2
- x^3 + 4*x^2 - 3*x^1