def getResponse(neighbors):classVotes = {}for x in range(len(neighbors)):response = neighbors[x][-1]if response in classVotes:classVotes[response] += 1else:classVotes[response] = 1sortedVotes = sorted(classVotes.iteritems(), key=operator.itemgetter(1), reverse=True)return sortedVotes[0][0]
1 回答
蛊毒传说
TA贡献1895条经验 获得超3个赞
def getResponse(neighbors):
#给结果搞个字典
classVotes={}
#把每个neighor都循环一遍
for x in range(len(neighbors)):
response = neighbors[x][-1]
print response
if response in classVotes:
classVotes[response]+=1
print classVotes
else:
classVotes[response] =1
sortedVotes = sorted(classVotes.iteritems(),key = operator.itemgetter(1),reverse = True)
#reverse是按降序方式排序
return sortedVotes[0][0]
添加回答
举报
0/150
提交
取消