I need to count a number of specific numbers per column in a matrix

Asked

Viewed 45 times

-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')

2 answers

0


#Presumindo que esse código só será usado em matrizes quadradas
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]]

ordem = len(mat)

coluna = lambda c: [linha[c] for linha in mat]
contaLinha = lambda l: len([*filter(lambda e: e == 1, mat[l])])
contaColuna = lambda c: len([*filter(lambda e: e == 1, coluna(c))])

dx = [contaLinha(l) for l in range(ordem)] 
dy = [contaColuna(c) for c in range(ordem)] 

print(dx, dy, sep = '\n')

0

Surely there is one more way pythonica to do this but this was my solution:

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 = []

#soma as quantidades por linha
for linha in range(len(mat)):
  soma = 0
  for i in range(len(mat[0])):
    if mat[linha][i] == 1:
      soma += 1
  dx.append(soma)

  #soma as quantidades por coluna
for coluna in range(len(mat[0])):
  soma = 0
  for i in range(len(mat)):
    if mat[i][coluna] == 1:
      soma += 1
  dy.append(soma)

print(dx)
print(dy)
  • Thank you very much!!

  • 1

    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

Browser other questions tagged

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