[Tutor] Comparing lines in two files, writing result into a t hird file

Scott Widney SWidney@ci.las-vegas.nv.us
Wed Apr 23 19:19:02 2003

> ...  So, I'm trying to learn as I go, but I gotta produce stuff
> fairly quickly and I don't have a lot of spare time to spend on
> my learning curve.  I do like Python though, and I'm glad I have
> something to use it for.
> So, anyway, now that I think about it a little bit, perhaps sorted
> order doesn't really matter.  One responder suggested that I use
> dictionaries in my code structure.  My understanding is that
> dictionaries are mappings, not sequences, so I guess ordering is
> not really relevant here.  FWIW, It does turn out that the files
> I'm working with are always ordered sequentially when I get them.
> Concerning dictionaries, do you think dictionaries is the structure 
> to use ? If so, I'll try to spend some time reading up on
> dictionaries.  I do remember having problems reading a file into a
> dictionary when I tried it a year ago or so.

Since you're pressed for time, I can give you a basic script using a

d = {} # Start with an empty dictionary

f1 = file('file1.txt', 'r')
for num in f1.readlines():
    num = num.strip()       # get rid of any nasty newlines
    d[num] = 1              # and populate

f2 = file('file2.txt', 'r')
for num in f2.readlines():
    num = num.strip()                # again with the newlines
    if d.has_key(num): d[num] += 1   # <- increment value, or
    else: d[num] = 1                 # <- create a new key

nums = d.keys()
f3 = file('file3.txt', 'w')
for num in nums:
    f3.write(num)          # Here we put the
    if d[num] > 1:         # newlines back, either
        f3.write("*\n")    # <- with
    else:                  # or
        f3.write("\n")     # <- without
f3.close()                 # the asterisk

Should be fairly quick. And it's certainly easier to flash-parse with the
naked eye than a value-packed list comprehension.