0
answer_matrix=[["n.a"]*matrix_size]*matrix_size

my code

for c in a:
    if answer_matrix[c[0]-1][c[1]-1]=="n.a":
        answer_matrix[c[0]-1][c[1]-1]=c[2]

Now a looks something like this

>>> a[0]
[1, 2, 1.3e-05, 6e-06, 0.0, 0.0, 0.0, 0.0, 0.0]
>>> a[1]
[1, 3, 0.000949, 0.000142, 0.0, 0.0, 1.8e-05, 5e-06, 0.00011]
>>> a[2]
[1, 4, 0.000417, 0.000126, 0.0, 0.0, 0.0, 0.0, 5.4e-05]

It is a list of list, first 2 entries are rows and columns (not using numpy yet)

My problem is that after running that for loop the answer_matrix is modified in places it shouldn't be.

For example in a there is no [1, 1, ...] entry but I find the answer_matrix[0][0] changed to 9e-06.

When I try to get rid of the for loop and run the code with c=a[0] for example, the code block works perfectly.

What exactly is happening?

Pk.yd
  • 311
  • 1
  • 3
  • 6
  • 1
    I'm not quite sure what your code is doing. Can you reproduce the error if you do `matrix = [ [ ["n.a"] for _ in range(matrix_size)] for __ in range(matrix_size)]`? I think it may be related to http://stackoverflow.com/questions/240178/python-list-of-lists-changes-reflected-across-sublists-unexpectedly – Adam Smith Mar 17 '15 at 18:58
  • sorry edited. So I have a 2 dimensional list that is meant to be a matrix. Initially it is full of "n.a". I also have a seperate 2 dimensional list called "a". Every "row" of a contains a row and column for the answer matrix, third column of a has the value I want to replace the "n.a" So a[0] means replace answer_matrix[0][1]=1.3e-05 – Pk.yd Mar 17 '15 at 19:04
  • I understand that you've initialized a 2d list and are trying to modify it in some way, but I'm not taking the time to grasp exactly how you're mutating it. Please try initializing your 2d list with the code I gave and let us know if the problem persists. – Adam Smith Mar 17 '15 at 19:05
  • @Adam Smith you may actually be right! I followed the links and did some test, seems to be a related problem – Pk.yd Mar 17 '15 at 19:06

0 Answers0