电影推荐系统:为每一部电影找到评分者评分的电影



我有一个嵌套的电影评级字典:

{约翰:{星球大战:1.0,"静音":2.0,"Forrest":3.0},玛丽亚:{星球大战:南,"静音":1.0,"Forrest":2.0},《迈克》:{《星球大战》:9.0,"沉默":nan,《阿甘正传》:nan,

现在我想做一个函数,计算每部电影有多少人看过那部电影(score!=nan(,也看过其他电影,然后按降序排列。

我希望这是清楚的。提前感谢

这将是我使用这个排名系统的方法:

from collections import defaultdict
nan = 0    # to simplify the parsing...
dc = {'John' :{'Star Wars': 1.0, 'Silence': 2.0, 'Forrest' : 3.0},
'Maria':{'Star Wars': 0, 'Silence': 1.0, 'Forrest':2.0},
'Mike':{'Star Wars': 9.0, 'Silence': 0, 'Forrest': 0}}

scores = defaultdict(int)
for name, movies in dc.items():

for movie, score in movies.items():
if score:       # not 0
scores[movie] += score

print(scores)
ranking = {k: v for k, v in sorted(scores.items(),
key=lambda item: -item[1])}
print(ranking)    # {'Star Wars': 10.0, 'Forrest': 5.0, 'Silence': 3.0}

最新更新