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
原文地址:http://www.cnblogs.com/data80386/p/3842228.html