Finding matching keys in two large dictionaries and doing it fast

前端 未结 11 1643
鱼传尺愫
鱼传尺愫 2020-12-15 17:35

I am trying to find corresponding keys in two different dictionaries. Each has about 600k entries.

Say for example:

    myRDP = { \'Actinobacter\':          


        
11条回答
  •  执念已碎
    2020-12-15 18:05

    Use sets, because they have a built-in intersection method which ought to be quick:

    myRDP = { 'Actinobacter': 'GATCGA...TCA', 'subtilus sp.': 'ATCGATT...ACT' }
    myNames = { 'Actinobacter': '8924342' }
    
    rdpSet = set(myRDP)
    namesSet = set(myNames)
    
    for name in rdpSet.intersection(namesSet):
        print name, myNames[name]
    
    # Prints: Actinobacter 8924342
    

提交回复
热议问题