Skip to content Skip to sidebar Skip to footer

Two Lists, Faster Comparison In Python

I'm writting python (2.7) script to compare two lists. These lists are created from files by reading their content. Files are just text files, no binary. File 1 contains only hashe

Solution 1:

Store one of these files in a dictionary or set; that takes out a full loop and lookups are O(1) constant time on average.

For example, it looks like the crackdata file can easily be converted to a dictionary:

with open(CRACKED) as crackedfile:
    cracked = dict(map(str.strip, line.split(':')) for line in crackedfile if ':' in line)

and now you only have to loop over the other file once:

with open(HASHES) as hashes:
    for line in hashes:
        hash = line.strip()
        if hash in cracked:
            print('Found:', hash, 'which maps to', cracked[hash])

Post a Comment for "Two Lists, Faster Comparison In Python"