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 13 31601 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
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
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
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 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
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 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'
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
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. 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
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
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.
$ 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul Gobée |
last post by:
What is the parent element of a button (IE6 on Win, transitional mode)?
Is it the BODY, the browser default stylesheet, or something else?
Contra "body is parent-element":
- Buttons with no...
|
by: Jon Slaughter |
last post by:
Is there any way to get the class type from a pointer?
I'm working on a class that acts as a set:
I have two classes, one called FSet and the other called Element.
FSet inherets Element and...
|
by: s99999999s2003 |
last post by:
hi
the database "execute" function returns a list of logical results. Each
logical result is a list of row tuples, as explained in the documents.
everytime i use it to execute various...
|
by: Wayne Wengert |
last post by:
I have a basic understanding of XML and I have a similar basic grasp of
VB.NET for developing Windows applications. I am still struggling with the
when/where/how in the proper use of OO classes and...
|
by: Davy |
last post by:
Hi all,
I have a Set contain several elements.
I want to do:
(1) Select one element from Set randomly;
(2) Delete this element from Set;
(3) If Set!=empty, goto(1);else, end.
Is there any...
|
by: Christoph |
last post by:
I'm trying to make it so that if the user sets focus to a form element
(text), it automatically sets focus to the very next form element. This
will, in effect, prevent the user from modifying the...
|
by: jkflens |
last post by:
Hello,
i convert one XML-document by using XSLT into another XML-document.
First change all attributes to elements is no problem.
Then i try to insert a new element into the new document by...
|
by: Rey |
last post by:
Howdy all.
Am using visual web developer 2005 (vb), xp pro sp2.
In testing of the system.net.mail to send email from an aspx page where
I'm pulling the email contents from a textbox, find that...
|
by: Arnaud Delobelle |
last post by:
Hi all,
I often find myself needing to get (non-destructively) the value of
the member of a singleton set. Is there a good way to do this (as an
expression?) None of the ones I can think of...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |