Reputation: 13
transposta = []
nova_linha = []
for i in range (len(matriz)):
for j in range(len(matriz[i])):
nova_linha.append(matriz[i][j])
i+=1
transposta.append(nova_linha)
j+=1
nova_linha = []
return transposta
I am getting the list index out of range error in line nova_linha.append(matriz[i][j])
why is this happening?
Upvotes: 0
Views: 49
Reputation: 365707
This would work fine:
for i in range (len(matriz)):
for j in range(len(matriz[i])):
nova_linha.append(matriz[i][j])
transposta.append(nova_linha)
nova_linha = []
However, you added in lines that do i += 1
and j += 1
. A for
loop over a range
already takes care of that for you.
Normally, that would be as harmless as it is useless, because your change would just get thrown away the next time through the loop—but you also got them backward. So now, each time through the inner loop, you increment i
, and pretty quickly you run off the bottom of the matrix.
Upvotes: 4