words = {'foo', 'bar', 'foobar'}
with open('out.txt') as f:
    counts = Counter(word for line in f for word in line.split() if word in
        words)
Perl: counting many words in many strings efficiently