-6
I’m doing a program that reads the number "1" per row in the matrix and I need to find the number "1" per column. It turns out that I am not able to assemble a code that reads only the columns and adds the numbers 1. I need that in dx
leaves the quantities of 1 in each row, and in dy
the quantity of 1 in each column.
Below is my attempt:
import random
n = 10
mat = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 1, 1, 0, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 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]]
dx = []
dy = []
for j in range(n):
for i in range(n):
valor = mat[j][i]
print("\t", valor, end='')
print("\n")
for i in range(n):
#conta linha
dx.append(mat[i].count(1))
for j in range(10):
dy.append(mat[j].count(1))
print(dx,dy, sep = '\n')
Thank you very much!!
– Giovanna Muniz
If I answered your question, don’t forget to click the green tick on the left to help other people on the site who have the same question as you
– Natan Fernandes