开发者

Adding to a int value with pre-existing dictionaries

开发者 https://www.devze.com 2023-04-11 13:11 出处:网络
I am trying to set up a function that will calculate a score for the similarity of two films.There are pre-existing dictionaries that with the films as keys and either the directors, genres, or main a

I am trying to set up a function that will calculate a score for the similarity of two films. There are pre-existing dictionaries that with the films as keys and either the directors, genres, or main actors are values. There are three actor dictionaries (the 3 lead actors for each film is listed). The code mostly works fine but sometimes I get a resulting score greater than what I should get.

# 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 eac开发者_JAVA技巧h 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

my assumption is that in counting the actors that they have in common, it is counting some things twice. is there a way to revise this part of the code so it comes out with a more accurate result?

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     


Try with the in condition :

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

When you write a==b or c or d this is true if a is equal to b or if c is true or if d is true not true if a is equal to b or c or d.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号