Problem attaching value to array in Python 2

1

The problem is in matrix.append([(x+1), vectorY[x][1]]) . As much as the value of vextor[x][1] is different from (x+1) , when it is added it will receive the same value of (x+1) , leaving an array with two equal values, ex: ([1,1])
This is full code link: link

def createMatrixDi(self, matrixxy):
    matrixxy.sort()
    vectorY = []
    index = 1
    for values in matrixxy: 
        vectorY.append( [ values[1], index ])       
        index += 1
    x = 0
    matrix= []
    while (x < len(vectorY)):
        matrix.append( [(x+1), vectorY[x][1]] )
        x += 1
    return(matrix, len(vectorY))
    
asked by anonymous 06.11.2014 / 15:43

1 answer

1

I simulated the execution of your code sequentially, in parts. I adapted the code a bit and did the following:

a = [[6, 2], [8, 4], [1, 9]]
a.sort()

print(a)

vectorY = []
index = 1

for values in a: 
    vectorY.append( [ values[1], index ])       
    index += 1

print(vectorY)

a printed:

[[1, 9], [6, 2], [8, 4]]

vectorY printed:

[[9, 1], [2, 2], [4, 3]]

Then we have:

x = 0
matrix = []

while (x < len(vectorY)):
    matrix.append( [(x+1), vectorY[x][1]] )
    x += 1

print(matrix, len(vectorY))

You have printed:

([[1, 1], [2, 2], [3, 3]], 3)

There is no problem with your code. In fact, unwanted behavior is in this line:

matrix.append( [(x+1), vectorY[x][1]] )

But notice that vectorY[x][1] , whatever x , will get the second value of [[9, 1], [2, 2], [4, 3]] , which in the previous loop was already sequential.

If I understand what you want, the expected result is:

([[1, 9], [2, 2], [3, 4]], 3)

That is:

matrix.append( [(x+1), vectorY[x][0]] )
    
07.11.2014 / 05:23