Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting some element from sets.Set

9 views
Skip to first unread message

jm.s...@gmail.com

unread,
May 4, 2007, 2:08:33 AM5/4/07
to
It is not possible to index set objects. That is OK.
But, what if I want to find some element from the Set.

from sets import Set
s = Set( range(12 )

if I do pop, that particular element gets removed.
I do not want to remove the element, but get some element
from the Set.

s.some_element() # Is not available

Is there a way to do this. I am doing it like this:

for x in s: break

Now x is /some_element/ from s.

-
Suresh

Peter Otten

unread,
May 4, 2007, 2:34:22 AM5/4/07
to
jm.s...@no.spam.gmail.com wrote:

A set is probably not the appropriate container then. What is your use case?

Peter

Steven D'Aprano

unread,
May 4, 2007, 2:40:35 AM5/4/07
to
On Thu, 03 May 2007 23:08:33 -0700, jm.s...@no.spam.gmail.com wrote:

> It is not possible to index set objects. That is OK.
> But, what if I want to find some element from the Set.
>
> from sets import Set
> s = Set( range(12 )
>
> if I do pop, that particular element gets removed.
> I do not want to remove the element, but get some element
> from the Set.
>
> s.some_element() # Is not available

Looking at help(sets.Set), it seems that there is no direct way to ask for
a single element of a set, except with pop. So you can pop an element,
then add it back in:

some_element = s.pop()
s.add(some_element)

Another solution is to extract all the elements, then pick one:

some_element = list(s)[0]


--
Steven D'Aprano

Raymond Hettinger

unread,
May 4, 2007, 2:41:48 AM5/4/07
to
> I do not want to remove the element, but get some element
> from the Set.
. . .

> Is there a way to do this. I am doing it like this:
>
> for x in s: break
>
> Now x is /some_element/ from s.

That is one way to do it. Another is to write:
x = iter(s).next()

One more approach:

x = s.pop()
s.add(x)


Raymond Hettinger

jm.s...@gmail.com

unread,
May 4, 2007, 4:23:49 AM5/4/07
to
On May 4, 11:34 am, Peter Otten <__pete...@web.de> wrote:

Peter, I need to do a lot of union and intersection operations on
these elements. So, set is a must for me in this case.

In the particular case, I have to read an attribute from any one of
the elements, which one doesn't matter because this attribute value is
same across all elements in the set.

-
Suresh

Peter Otten

unread,
May 4, 2007, 4:48:29 AM5/4/07
to
jm.s...@no.spam.gmail.com wrote:

> On May 4, 11:34 am, Peter Otten <__pete...@web.de> wrote:

>> A set is probably not the appropriate container then. What is your use
>> case?

> Peter, I need to do a lot of union and intersection operations on


> these elements. So, set is a must for me in this case.
>
> In the particular case, I have to read an attribute from any one of
> the elements, which one doesn't matter because this attribute value is
> same across all elements in the set.

Convinced -- I lacked the imagination for this scenario :-)

Peter

John Machin

unread,
May 4, 2007, 6:06:57 PM5/4/07
to
On May 4, 6:23 pm, "jm.sur...@no.spam.gmail.com" <jm.sur...@gmail.com>
wrote:

> On May 4, 11:34 am, Peter Otten <__pete...@web.de> wrote:
>
>
>
> > jm.sur...@no.spam.gmail.com wrote:
> > > It is not possible to index set objects. That is OK.
> > > But, what if I want to find some element from the Set.
>
> > > from sets import Set
> > > s = Set( range(12 )
>
> > > if I do pop, that particular element gets removed.
> > > I do not want to remove the element, but get some element
> > > from the Set.
>
> > > s.some_element() # Is not available
>
> > > Is there a way to do this. I am doing it like this:
>
> > > for x in s: break
>
> > > Now x is /some_element/ from s.
>
> > A set is probably not the appropriate container then. What is your use case?
>
> > Peter
>
> Peter, I need to do a lot of union and intersection operations on
> these elements. So, set is a must for me in this case.

Errmm, union and intersection operations each apply to two (or more)
sets, not to the elements of a set.

>
> In the particular case, I have to read an attribute from any one of
> the elements, which one doesn't matter because this attribute value is
> same across all elements in the set.
>

Well, I'm not so easily convinced as some people :-)

You say the rule is that each element in a set has
element.someattribute == somevalue.

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?

You have a rather redundant data structure, and perhaps should
consider refactoring it.

A physical analogy: somebody has packed fruit into boxes, all the
bananas in one box, the oranges in a second box, the apples in a third
box, etc, without writing labels on the boxes. The somebody has
however laboriously pasted a label ("banana", "orange", etc) on each
piece of fruit. You now need to grab an object from each box,
presumably to identify the box contents. How close is this analogy to
your scenario?

Cheers,
John

Andrew McLean

unread,
May 7, 2007, 9:49:03 AM5/7/07
to
jm.s...@no.spam.gmail.com wrote:
> In the particular case, I have to read an attribute from any one of
> the elements, which one doesn't matter because this attribute value is
> same across all elements in the set.

Someone else pointed out that there might be better data structures. If
performance was not an issue one approach would be illustrated by the
following:

>>> Q=set(['A','a'])
>>> list(set(x.upper() for x in Q))
['A']

This has the benefit that it does not assume all the elements of the set
have the same value of the given attribute.

Again not very efficient:

>>> list(Q)[0]
'A'

I'm guessing this would be quicker

>>> iter(Q).next()
'A'

Christopher Arndt

unread,
May 7, 2007, 9:56:22 AM5/7/07
to
On 4 Mai, 10:23, "jm.sur...@no.spam.gmail.com" <jm.sur...@gmail.com>

wrote:
> > > It is not possible to index set objects. That is OK.
> > > But, what if I want to find some element from the Set.
>
> In the particular case, I have to read an attribute from any one of
> the elements, which one doesn't matter because this attribute value is
> same across all elements in the set.

Just to clarify: do you want to just get an *arbitrary* element from
the set or do you want to find a *specific* element in the set?

In the first case you have to convert it to a list (as pointed out
earlier in this thread):

>>> s = set(range(10))
>>> list(s)[0]
0

In the second case, just use th "in" operator:

>>> 10 in s
False
>>> 5 in s
True

Since you have to have a reference to the object for whose membership
you are testing, you can just use this object.

Stupid example:

>>> class Point:
... def __init__(self, x, y):
... self.x = x
... self.y = y
...
>>> l = [Point(n,n+2) for n in range(10)]
>>> s = set(l)
>>> Point(0,2) in s
False
>>> l[0] in s
True
>>>
>>> l[0].x,l[0].y
(0, 2)


Chris

tut...@gmail.com

unread,
May 9, 2007, 11:42:24 AM5/9/07
to
On May 4, 5:06 pm, John Machin <sjmac...@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.

Steven Bethard

unread,
May 9, 2007, 12:41:45 PM5/9/07
to
tut...@gmail.com wrote:
> 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.

You can find out::

$ python -m timeit -s "s = set('abcdef')" "x = iter(s).next()"
1000000 loops, best of 3: 0.399 usec per loop

$ python -m timeit -s "s = set('abcdef')" "x = s.pop(); s.add(x)"
1000000 loops, best of 3: 0.339 usec per loop

So it looks like it's more efficient to use s.pop() + s.add().

STeVe

Marc 'BlackJack' Rintsch

unread,
May 9, 2007, 1:12:08 PM5/9/07
to
In <1178725344.4...@n59g2000hsh.googlegroups.com>,
tut...@gmail.com wrote:

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

Give it a name and it gets more readable:

def get_name(setobj):
return iter(setobj).next()

Ciao,
Marc 'BlackJack' Rintsch

tut...@gmail.com

unread,
Jun 28, 2007, 6:40:39 PM6/28/07
to
On May 9, 11:41 am, Steven Bethard <steven.beth...@gmail.com> wrote:

> $ python -m timeit -s "s = set('abcdef')" "x = s.pop(); s.add(x)"

It's interesting that that's faster. I'd be hesitant to use that in a
real program because it's so unattractive (as is the '.next()'
approach). To me a builtin method would be worthwhile in this case.

Raymond Hettinger

unread,
Jun 29, 2007, 4:44:28 AM6/29/07
to
> $ python -m timeit -s "s = set('abcdef')" "x = iter(s).next()"
> 1000000 loops, best of 3: 0.399 usec per loop
>
> $ python -m timeit -s "s = set('abcdef')" "x = s.pop(); s.add(x)"
> 1000000 loops, best of 3: 0.339 usec per loop
>
> So it looks like it's more efficient to use s.pop() + s.add().


There's a faster, cleaner way:

>python -m timeit -s "s = set('abcdef')" "x = iter(s).next()"

1000000 loops, best of 3: 0.539 usec per loop

>python -m timeit -s "s = set('abcdef')" "x = s.pop(); s.add(x)"

1000000 loops, best of 3: 0.465 usec per loop

>python -m timeit -s "s = set('abcdef')" "for x in s: break"
1000000 loops, best of 3: 0.175 usec per loop


FWIW, the latter approach is general purpose and works with any
iterable (useful for reading the first line of file objects for
example).


Raymond

0 new messages