Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding matching keys in two large dictionaries and doing it fast

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.

Thanks.

like image 417
Austin Richardson Avatar asked Aug 23 '09 00:08

Austin Richardson


People also ask

How do you compare keys in two dictionaries?

Python List cmp() Method. The compare method cmp() is used in Python to compare values and keys of two dictionaries. If method returns 0 if both dictionaries are equal, 1 if dic1 > dict2 and -1 if dict1 < dict2.

Can a dictionary have two keys with the same value two values with the same key?

No, each key in a dictionary should be unique. You can't have two keys with the same value. Attempting to use the same key again will just overwrite the previous value stored. If a key needs to store multiple values, then the value associated with the key should be a list or another dictionary.


1 Answers

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 
like image 125
RichieHindle Avatar answered Oct 02 '22 16:10

RichieHindle