我在使用python的list函数处理多项式时遇到了一些问题。在
例如,如果我写poynomial 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):
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
你也可以这样做
还有一种方法可以处理这个标志:
这里有一个线性函数,它可以将一个列表转换成一个多项式,并具有系数的正确条件
s
^{pr2}$按相反的顺序打印(先用高次幂):
相关问题 更多 >
编程相关推荐