[Python] Replace values in the diagonal of a matrix

Discussão em 'Python' iniciado por Stack, Maio 8, 2025.

  1. Stack

    Stack Membro Participativo

    My task was to find the diagonal of the matrix and then convert the individual values into 0 or 1. 1 when value is >= 0 and 0 when value is < 0. Here is my code:

    A = [[2, 4, 5],
    [-5, 0, 9],
    [-6, 7, -1]]
    n = 3
    for i in range(n):
    for j in range(n):
    if(i == j):
    if(A < 0):
    A == 0
    elif(A>0 & A == 0):
    A == 1
    else:
    pass
    print(A)


    I constantly get this error and I don't know how to fix it.

    TypeError: '<' not supported between instances of 'list' and 'int'

    Continue reading...

Compartilhe esta Página