Module RE, Have a couple questions

Francis Girard francis.girard at
Tue Mar 1 20:57:43 CET 2005

Le mardi 1 Mars 2005 16:52, Marc Huffnagle a écrit :
> [line for line in document if (line.find('word') != -1 \
>         and line.find('wordtwo') != -1)]


Using re might be faster than scanning the same line twice :

=== begin snap

import re
import sys

def iWordsMatch(lines, word, word2):
  reWordOneTwo = re.compile(r".*(%s|%s).*" % (word,word2))
  return (line for line in lines if reWordOneTwo.match(line))
for line in iWordsMatch(open(""), "re", "return"):
=== end snap

Furthermore, using list comprehension generator (2.4 only I think) and file 
iterator, you can scan files as big as you want with very little memory 


Francis Girard

More information about the Python-list mailing list