Finding matching keys in two large dictionaries and doing it fast
bioinformatics, python
Solution
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
Problem
I am trying to find corresponding keys in two different dictionaries. Each has about 600k entries. Say for example: ``` myRDP = { 'Actinobacter': 'GATCGA...TCA', 'subtilus sp.': 'ATCGATT...ACT' } myNames = { 'Actinobacter': '8924342' } ``` I want to print out the value for Actinobacter (8924342) since it matches a value in myRDP. The following code works, but is very slow: ``` for key in myRDP: for jey in myNames: if key == jey: print key, myNames[key] ``` I've tried the following but it always results in a KeyError: ``` for key in myRDP: print myNames[key] ``` Is there perhaps a function implemented in C for doing this? I've googled around but nothing seems to work.