Re: Getting some element from sets.Set

2007-06-29 Thread Raymond Hettinger
  $ python -m timeit -s s = set('abcdef') x = iter(s).next()
  100 loops, best of 3: 0.399 usec per loop

  $ python -m timeit -s s = set('abcdef') x = s.pop(); s.add(x)
  100 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()
100 loops, best of 3: 0.539 usec per loop

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

python -m timeit -s s = set('abcdef') for x in s: break
100 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-06-28 Thread [EMAIL PROTECTED]
On May 9, 11:41 am, Steven Bethard [EMAIL PROTECTED] 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-09 Thread [EMAIL PROTECTED]
On May 4, 5:06 pm, John Machin [EMAIL PROTECTED] 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-09 Thread Steven Bethard
[EMAIL PROTECTED] 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()
 100 loops, best of 3: 0.399 usec per loop

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

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

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-09 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED],
[EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-07 Thread Andrew McLean
[EMAIL PROTECTED] 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'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-07 Thread Christopher Arndt
On 4 Mai, 10:23, [EMAIL PROTECTED] [EMAIL PROTECTED]
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-04 Thread John Machin
On May 4, 6:23 pm, [EMAIL PROTECTED] [EMAIL PROTECTED]
wrote:
 On May 4, 11:34 am, Peter Otten [EMAIL PROTECTED] wrote:



  [EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Getting some element from sets.Set

2007-05-04 Thread [EMAIL PROTECTED]
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-04 Thread Peter Otten
[EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-04 Thread Steven D'Aprano
On Thu, 03 May 2007 23:08:33 -0700, [EMAIL PROTECTED] 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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-04 Thread Raymond Hettinger
 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-04 Thread [EMAIL PROTECTED]
On May 4, 11:34 am, Peter Otten [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting some element from sets.Set

2007-05-04 Thread Peter Otten
[EMAIL PROTECTED] wrote:

 On May 4, 11:34 am, Peter Otten [EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list