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.
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.
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
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