def findSimNeighbor_list(person, k): scores = [ (sim_socre_pearson(person, other_person), other_person) for other_person in dataset.keys() if other_person != person ] # 求相似性列表 # 列表排序 scores.sort() scores.reverse() return scores[0:k]
def findSimNeighbor(person, k): neighbours_sim = {} for item in dataset.keys(): if item != person: neighbours_sim[item] = sim_socre_pearson(person, item) # 字典排序 sorted_neighbours = sorted(neighbours_sim.iteritems(), key=lambda asd: asd[1], reverse=True) # 但是字典是无序的,如何将结果传出 neighbours_List = list(sorted_neighbours) return neighbours_List[0:k]