No need to keep adding up the elements, just go through the lists and check if each value is different from zero.
One way to solve is to first create a function that checks if a list has only zeros:
def somente_zeros(lista):
for elemento in lista:
if elemento != 0:
return False;
return True;
Note that if you find an element that is not zero, the function already returns False
(not only zeros), because in this case I don’t even need to go through the rest of the list.
If the for
finish, it means that no one entered the if
, and therefore all elements are equal to zero, and the function returns True
.
Now just go through the matrix. At the bottom, the matrix is just a list of lists: a list in which each element is also a list. So just make a for
in the matrix elements, and use the function above to check that it only has zeros:
m = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
# para cada linha da matriz
for linha in m:
# se encontrou algum elemento diferente de zero, já imprime a mensagem e sai do loop
if not somente_zeros(linha):
print('nao nulo')
break
else:
print('nulo')
The for
traverse each row of the matrix and pass it to the function somente_zeros
. If the list has an element other than zero, enter the if
, which already prints the "not null" message and exits the loop (the break
interrupts the for
).
If none of the lines enter the if
(that is, all elements are equal to zero), the break
is not called and the block else
is executed (yes, in Python, a for
may have a block else
associated).
As a general solution for "matrices" with more than two dimensions (i.e., list lists of lists of lists...) you can create another auxiliary function to convert the matrix to a list of one dimension:
from collections.abc import Iterable
def flatten(matriz):
result = []
for elemento in matriz:
# se for um elemento iterável (exceto strings), faz o flatten recursivamente
if isinstance(elemento, Iterable) and not isinstance(elemento, str):
result.extend(flatten(elemento))
else:
result.append(elemento)
return result
With this, no matter the dimension of the matrix, it will be reduced to a single list, containing all the elements. Therefore, you just need to scroll through it and check if some element is not zero:
m = [
[
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
],
[
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
],
[
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
]
]
for elemento in flatten(m):
if elemento != 0:
print('nao nulo')
break
else:
print('nulo')
The idea is the same as the function somente_zeros
: if you find an element that is not zero, print "not null" and stop the loop (do not need to check the rest as I have already found an element that is not zero).
If the for
comes to an end, it means that all elements are zero. The block else
is executed and prints "null".
Why are you adding the values to "0"?
– Wictor Chaves
I don’t understand the sorry question about the list? if it is is I was testing here but can consider another list for example [[0,0,1],[0,0,0],[0,0,0]] in the output will say that it is not null
– Matheus Andrade
Should it return "null" if the matrix has all elements equal to zero or if the sum of all elements is 0? Basically, [0, -1, 1] does not have all values equal to zero, but the sum will be 0, in this case what should be the output?
– Woss