0

I put a polynomial in a nested list; for example 5X^3+4X^2+8X. I stored coefficients (5,4,8) in first list and exponents(3,2,1) in second:

polynom = [[5,4,8],[3,2,0]]

Then I define a function to pop the last term of coefficients and exponents like this

def expon_coef_pop(seq):
 expon = seq[1]
 coef = seq[0]
 expon.pop()
 coef.pop()
 return coef, expon
print(expon_coef_pop(polynom))
print(polynom)
# polynom changed into [[5,4],[4,2]]

Surprisingly, I found the value of polynom turned into [[5,4],[3,2]]. I thought I just modified value of expon and coef.

I don't want to change the value of polynomial.

How could this happen, and how to deal with this problem? I am confused about why the polynorm changed not the function. (I just wrote the function for a simple example.)

asked Oct 24, 2015 at 11:50
3
  • how is it 8X shouldnt it be 8 as X^0 is 1 Commented Oct 24, 2015 at 11:52
  • coeff and expon are references to the same list objects as seq[0] and seq[1]. Commented Oct 24, 2015 at 11:53
  • Sorry, a mistake (くろまる'◡'くろまる) Commented Oct 24, 2015 at 11:54

3 Answers 3

1

Both coef and expon are references pointing at the same list objects as seq[0] and seq[1] correspondingly. You will need to copy the lists before popping from them, which can also be done all in one step:

def expon_coef_pop(seq):
 return seq[0][:-1], seq[1][:-1]
answered Oct 24, 2015 at 11:59
0

It's because you pass your lists by reference. Compare with this assignment with copying

expon = list(seq[1])
coef = list(seq[0])
Remi Guan
22.4k17 gold badges67 silver badges90 bronze badges
answered Oct 24, 2015 at 11:54
5
  • That`s just what I need! Thank you! Commented Oct 24, 2015 at 11:57
  • it said 'TypeError: 'list' object is not callable' Commented Oct 24, 2015 at 12:10
  • @SeanXie try my answer and let me know Commented Oct 24, 2015 at 12:11
  • @SeanXie I checked it on both python 2 and python 3 and it works. Maybe you have object called 'list' in another part of your program? If it's true, just rename it. Commented Oct 24, 2015 at 12:19
  • I just tried expon=list(polynom[0]) in python 3.5 console. It still said TypeError Commented Oct 24, 2015 at 12:22
0

Just change simple modification. You can try it:

you need to Change

expon = seq[1] to expon = seq[1][:]
coef = seq[0] to coef = seq[0][:]

Your final code:

polynom = [[5,4,8],[3,2,0]]
def expon_coef_pop(seq):
 expon = seq[1][:]
 coef = seq[0][:]
 expon.pop()
 coef.pop()
 return coef, expon
print(expon_coef_pop(polynom))
print(polynom)
answered Oct 24, 2015 at 12:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.