Set like feature

Hari Pulapaka hari04 at
Mon Nov 15 23:36:50 CET 2004

Alex Martelli wrote:
> Do you want to compare corresponding fields?  That's the only way I
> read that 'column by column basis', and thus I don't see what sets
> possibly have to do with it.
> Do you want to compare each row with every other row?  I also note in
> your example that the number of fields in each row appear to be
> variable, so how do you want to deal with 'missing' fields?

I want to comapre every element in each row with the element in the
remaining rows having the same column position. The rows need not have
the same number of elements, in which case I have to do some more
thinking :)

I was thinking of making each row of the array as a set and then
comparing each row of the array with the compare function being the set
intersection operation.

You have pretty much captured what I was thinking, and my solution is
also similar to what you showed.

> Too many unanswered questions, I guess.  But for some specified set
> answers to those question, you might do...:
> def compare_fields(i, j, base, other):
>     for k, f1, f2 in zip(xrange(sys.maxint), base, other):
>         if f1 != f2:
>             print 'DIFF', i, j, k, repr(f1), repr(f2)
> def lots_of_compares(list_of_strings):
>     list_of_lists_of_fields = [row.split() for row in
>     num_rows = len(list_of_lists_of_fields)
>     for i in xrange(num_rows):
>         base_row = list_of_lists_of_fields[i]
>         for j in xrange(i+1, num_rows):
>             compare_fields(i, j, base_row,

Thanks for your help.

> You can do better with enumerate, itertools and other things which
> didn't have, but sets wouldn't help.  Now, I hope this clarifies the
> many unanswered questions which your 'specs' leave open, so you can
> out exactly what you want.
> And, btw: upgrate to 2.4.  Sets or no sets, the performance
> by itself will be vastly sufficient to repay whatever inconvenience
> think the upgrade might 

Not in my hands. 

- Hari

> Alex

More information about the Python-list mailing list