473,837 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting some element from sets.Set

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
13 31651
jm*******@no.sp am.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
On Thu, 03 May 2007 23:08:33 -0700, jm*******@no.sp am.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_elem ent)

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

some_element = list(s)[0]
--
Steven D'Aprano

May 4 '07 #3
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
On May 4, 11:34 am, Peter Otten <__pete...@web. dewrote:
jm.sur...@no.sp am.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
jm*******@no.sp am.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
On May 4, 6:23 pm, "jm.sur...@no.s pam.gmail.com" <jm.sur...@gmai l.com>
wrote:
On May 4, 11:34 am, Peter Otten <__pete...@web. dewrote:
jm.sur...@no.sp am.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.someatt ribute == 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
jm*******@no.sp am.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.up per() 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
On 4 Mai, 10:23, "jm.sur...@no.s pam.gmail.com" <jm.sur...@gmai l.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
On May 4, 5:06 pm, John Machin <sjmac...@lexic on.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2595
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 style specified have a different font-size than the body's font-size. - If I change the body's font-size, a button with no style specified remains at the same font-size
6
2441
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 has an internal pointer to a list of pointer of Element type.
4
1902
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 statements, it returns me, for example (, , ) and sometimes , (, ) or () in my call, i give eg (a,b,c) = db.execute(stmt) so that it returns me (, ,
0
2186
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 such. I currently have a set of XML tables that represent serialized arrays that I need to be able to read into a VB application, deserialize to arrays, modify some values and then re-serialize the arrays.In googling to various resources I...
4
429
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 easy approach?
15
2847
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 value. I know I can do this by setting the form element to 'readonly' but that's not possible as I'll need to modify that value elsewhere in my JS. Anyway... I know I can do something like this: document.forms.elements.focus().
2
2377
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 XSLT, but it doesn't work correctly :-( Example:
0
1894
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 if I mix plain text with html formatted text and set the isBodyHTML = true that I get a continuous line of plain text without carriage returns and proper HTML. I've a checkbox that if checked sets the IsBodyHTML=true.
2
1479
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 satisfy me, eg: * list(myset) * iter(myset).next() * set(myset).pop()
0
9696
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10902
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10288
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9420
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7824
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5680
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.