我是python的新手。我正在通过从文本文件中读取多项式系数来创建多项式。当我在代码下面运行时,我收到错误
“TypeError:无法在标量上累积”
read_file = open('coefficient.txt','r')
coefficient = read_file.read()
p1 = poly1d([coefficient])
print(p1)
请提供您的意见
答案 0 :(得分:1)
在将string
列表传递给poly1d之前,您必须将int
列表转换为from numpy import poly1d
read_file = open('coefficient.txt','r') # 1,1,0,1,0 store in file coefficient.txt
coefficient = read_file.readline().split(',') # coefficient =['1', '1', '0', '1', '0']
p1 = poly1d(map(int, coefficient)) #convert it to [1, 1, 0, 1, 0] with map for python2
#p1 = poly1d(list(map(int, coefficient))) #for python3
print(p1)
列表:
4 3
1 x + 1 x + 1 x
输出:
node.master: true
node.data: false
node.ingest: false