Step 1: Read all the lines from file 1, separate them and add them as tuples to the set. This will help us perform the search in the next step faster.
with open('file1', 'r') as f:
file1_lines = set([tuple(line.strip().split()) for line in f])
Step 2: Filter the lines from file2 that match your criteria if they start with any of the lines in file1:
with open('file2', 'r') as f2:
for line in itertools.ifilter(lambda x: tuple(x.split()[:3]) in file1_lines, f2):
print line
source
share