Set like feature

Alex Martelli aleaxit at yahoo.com
Mon Nov 15 18:05:02 EST 2004


Hari  Pulapaka <hari04 at gmail.com> wrote:

> 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.

Sets have no order, so that just woudln't work the way you state it.
Rows 'a b' and 'b a' would appear identical, so the "having the same
column position" condition would not be respected.

You could maybe use a set(enumerate(therow.split())) -- but intersecting
such sets would be of dubious utility.  Maybe you mean symmetric
difference (union minus intersection), but even then you'd still have to
proceed in order to investigate which item of that difference comes from
which of the two rows (assuming you do care -- hard to tell from here).

I believe gadfly comes with a fast C-coded extension called kjbuckets
which might help with this kind of things (and a Python-coded
'fallback', not all that fast but easily portable, too).  You might want
to investigate that, if there's a chance you could get C-coded
extensions installed on your Python 2.2 installation.


Alex



More information about the Python-list mailing list