比使用set更快速地比较字典

时间:2011-12-27 22:09:51

标签: python dictionary set

我有两个带有唯一键但可能重叠值的大型词典。我想将每组字典值相互比较,并找到重叠的数量。我使用两个for循环和set完成了此操作,但我想知道是否有更快/更优雅的方法来执行此操作。

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])

1 个答案:

答案 0 :(得分:2)

您想要“反转”一个(或两个)词典。

val1 = defaultdict(list)
for k in dic1:
    for v in dic1[k]:
        val[v].append( k )
# val1 is a dictionary with each value mapped to the list of keys that contain that value.

for k in dic2: 
    for v in dic2[k]:
        val1[v] is the list of all keys in dic1 that have this value