0
The program does the following: I say the number of rows and columns I want in my matrix and then assign a value to each column in each row, the problem is that when I specify the number of rows through the append() it creates a "fake list"by placing the elements of the first sub-list in all other:
lista = []
linha = []
nc = int(input('Quantas colunas? '))
nl = int(input('Quantas linhas? '))
for c in range(0, nl):
lista.append(linha)
for c1 in range(0, nl):
for c2 in range(0, nc):
n = int(input(f'Número L[{c1+1}] C[{c2+1}]: '))
lista[c1].append(n)
print(lista)
And in case I try to put lista[c1][c2]
he makes the following mistake:
`lista[c1][c2].append(n)`
> zIndexError: list index out of range`
Output:
Quantas colunas? 3 Quantas linhas? 3 Número L[1] C[1]: 1 Número L[1] C[2]: 2 Número L[1] C[3]: 3 Número L[2] C[1]: 4 Número L[2] C[2]: 5 Número L[2] C[3]: 6 Número L[3] C[1]: 7 Número L[3] C[2]: 8 Número L[3] C[3]: 9 [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]]
`
Now when I leave set the amount of lines
lista = [[], [], []]
for c1 in range(0, 3):
for c2 in range(0, 3):
n = int(input(f'Número L[{c1+1}] C[{c2+1}]: '))
lista[c1].append(n)
print(lista)
it adds the numbers at the correct positions:
Número L[1] C[1]: 1 Número L[1] C[2]: 2 Número L[1] C[3]: 3 Número L[2] C[1]: 4 Número L[2] C[2]: 5 Número L[2] C[3]: 6 Número L[3] C[1]: 7 Número L[3] C[2]: 8 Número L[3] C[3]: 9 [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
I really appreciate you guys helping me out!
Thanks, I thought I couldn’t put
[ ]
in parentheses that he would accept as a list...– Andre Paganotto
@Andrepaganotto Don’t forget to mark the answer as accepted if it solved the problem
– Pagotti