0

I'm working on a polynomial organizer. I need to insert two items into the list of coefficients and powers, (a zero for the coefficient and the missing power) wherever needed.

print poly
poly=list(poly.split(','))
for i in range(0,13):
  if int(poly[i*2+1])!=int(12-i):
    poly=poly.insert((i*2+1),str(12-i))
    poly=poly.insert((i*2+1),"0")

returns

0,12,0,11,0,10,0,9,0,8,-7,7,1,5,-1,4,1,3,-2,2,5,0
Traceback (most recent call last):
  File "python", line 105, in <module>
  File "python", line 97, in mypoly
AttributeError: 'NoneType' object has no attribute 'insert'

I'm confused because from what I've read on the insert function, it is made to work on lists, but here it seems not to. Please don't kill this question... I've been trying to figure it out on my own for a while now, and always run into this problem.

So I want it to look like this:

[0,12,0,11,0,10,0,9,0,8,-7,7,0,6,1,5,-1,4,1,3,-2,2,0,1,5,0]

Notice the 0,6 and 0,1.

kiyah
  • 1,502
  • 2
  • 18
  • 27
Lewis
  • 23
  • 4
  • `poly.split()` will also return a list, so wrapping it in `list()` is also unnecessary. As is `int(12-i)` (since all the components are already ints). – match Jan 27 '18 at 12:55

1 Answers1

2

The method insert of list returns None, since it modifies the list. You have to change poly = poly.insert(...) to just poly.insert(...):

print poly
poly=list(poly.split(','))
for i in range(0, 13):
  if int(poly[i*2+1]) != int(12-i):
    poly.insert((i*2+1), str(12-i))
    poly.insert((i*2+1), "0")
Bakuriu
  • 98,325
  • 22
  • 197
  • 231