简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM