为了账号安全,请及时绑定邮箱和手机立即绑定

请帮忙看下怎麼改,谢谢

请帮忙看下怎麼改,谢谢

猛跑小猪 2023-01-04 16:12:37

def getResponse(neighbors):
classVotes = {}
for x in range(len(neighbors)):
response = neighbors[x][-1]
if response in classVotes:
classVotes[response] += 1
else:
classVotes[response] = 1
sortedVotes = sorted(classVotes.iteritems(), key=operator.itemgetter(1), reverse=True)
return sortedVotes[0][0]

查看完整描述

1 回答

?
蛊毒传说

TA贡献1630条经验 获得超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]


查看完整回答
反对 回复 2023-01-06

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信