python 求众数 含重复的众数
def mode(arr): dict_cnt = {} for x in arr: dict_cnt[x] = dict_cnt.get(x,0)+1 max_cnt = max(dict_cnt.values()) most_values = [k for k,v in dict_cnt.items() if v==max_cnt] s = 0.0 for x in most_values: s = s + x return s/len(most_values)
arr = [2,2,3,3] mode(arr) #2.5