首页 > 代码库 > 众数定义及示例

众数定义及示例

def Mode(t):    """众数"""    if not t:        return None    arr = __getfreq(t)    if arr[0][0] == 1:        return None    else:        for k, g in itertools.groupby(arr, key=lambda x: x[0]):            return [t[1] for t in g]def __getfreq(t):    """获取t中每个值及其出现次数"""    arr = sorted(t)    alist = []    for k, g in itertools.groupby(arr):        alist.append((len(list(g)), k))    alist.sort(key=lambda x: x[0], reverse=True)    return alist