我试图建立一个函数,计算两部电影的相似性得分。已有的词典以电影为关键词,导演、体裁或主要演员都是价值观。有三个演员字典(列出每部电影的3个主演)。代码基本上运行良好,但有时我得到的结果比我应该得到的分数还要高。在
# create a two-variable function to deterime the FavActor Similarity score:
def FavActorFunction(film1,film2):
#set the result of the FavActor formula between two films to a default of 0.
FavActorScore = 0
#add 3 to the similarity score if the films have the same director.
if direct[film1] == direct[film2]:
FavActorScore += 3
#add 2 to the similarity score if the films are in the same genre.
if genre[film1] == genre[film2]:
FavActorScore += 2
#add 5 to the similarity score for each actor they have in common.
if actor1[film1] == actor1[film2] or actor2[film2] or actor3[film2]:
FavActorScore += 5
if actor2[film1] == actor1[film2] or actor2[film2] or actor3[film2]:
FavActorScore += 5
if actor3[film1] == actor1[film2] or actor2[film2] or actor3[film2]:
FavActorScore += 5
#print the resulting score.
return FavActorScore
我的假设是,在计算演员的共同点时,有些事情是两次计算的。有没有办法修改代码的这一部分,以便得出更准确的结果?在
^{pr2}$
尝试使用
in
条件:当你写
a==b or c or d
时,如果a等于b,或者如果c为真,或者如果d为真,则如果a等于b或c或d,则为true相关问题 更多 >
编程相关推荐