Iterating two files at same time, and compare words with strings PYTHON
Question: I'm trying to print whenever a word appears on a string, but actually it
doesn't print anything.My text files are:

    
    
    words={'apple', 'banana', 'pie'}
    strings={'Hello World!', 'I love pie', 'Ate an apple'}
    

`with open("words.txt") as words_file: with open("strings.txt") as
strings_file: all_strings = list(map(str.strip,strings_file)) for a_string in
all_strings: for word in words_file: if word in a_string: print a_string`

and the output would be like

Ate an apple

I love pie


Answer: 
    from itertools import izip
    with open("file1.txt") as f1:
        with open("file2.txt") as f2:
             for f1_line,f2_line in izip(f1,f2): #this will return the same lines from both files
                 do_something(f1_line,f2_line)
    

the reason you saw the same thing over and over was that you

    
    
      for line2 in file2:
          print line1.split() # line 1 will not change until you go through every line in file2
    

[edit] as mentioned in the comments if you are using python2.7+ you can
combine the file opening into one line

    
    
    with open("file1.txt") as f1, open("file2.txt") as f2:
             for f1_line,f2_line in izip(f1,f2): #this will return the same lines from both files
                 do_something(f1_line,f2_line)
    

the code below may do what you are hoping for ...

    
    
    from itertools import izip
    with open("words.txt") as words_file:
        with open("strings.txt") as strings_file:
            all_strings = list(map(str.strip,strings_file))
            for word in words_file:
            for a_string in all_strings:
               if word in a_string:
                  print a_string
    

