Intersecting two dictionaries in Python

后端 未结 8 2062
借酒劲吻你
借酒劲吻你 2020-11-27 18:26

I am working on a search program over an inverted index. The index itself is a dictionary whose keys are terms and whose values are themselves dictionaries of short document

8条回答
  •  被撕碎了的回忆
    2020-11-27 18:42

    In [1]: d1 = {'a':1, 'b':4, 'f':3}
    
    In [2]: d2 = {'a':1, 'b':4, 'd':2}
    
    In [3]: d = {x:d1[x] for x in d1 if x in d2}
    
    In [4]: d
    Out[4]: {'a': 1, 'b': 4}
    

提交回复
热议问题