Efficient grep using Python?

Fredrik Lundh fredrik at pythonware.com
Wed Dec 15 11:07:37 EST 2004


"sf" <sf at sf.sf> wrote:

> I have files A, and B each containing say 100,000 lines (each line=one
> string without any space)
>
> I want to do
>
> "  A  - (A intersection B)  "
>
> Essentially, want to do efficient grep, i..e from A remove those lines which
> are also present in file B.

that's an unusual definition of "grep", but the following seems to
do what you want:

afile = "a.txt"
bfile = "b.txt"

bdict = dict.fromkeys(open(bfile).readlines())

for line in open(afile):
    if line not in bdict:
        print line,

</F> 






More information about the Python-list mailing list