查找列表的模式

给出一个项目列表,回顾一下,列表的模式是最常出现的项目。

我想知道如何创build一个函数,可以find列表的模式,但如果列表没有模式(例如,列表中的所有项目只出现一次),显示一条消息。 我想在不导入任何函数的情况下做这个函数。 我试图从头开始自己的function。

您可以使用collections包中提供的具有mode转换function的Counter

 from collections import Counter data = Counter(your_list_in_here) data.most_common() # Returns all unique items and their counts data.most_common(1) # Returns the highest occurring item 

注意:Counter是python 2.7中的新function,在早期版本中不可用。

您可以使用maxfunction和一个键。 看看使用'key'和lambdaexpression式的python max函数 。

 max(set(list), key=list.count) 

Python 3.4包含了statistics.mode方法,所以很简单:

 >>> from statistics import mode >>> mode([1, 1, 2, 3, 3, 3, 3, 4]) 3 

您可以在列表中包含任何types的元素,而不仅仅是数字:

 >>> mode(["red", "blue", "blue", "red", "green", "red", "red"]) 'red' 

从一些统计软件,即SciPy和MATLAB ,这些只返回最小的最常见的值,所以如果两个值经常发生,最小的这些返回。 希望有一个例子可以帮助:

 >>> from scipy.stats import mode >>> mode([1, 2, 3, 4, 5]) (array([ 1.]), array([ 1.])) >>> mode([1, 2, 2, 3, 3, 4, 5]) (array([ 2.]), array([ 2.])) >>> mode([1, 2, 2, -3, -3, 4, 5]) (array([-3.]), array([ 2.])) 

你有什么理由不遵循这个惯例?

有很多简单的方法可以在Python中find列表的模式,例如:

 import statistics statistics.mode([1,2,3,3]) >>> 3 

或者,你可以通过它的数量find最大值

 max(array, key = array.count) 

这两种方法的问题是它们不适用于多种模式。 第一个返回错误,第二个返回第一个模式。

为了find一个集合的模式,你可以使用这个函数:

 def mode(array): most = max(list(map(array.count, array))) return list(set(filter(lambda x: array.count(x) == most, array))) 

我写了这个方便的functionfind模式。

 def mode(nums): corresponding={} occurances=[] for i in nums: count = nums.count(i) corresponding.update({i:count}) for i in corresponding: freq=corresponding[i] occurances.append(freq) maxFreq=max(occurances) keys=corresponding.keys() values=corresponding.values() index_v = values.index(maxFreq) global mode mode = keys[index_v] return mode 

简短,但不知何故丑:

 def mode(arr) : m = max([arr.count(a) for a in arr]) return [x for x in arr if arr.count(x) == m][0] if m>1 else None 

使用字典,稍微难看一点:

 def mode(arr) : f = {} for a in arr : f[a] = f.get(a,0)+1 m = max(f.values()) t = [(x,f[x]) for x in f if f[x]==m] return m > 1 t[0][0] else None 

这个函数返回一个函数的一个或多个模式,而不pipe数据集中多less个或多个模式的频率。 如果没有模式(即所有项目只出现一次),该函数返回一个错误string。 这与上面的A_nagpal函数类似,但是我认为这个函数更加完整,我认为对于任何Python新手(比如真正的)来说,阅读这个问题是很容易理解的。

  def l_mode(list_in): count_dict = {} for e in (list_in): count = list_in.count(e) if e not in count_dict.keys(): count_dict[e] = count max_count = 0 for key in count_dict: if count_dict[key] >= max_count: max_count = count_dict[key] corr_keys = [] for corr_key, count_value in count_dict.items(): if count_dict[corr_key] == max_count: corr_keys.append(corr_key) if max_count == 1 and len(count_dict) != 1: return 'There is no mode for this data set. All values occur only once.' else: corr_keys = sorted(corr_keys) return corr_keys, max_count 

稍长一点,但可以有多种模式,并可以获得大多数计数或数据types混合的string。

 def getmode(inplist): '''with list of items as input, returns mode ''' dictofcounts = {} listofcounts = [] for i in inplist: countofi = inplist.count(i) # count items for each item in list listofcounts.append(countofi) # add counts to list dictofcounts[i]=countofi # add counts and item in dict to get later maxcount = max(listofcounts) # get max count of items if maxcount ==1: print "There is no mode for this dataset, values occur only once" else: modelist = [] # if more than one mode, add to list to print out for key, item in dictofcounts.iteritems(): if item ==maxcount: # get item from original list with most counts modelist.append(str(key)) print "The mode(s) are:",' and '.join(modelist) return modelist 

为什么不只是

 def print_mode (thelist): counts = {} for item in thelist: counts [item] = counts.get (item, 0) + 1 maxcount = 0 maxitem = None for k, v in counts.items (): if v > maxcount: maxitem = k maxcount = v if maxcount == 1: print "All values only appear once" elif counts.values().count (maxcount) > 1: print "List has multiple modes" else: print "Mode of list:", maxitem 

这没有一些错误检查,但它会find模式,而不会导入任何函数,如果所有的值只出现一次,将会打印一条消息。 它也将检测到共享相同的最大数量的多个项目,虽然目前尚不清楚,如果你想这样做。

 def mode(inp_list): sort_list = sorted(inp_list) dict1 = {} for i in sort_list: count = sort_list.count(i) if i not in dict1.keys(): dict1[i] = count maximum = 0 #no. of occurences max_key = -1 #element having the most occurences for key in dict1: if(dict1[key]>maximum): maximum = dict1[key] max_key = key elif(dict1[key]==maximum): if(key<max_key): maximum = dict1[key] max_key = key return max_key 
 def mode(data): lst =[] hgh=0 for i in range(len(data)): lst.append(data.count(data[i])) m= max(lst) ml = [x for x in data if data.count(x)==m ] #to find most frequent values mode = [] for x in ml: #to remove duplicates of mode if x not in mode: mode.append(x) return mode print mode([1,2,2,2,2,7,7,5,5,5,5]) 

这是一个简单的函数,获取列表中的第一个模式。 它使用列表元素作为键和出现次数的字典,然后读取字典值来获得模式。

 def findMode(readList): numCount={} highestNum=0 for i in readList: if i in numCount.keys(): numCount[i] += 1 else: numCount[i] = 1 for i in numCount.keys(): if numCount[i] > highestNum: highestNum=numCount[i] mode=i if highestNum != 1: print(mode) elif highestNum == 1: print("All elements of list appear once.") 

如果您对最小,最大或所有模式感兴趣,

 def get_small_mode(numbers, out_mode): counts = {k:numbers.count(k) for k in set(numbers)} modes = sorted(dict(filter(lambda x: x[1] == max(counts.values()), counts.items())).keys()) if out_mode=='smallest': return modes[0] elif out_mode=='largest' return modes[-1] else: return modes 

这将返回所有模式:

 def mode(numbers) largestCount = 0 modes = [] for x in numbers: if x in modes: continue count = numbers.count(x) if count > largestCount: del modes[:] modes.append(x) largestCount = count elif count == largestCount: modes.append(x) return modes 

如果你想要一个清晰的方法,有用的教室,只有通过理解使用列表和字典,你可以做:

 def mode(my_list): # Form a new list with the unique elements unique_list = sorted(list(set(my_list))) # Create a comprehensive dictionary with the uniques and their count appearence = {a:my_list.count(a) for a in unique_dist} # Calculate max number of appearences max_app = max(appearence.values()) # Return the elements of the dictionary that appear that # of times return {k: v for k, v in appearence.items() if v == max_app}