-3
def count(matrix: list, number: int):

    count([[1, 4, 0, 0, 6, 3], [0, 3, 4, 0, 0, 0], [0, 0, 5, 6, 0]], 0)

I am trying to make function that counts all zeros or any other number from matrix list but for now its 0.

3

2 Answers 2

1

try this:

def count(matrix: list, number: int):
    return sum(l.count(number) for l in matrix)

count([[1, 4, 0, 0, 6, 3], [0, 3, 4, 0, 0, 0], [0, 0, 5, 6, 0]], 0)
# 9

count([[1, 4, 0, 0, 6, 3], [0, 3, 4, 0, 0, 0], [0, 0, 5, 6, 0]], 6)
# 2
Sign up to request clarification or add additional context in comments.

Comments

0
import itertools
def count(matrix: list, number: int):
    return list(itertools.chain.from_iterable(matrix)).count(number)
    
count([[1, 4, 0, 0, 6, 3], [0, 3, 4, 0, 0, 0], [0, 0, 5, 6, 0]], 0)

9

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.