def buildDictionary(): movieFile = open("movies.txt","r") #our dictionary movieDict={} #read line by line for line in movieFile: #break up the line lineList=line.split(",") #get actor actor = lineList[0] #get movies movieList = lineList[1:] #go through each movie, adding to dictionary for movie in movieList: movie=movie.strip() #put movies (key) in dictionary #movie not in dictionary if movie not in movieDict: movieDict[movie]=set() movieDict[movie].add(actor) #else movie is dictionary else: movieDict[movie].add(actor) movieFile.close() return(movieDict) def compareTwoMovies(movie1, movie2): movieDict = buildDictionary() #print error if movie not in dictionary if movie1 not in movieDict or movie2 not in movieDict: print("Movie(s) not found.") return None #set operations set1 = movieDict[movie1] set2 = movieDict[movie2] #union print("The union is", set1 | set2) #intersection print("The intersection is", set1 & set2) #sym difference print("The sym difference is", set1 ^ set2) def coActors(actor): movieDict = buildDictionary() outputSet = set() #go though each move, looking for actor for key in movieDict: #if value contains actor, add all actors to output set if actor in movieDict[key]: outputSet = outputSet | movieDict[key] #you can't be your own co-actor! outputSet.discard(actor) #discard won't throw error! #if the output is empty, we didn't find anyone if len(outputSet) == 0: print("No coactors found.") else: print(outputSet)