python第七章课后习题plus(8)

完成函数fun8,v是一个n*m的二维整数列表(值的范围是:0-9),找出该二维数组出现次数最多的数字。返回结果是一个numpy数组,可能包含多个数值, 包含多值的时候按顺序排列。

提示: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]

猜你喜欢

转载自blog.csdn.net/qq_53029299/article/details/115281877