[Tutor] Searching for items in two lists.
Doug.Shawhan@gecits.ge.com
Doug.Shawhan@gecits.ge.com
Tue, 19 Mar 2002 16:52:16 -0500
Hi there.
I have a simple sorting routine that is giving me some unexpected results.
I have a file full of 9 character strings. I have a second file that
contains strings with those same 9 character srings that I wish to extract
to a list, then save to a file.
When I try my basic idea in the interpreter it works like so:
>>> l='freep, creeep, jeep'
>>> foop=l.split(',')
>>> turf=['eee','aaa']
>>> dorf=[]
>>> for thing in turf:
for each in foop:
if re.findall(thing, each)==[]:
print 'nope'
else: dorf.append(each)
nope
nope
nope
nope
nope
>>> dorf
[' creeep']
>>>
This is what I expected...
However, when I try it in a script:
------------------------script---------------
import re
#open list of stuff to look for
f1=open("\\tmp\\snarf.txt",'r')
#open 600k file to look in
f2=open("\\tmp\\new.csv",'r')
lookfor=f1.readlines()
lookin=f2.readlines()
answers=[]
for item in lookfor:
for searched_string in lookin:
if re.findall(item, searched_string) == []:
print 'nope'
else: answers.append(searched_string)
report=open("\\tmp\\report.csv",'w')
for all in answers:
report.write(all)
report.close()
---------------/script--------------------------------
I must have a flaw in my logic somewhere, cause my output file is on the
order of 15 megs!
Halp! What am I overlooking?
Thanks!