0

I have a problem with my python code. I try to create a new matrix (adjacency matrix representation of graph) by assigning a value from the old matrix in a loop.

Here's the code:

for x in xrange(0,len(matrixD)):
    matrixD[x][0] = int(matrixD[x][0])
    matrixD[x][1] = int(matrixD[x][1])
    matrixD[x][2] = float(matrixD[x][2])
    print matrixD[x]
matrixFull = [[0]*332]*332

for x in matrixD:
    matrixFull[x[0]-1][x[1]-1] = x[2]
    matrixFull[x[1]-1][x[0]-1] = x[2]
    print x[0]
    print x[1]
    print x[0]-1
    print x[1]-1
    print x[2]
    print matrixFull[x[1]-1][x[0]-1]
    print matrixFull[x[0]-1][x[1]-1]    
    print matrixFull[0][0]
    print "\n\n"

The error happens in the loop creating the matrixFull. As you can see, matrixFull assigning the position follow x in matrixD. I print all the results when the loop runs.

But there's an error:

x[0]:2
x[1]:4
x[0]-1:1
x[1]-1:3
x[2]:0.0515
matrixfull[x[0]-1][x[1]-1]:0.0515
matrixfull[x[1]-1][x[0]-1]:0.0515
matrixfull[0][0]:0



x[0]:1
x[1]:4
x[0]-1:0
x[1]-1:3
x[2]:0.0767
matrixfull[x[0]-1][x[1]-1]:0.0767
matrixfull[x[1]-1][x[0]-1]:0.0767
matrixfull[0][0]:0.0767.

Here are some instances of matrixD:

...
[11, 13, 0.0033]
[10, 11, 0.0011]
[9, 11, 0.0022]
[315, 316, 0.0013]
[315, 317, 0.0022]
...

As you can see, in the second loop the code didn't put the value into [0][0] position but changed it to 0.0767. I have read some post in stackoverflow but can not find anything.

0 Answers0