yovelde
yovelde

Reputation: 1

How to count how many times the value appears in the matrix

How to count how many times the value appears in the matrix on Python function that gets a matrix and a number and checks how many times the number is inside the matrix and returns the value

def how_many(mat,number):
    counter = 0
    counter =  list(map(lambda x: list(map(lambda z:x  += 1 if z == mat , x)), mat))

    return counter

example: matrix {1,2,3,1} and number = 1

print: 2

Upvotes: 0

Views: 207

Answers (1)

Corralien
Corralien

Reputation: 120399

Use count method of list (if your matrix is a list)

def how_many(mat, number):
    return mat.count(number)

print(how_many([1, 2, 3, 1], 1))

# Output:
2

Upvotes: 2

Related Questions