I have two large dictionaries with unique keys but possibly overlapping values. I want to compare each set of dictionary values against each other and find the number of overlaps. I have done this using two for loops and set but am wondering if there is a faster/more elegant way to do this.
dic1 = {'a': ['1','2','3'], 'b':['4','5','6'], 'c':['7','8','9']}
dic2 = {'d': ['1','8','9'], 'e':['10','11','12'], 'f':['7','8','9']}
final_list=[]
for key1 in dic1:
temp=[]
for key2 in dic2:
test = set(dic1[key1])
query = set(dic2[key2])
x = len(test & query)
temp.append( [key2, x] )
final_list.append([key1, temp])
You want to “invert” one (or both) of your dictionaries.