【发布时间】:2015-07-12 04:48:49
【问题描述】:
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)
我似乎无法正确地遍历我的列表...
我可以使用第二个 for 循环成功获得第一个返回 Mode = 87 的模式,但是我无法让它搜索列表的其余部分,因此它也会返回 Mode = 92
我已经删除了我在Mode = 92 的尝试,有人可以帮忙填空吗?
【问题讨论】:
-
你能显示你正在测试的列表吗?没有它,您对 87 和 92 等特定值的引用就没有多大意义。
-
L = [98,75,92,87,89,90,92,87]
-
我不太明白你想要完成什么,但是在“for i in show:”中,当“if modeList == []:”和“elif mode”时会做同样的事情不在 modeList 中:”,因此它们可以合并为一个“如果 modeList == [] 或模式不在 modeList 中:”
标签: python python-3.x