Simple nested loop not working correctly

I don't see a problem here, and it drives me crazy. I iterate over 2 text files. Some lines in each file match, and some do not. What I am doing is looping through file1. For each line in this file, iterate over file2 and compare each item to see if they match. What happens is my loop stops after the first loop through file1. Here is my code:

while f < 50: for line in file1: for name in file2: if name == line: print 'a match was found' f+=1 

The while loop happens from somewhere else, but it works fine. I just included it in context. The problem is that file1 only gives me the first line, compares it with all the β€œnames” in file2, and then stops and does not repeat the process for the next line in file1. Am I missing something obvious?

EDIT: if I put a print statement after the first for loop and comment out another for loop, it will skip the whole first file

+4
source share
4 answers

You cannot scroll the file and then iterate over the same file again without trying to start it.

Either reopen file2, call .seek(0) in file2, or load all the lines into a list and loop over them.

In your particular case, using the set name for names is likely to be the fastest:

 names = set(name.strip() for name in file2) while f < 50: for line in file1: if line.strip() in names: f += 1 

You can do the same with the lines in file1 and make a set of intersections, provided that the lines are unique in both files1 and file2.

+9
source

The problem may be that after you repeat over file2 , it is exhausted, so your inner while loop no longer runs (since there is nothing iteration left in file2 ). You can close / reopen file2 every time through the loop, or you can go back to the beginning before this loop is executed.

A slightly better approach is to use sets (if the files are not too large and you are not interested in duplicates in the file or order):

 matches = set(file1).intersection(file2) 

This should only read file1 in memory and loop through file2 implicitly.

+4
source

After the first completion of the inner loop, the internal iterator over file2 reached the goal, so the solution is to start each internal iterator of file2 every time, for example:

 while f < 50: for line in file1: file2.seek(0, 0) for name in file2: if name == line: print 'match!' 
+1
source

Depending on the size of the files, you can use the readlines() function to read the lines of each file in the list.

Then repeat these lists. This ensures that you have no problems with the current position of the file position.

0
source

All Articles