使用预先存在的字典向int值添加

2024-09-29 21:36:12 发布

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

我试图建立一个函数,计算两部电影的相似性得分。已有的词典以电影为关键词,导演、体裁或主要演员都是价值观。有三个演员字典(列出每部电影的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}$

Tags: orthetoaddif电影scoresimilarity
1条回答
网友
1楼 · 发布于 2024-09-29 21:36:12

尝试使用in条件:

if actor1[film1] in (actor1[film2], actor2[film2], actor3[film2]):
   FavActorScore += 5 
if actor2[film1] in ( actor1[film2], actor2[film2], actor3[film2]):
   FavActorScore += 5
if actor3[film1] in (actor1[film2], actor2[film2], actor3[film2]):
   FavActorScore += 5

当你写a==b or c or d时,如果a等于b,或者如果c为真,或者如果d为真,则如果a等于b或c或d,则为true

相关问题 更多 >

    热门问题