[Tutor] 1 to N searches in files

Dave Angel d at davea.name
Mon Dec 3 16:58:32 CET 2012


On 12/03/2012 10:46 AM, Spectral None wrote:
> <snip>
> Hi Dave
> 
> Your solution seems to work:
> 
> setA = set(FileA)
> setB = set(FileB)
> 
> for line in setB:
>   if line in setA:
>     matched_lines.writelines(line)
>   else:
>     non_matched_lines.writelines(line)
> 
> There are no duplicates in the results as well. Thanks for helping out
> 

You didn't specify whether you wanted dups to be noticed.  You had said
that there were none in A, but B was unspecified.

The other question is order.  If you want original order, you'd have to
omit the setB step, and iterate on FileB.  And then eliminate dups as
you go.

Or if you want sorted order without dups, you could simply iterate on
sorted(setB).


-- 

DaveA


More information about the Tutor mailing list