By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,994 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,994 IT Pros & Developers. It's quick & easy.

Getting some element from sets.Set

P: n/a
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

May 4 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
jm*******@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
May 4 '07 #2

P: n/a
On Thu, 03 May 2007 23:08:33 -0700, jm*******@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

May 4 '07 #3

P: n/a
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

May 4 '07 #4

P: n/a
On May 4, 11:34 am, Peter Otten <__pete...@web.dewrote:
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.

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

May 4 '07 #5

P: n/a
jm*******@no.spam.gmail.com wrote:
On May 4, 11:34 am, Peter Otten <__pete...@web.dewrote:
>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

May 4 '07 #6

P: n/a
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.dewrote:
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

May 4 '07 #7

P: n/a
jm*******@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'
May 7 '07 #8

P: n/a
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

May 7 '07 #9

P: n/a
On May 4, 5:06 pm, John Machin <sjmac...@lexicon.netwrote:
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.

May 9 '07 #10

P: n/a
tu*****@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
May 9 '07 #11

P: n/a
In <11**********************@n59g2000hsh.googlegroups .com>,
tu*****@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
May 9 '07 #12

P: n/a
On May 9, 11:41 am, Steven Bethard <steven.beth...@gmail.comwrote:
$ 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.

Jun 28 '07 #13

P: n/a
$ 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

Jun 29 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.