def mode(L):
shows = []
modeList = []
L.sort()
length = len(L)
for num in L:
count = L.count(num)
shows.append(count)
print 'List = ', L
maxI = shows.index(max(shows))
for i in shows:
if i == maxI:
if modeList == []:
mode = L[i]
modeList.append(mode)
print 'Mode = ', mode
elif mode not in modeList:
mode = L[i]
modeList.append(mode)
print 'Mode = ', mode
return mode
mode(L)
我似乎无法通过我的列表中正常遍历...... 我能顺利拿到第一个模式,但是循环使用第二返回Mode = 87
,我可以不要让它搜索列表的其余部分,这样它也将返回Mode = 92
的Python 3 - 查找列表的方式
我已经删除了我的尝试Mode = 92
,有人可以帮忙填补空白吗?
你能显示你正在测试的列表吗?没有这些,你对87和92等具体数值的引用就没有多大意义。 – Blckknght
L = [98,75,92,87,89,90,92,87] – ARW
我不太明白你要完成什么,但是在“我在节目中:”同样的事情是在“如果modeList == []:“,并且当”elif模式不在modeList:“中,因此它们可以组合成一个”if modeList == []或mode mode not in modeList:“ – 2015-07-12 08:22:38