Python Chapter Seven After-Class Exercises plus(8)

Complete the function fun8, where v is an n*m two-dimensional integer list (the value range is: 0-9), and find the number with the most occurrences in the two-dimensional array. The return result is a numpy array, which may contain multiple values, and the multiple values ​​are arranged in order.

Tip: np.bincount, np.where

def fun8(v):
    """
    Arg:
        v: a Two-dimensional integer list.
    return a np.array include the most frequent number.
    e.g. v:[[0,0,0,2,0],
            [0,1,5,0,0],
            [0,0,2,0,0],
            [1,0,0,5,0],
            [0,1,0,0,0]]
         return: [0]
    """
    count=np.bincount(np.array(v).ravel())
    return np.where(count==np.max(count))[0]

Guess you like

Origin blog.csdn.net/qq_53029299/article/details/115281877