-
HUWWW
为什么你不使用 python 的内置模块,statistics。您可以像这样使用模块:import statistics### your input codemode = statistics.mode(mostFreqenNum)print(mode)mode() 接收参数列表类型。然后你可以使用 count()。另一个例子,可能像这样:>>> import statistics>>> lists = [2,3,2,2,3,4,5]>>> mode = statistics.mode(lists)>>> print(mode)2>>> lists.count(2)3>>>
-
拉风的咖菲猫
我不确定你到底想做什么,但也许这可行:mostFreqenNum = {}contMostnum = 0myList = [1, 2, 3, 2, 4, 3, 2, 3, 5, 3]for i in myList: if i in mostFreqenNum: mostFreqenNum[i] += 1 else: mostFreqenNum[i] = 1 for x in mostFreqenNum: if mostFreqenNum[x] > contMostnum: contMostnum = mostFreqenNum[x] mostFreqKey = x else: continueprint(f'Most frequent key, {mostFreqKey}, seen {contMostnum} times.')
-
慕神8447489
def Prediction_Model_v3():alnv3 = [[],[]]inpv3 = int(input('How many time You like to Train you input V3: '))for i in range(inpv3): pdInpv3 = int( input('V3 input number whatever you want: ')) alnv3[0].append(pdInpv3) mdv3 = statistics.mode(alnv3[0]) if(pdInpv3 == mdv3): alnv3[1].append(str(len(alnv3[1]))) print('numberInput V3: ', alnv3[0])print('Most Frequent number V3 is ', str(mdv3), ':', str(len(alnv3[1])))pdtISv3 = (((inpv3-int(len(alnv3[1])))*100)/inpv3)print('Result of prediction V3 is: ', str( mdv3), '=', str(pdtISv3), '%')alnv3.clear()return str(pdtISv3)
-
偶然的你
给定一个值列表inp,您可以找到最常见的值,如下所示:使用collections.Counterfrom collections import Countermost_common = Counter(inp).most_common(1)输出是一个tuple里面(value, count)使用sortedsorted(inp, key=lambda x: inp.count(x), reverse=True)[0]输出是列表中最常见的值使用numpy:# note only works with numeric valuesnp.argmax(np.bincount(inp))输出是列表中最常见的值另一个使用builtins:max(set(inp), key=inp.count)输出是列表中最常见的值另一个使用pandas:import pandas as pdpd.value_counts(inp).index[0]输出是列表中最常见的值