Finding matching keys in two large dictionaries and doing it fast

前端 未结 11 1633
鱼传尺愫
鱼传尺愫 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:21

    You could do this:

    for key in myRDP:
        if key in myNames:
            print key, myNames[key]
    

    Your first attempt was slow because you were comparing every key in myRDP with every key in myNames. In algorithmic jargon, if myRDP has n elements and myNames has m elements, then that algorithm would take O(n×m) operations. For 600k elements each, this is 360,000,000,000 comparisons!

    But testing whether a particular element is a key of a dictionary is fast -- in fact, this is one of the defining characteristics of dictionaries. In algorithmic terms, the key in dict test is O(1), or constant-time. So my algorithm will take O(n) time, which is one 600,000th of the time.

提交回复
热议问题