如何在python中创建“推荐”函数

2024-04-19 12:34:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一种方法,使用特定的系统向一个人推荐电影。我会提供更多细节。你知道吗

假设我们有以下两个词典:

#this is a dictionary where the keys are people and the items are the movies they liked
person_to_movies = {'James': ['Up', 'Fight Club'], 
                    'Lily': ['Harry Potter', 'Up', 'Wreck it Ralph']
                    'Albus': ['50 Shades of Gray', 'Iron Man']
                    'Severus': ['The Matrix', 'Spiderman 1', 'Captain America', 'Iron Man', 'Joker']}
#this is a dictionary of who is friends with whom
person_to_friends = {'James': ['Albus', 'Harry', 'Lily']
                     'Albus': ['James', 'Lily']
                     'Harry': ['James', 'Lily', 'Severus', 'Ronald']
                     'Ronald': ['Harry']}

我的方法应该做的是输入3个参数:person-to-friends字典、person-to-movies字典和他们推荐电影给的人。 例如:recommend_movies(person_to_friends, person_to_clubs, 'James')将是一个有效的调用。你知道吗

它应该做的是(在这个例子中)

a)如果詹姆斯的朋友喜欢一部电影,那么这部电影得+1分

b)如果詹姆斯的朋友喜欢一部电影,结果发现他们两人都喜欢另一部电影,那么詹姆斯的朋友喜欢的电影就得到了另一个分数。你知道吗

举个例子,既然詹姆斯和莉莉是朋友,电影《失事拉尔夫》就有道理,但既然莉莉和哈利都喜欢电影《起来》,电影《失事拉尔夫》就有了另一个道理

输出的格式是一个元组列表,其中每个元组有两个元素:电影和它的点

这是我目前掌握的情况

recommend_movies(p2f: Dict[str, List[str]],
        p2m: Dict[str, List[str]],
        p: str,) -> List[Tuple[str, int]]:

    ignore_movies = []

    #we want to ignore the movies the person has already watched
    if p in p2m:
        ignore_movies = p2m[p]

    #we'll make a list of their friends that like at least 1 movie
    friends_list = p2f[p]
    for friend in friends_lst:
        if friend not in p2m:
            friends_lst.remove(friend)    

    #now let's make a list of the movies the person's friends have liked
    movies_lst = []
    for friend in friends_lst:
        for movie in p2m[friend]:
            if movie not in ignore_movies:
                movies_lst.append(movie)

    movie_recommendations = {}
    for movie in movies_lst:
        if movie not in movie_recommendations:
            movie_recommendations[movie] = 1
        elif movie in movie_recommendations:
            movie_recommendations[movie] += 1

    return [(k, v) for k, v in movie_recommendations.items()] 

这个代码似乎适用于(a),如果一个人的朋友喜欢一部电影,它会增加这个数字,但我不知道该为(b)做什么,如果一个人的朋友喜欢一部电影,而这个朋友和这个人有另一部相互喜欢的电影。你知道吗

所以这个例子的输出是[('50 Shades of Gray', 1), ('Iron Man', 1), ('Harry Potter', 1), ('Wreck it Ralph', 1)],而不是[('50 Shades of Gray', 1), ('Iron Man', 1), ('Harry Potter', 2), ('Wreck it Ralph', 2)]


Tags: ofthetoin电影朋友moviesmovie
1条回答
网友
1楼 · 发布于 2024-04-19 12:34:10

询问家庭作业

  • 先自己解决问题。如果我们看不到你做了足够的工作,你的问题很可能会被嘘下台;它将被否决并关闭。

  • 询问您的现有实现中的具体问题。如果您还不能做到这一点,请先尝试更多您自己的工作或寻求更一般的帮助;您的教授可能是此阶段比堆栈溢出更好的资源。

  • 了解学校政策。如果学校有关于家庭作业外部帮助的政策,请确保在请求/接受家庭作业帮助之前了解该政策。如果有特定的限制(例如,您可以接收帮助,但不能接收完整的代码示例),请将它们包含在问题中,以便那些提供帮助的人可以让您避免麻烦。

  • 永远不要使用你不懂的代码。以后(放学后,在以后的作业中,在考试中,等等)这肯定对你没有帮助,如果你被要求解释你上交的代码,最多也会很尴尬。

  • 了解“询问关于家庭作业的问题”和“询问关于家庭作业中代码的特定问题”之间的区别。你不应该询问关于家庭作业的问题,因为通常情况下,它不会满足本问题其余部分的建议。取而代之的是,询问关于你为解决家庭作业问题而编写的代码的问题,并具体说明输入、所需输出和错误消息。如果您使用代码并创建一个MCVE而不是粘贴整个代码,尤其是当它是一个长代码块时,这是非常理想的。

尽管如此

def recommend_movies(person_to_friends,person_to_movies,person):
    movies = {}
    # these are the movies the person has seem
    my_movies = set(person_to_movies[person])
    for friend in person_to_friends[person]:
      friends_movies = person_to_movies.get(friend,[])
      modifier = len(my_movies.intersection(friends_movies))+1
      # you get one point for being a friend, + N points for the overlap in liked movies
      for movie in friends_movies: # score each of the friends movies
         movies[movie] = movies.get(movie,0) + modifier; 
    for movie in my_movies: 
      # remove each of the movies this person has seen
      movies.pop(movie,None)
    return sorted(movies.items(),key = lambda x:x[-1])

相关问题 更多 >