Get values from a list and column

Asked

Viewed 109 times

0

I’m having trouble with a check, I have a certain matrix with N rows and N columns, I need to go through it and know in which row and column have only odd numbers.

Go as far as I can go.

matriz = [[1, 2, 3], [3, 4, 6], [9, 6, 13]]
impares = []
for i in range(len(matriz)):
    for j in range(len(matriz[i])):
        if matriz[i][j] % 2 != 0:
            impares.append(matriz[i][j])

print(impares)

  • From what I understand of your description of the problem is not to check whether an element of the matrix is odd but rather whether all row(s) and/or column(s). For your example only the first column.

  • That’s right bro, that’s what I got to show.

1 answer

1

You can create a variable with an initially True bool. When evaluating a column, switch to False if a number is even. Thus, if the variable continues True after evaluating the entire column, then the column only has odd ones. The same for the rows. Ex:

matriz = [[1, 2, 3], [3, 4, 6], [9, 6, 13]]
colunasimpares = []
for i in range(len(matriz)):
    col_impar = True
    for j in range(len(matriz[i])):
        if matriz[i][j] % 2 == 0:
            col_impar = False
        if col_impar == True: # Avaliamos a coluna inteira e não achamos um par
            colunasimpares.append(j) # Matriz que armazena os índices das colunas ímpares

Something similar for the lines

Browser other questions tagged

You are not signed in. Login or sign up in order to post.