I am a beginner in Python. The dynamic programming solution to the coin change problem requires an initialized table that basically could, for example, look like this:
[
[1,0,0,0],
[1,0,0,0],
[1,0,0,0]
]
Which table I specified this way:
table=[[1]+[0]*3]*3
Now, if I write
table[1][2]=1
for x in table: print x
I get a print of:
[1, 0, 2, 0]
[1, 0, 2, 0]
[1, 0, 2, 0]
And I have no damn clue why the whole column gets changed instead of the specified number.
The error is in the code you didn’t show. Presumably, you initialised your list like
or
or something equivalent, which leads to a list containing three times the same list object. If you modify this single object, it changes everywhere it is referenced, because it is only a single object.
If you need further details, show us the code creating the table.
A correct way of initializing
tableisor simply