comparing strings

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Jul 31 08:44:24 EDT 2002


"jadedlime" <jadedlime at hotmail.com> wrote in
news:3d47d6b9$1_6 at news.teranews.com: 
> I appreciate any and all help on this problem, part of the code is
> included. this seems to produce the matches, but not the mismatches.
> 
> for key in dictionarylist:
>         for item in journallist:
>                 if re.search(key, item):
>                         output.write("found\t" + item + "\n")
                          break
>         else:
>                 output2.write("not found\t" + item + "\n")
> 
You don't want that else clause on the for loop to execute if a match was 
found, so insert a break statement after the output.write (as shown above).

It isn't clear from your code why you are using regular expressions at all. 
The expressions in your example are all simple string matches. You could go 
a lot faster by dropping the ^ and $ from each dictionary key and simply 
testing each item for dictionary membership. Or does your real data have 
some more complex patterns?

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list