Getting some element from sets.Set

tutufan at gmail.com tutufan at gmail.com
Wed May 9 11:42:24 EDT 2007


On May 4, 5:06 pm, John Machin <sjmac... at lexicon.net> wrote:
> Errmm, union and intersection operations each apply to two (or more)
> sets, not to the elements of a set.

> You have n sets set0, set1, ....
>
> Let u be the number of unique somevalues (1 <= u <= n)
>
> If u > 1, then after setn = union(set0, set1), setn may not conform to
> the rule -- does this matter?


I've also previously run into the same need as the original poster.  I
no longer recall the details, but I think maybe I was implementing a
union/find type algorithm.  This basically involves partitioning a
universe set into partitions, where any element of a partition can be
used as a name/handle/etc for the partition in question.  Sets are the
obvious representation for these partitions, esp since they implement
union efficiently.  And given this representation, it's very obvious
to want to generate a "name" when you have a set in hand.  Since any
element of the set serves as a name (and you know the sets are all non-
empty), it'd be very nice to have a .element() method, or some such.
I guess "iter(s).next()" works okay, but it's not very readable, and I
wonder if it's efficient.

This is at least the second time this has come up, so maybe there is a
need.




More information about the Python-list mailing list