473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Set operations on object attributes question

Hi,

I have run into something I would like to do, but am not sure how to
code it up. I would like to perform 'set-like' operations (union,
intersection, etc) on a set of objects, but have the set operations
based on an attribute of the object, rather than the whole object.

For instance, say I have (pseudo-code):

LoTuples1 = [(1,1,0),(1,2,1),(1,3,3)]
Set1=set(LoTuples1)
LoTuples2 = [(2,1,3),(2,2,4),(2,3,2)]
Set2=set(LoTuples2)

What I would like to be able to do is:

Set3 = Set1union(Set2)
Set3.intersection(Set2, <use object[2]>)

to return:
set([(2,1,3), (1,3,3)])

How can one do this operation?

Thanks,
Duane

Oct 23 '07 #1
5 1418
On 2007-10-23, TheSeeker <du***********@gmail.comwrote:
Hi,

I have run into something I would like to do, but am not sure how to
code it up. I would like to perform 'set-like' operations (union,
intersection, etc) on a set of objects, but have the set operations
based on an attribute of the object, rather than the whole object.

For instance, say I have (pseudo-code):

LoTuples1 = [(1,1,0),(1,2,1),(1,3,3)]
Set1=set(LoTuples1)
LoTuples2 = [(2,1,3),(2,2,4),(2,3,2)]
Set2=set(LoTuples2)

What I would like to be able to do is:

Set3 = Set1union(Set2)
Set3.intersection(Set2, <use object[2]>)

to return:
set([(2,1,3), (1,3,3)])

How can one do this operation?
Put your data in a class, and implement __hash__ and __eq__
Finally, put your objects in sets.

Albert

Oct 23 '07 #2
[Duane]
LoTuples1 = [(1,1,0),(1,2,1),(1,3,3)]
Set1=set(LoTuples1)
LoTuples2 = [(2,1,3),(2,2,4),(2,3,2)]
Set2=set(LoTuples2)

What I would like to be able to do is:

Set3 = Set1union(Set2)
Set3.intersection(Set2, <use object[2]>)

to return:
set([(2,1,3), (1,3,3)])

How can one do this operation?
Conceptually, there is more than one operation going on. First,
finding the attributes shared in both sets:
ca = set(t[2] for t in LoTuples1) & set(t[2] for t in LoTuples2)
which gives:
set([3])

Second, find any tuple which has that attribute (including multiple
results for the same attribute):
set(t for t in (LoTuples1 + LoTuples2) if t[2] in ca)
which returns:
set([(2, 1, 3), (1, 3, 3)])

Wanting multiple results for the same attribute value (i.e. both
(2,1,3) and (1,3,3) have 3 in the second position) is why multiple
steps are needed; otherwise, the behavior of intersection() is to
return a single representative of the equivalence class.
Raymond

Oct 23 '07 #3
Hi,

Thanks for the response! (See below for more discussion)

On Oct 23, 10:39 am, Raymond Hettinger <pyt...@rcn.comwrote:
[Duane]
LoTuples1 = [(1,1,0),(1,2,1),(1,3,3)]
Set1=set(LoTuples1)
LoTuples2 = [(2,1,3),(2,2,4),(2,3,2)]
Set2=set(LoTuples2)
What I would like to be able to do is:
Set3 = Set1union(Set2)
Set3.intersection(Set2, <use object[2]>)
to return:
set([(2,1,3), (1,3,3)])
How can one do this operation?

Conceptually, there is more than one operation going on. First,
finding the attributes shared in both sets:
ca = set(t[2] for t in LoTuples1) & set(t[2] for t in LoTuples2)
which gives:
set([3])
In my use case, I already know object[2] is the key I wish to use, so
I could do this without the first step.
I am thinking of object[n] as the 'key' I wish to operate on, and the
other items in the tuple are useful attributes
I'll need in the end.
>
Second, find any tuple which has that attribute (including multiple
results for the same attribute):
set(t for t in (LoTuples1 + LoTuples2) if t[2] in ca)
which returns:
set([(2, 1, 3), (1, 3, 3)])

Wanting multiple results for the same attribute value (i.e. both
(2,1,3) and (1,3,3) have 3 in the second position) is why multiple
steps are needed; otherwise, the behavior of intersection() is to
return a single representative of the equivalence class.
This second operation is really much like what I cam up with before I
started looking into exploiting the power of sets
(this same operation can be done strictly with lists, right?)

Since what I _really_ wanted from this was the intersection of the
objects (based on attribute 2), I was looking for a set-based
solution,
kinda like a decorate - <set operation- undecorate pattern. Perhaps
the problem does not fall into that category.

Thanks for your help,
Duane

Oct 23 '07 #4
Since what I _really_ wanted from this was the intersection of the
objects (based on attribute 2), I was looking for a set-based
solution,
kinda like a decorate - <set operation- undecorate pattern. Perhaps
the problem does not fall into that category.
The "kinda" part is where the idea falls down. If you've decorated
the inputs with a key function (like the key= argument to sorted()),
then the intersection step will return only a single element result,
not both matches as you specified in your original request. IOW,
you cannot get set([(2, 1, 3), (1, 3, 3)]) as a result if both
set members are to be treated as equal.

It would be rather like writing set([1]).intersection(set([1.0]))
and expecting to get set([1, 1.0]) as a result. Does your
intuition support having an intersection return a set larger
than either of the two inputs?
Raymond

Oct 23 '07 #5
[Duane]
Since what I _really_ wanted from this was the intersection of the
objects (based on attribute 2), I was looking for a set-based
solution,
kinda like a decorate - <set operation- undecorate pattern. Perhaps
the problem does not fall into that category.
Decorating and undecorating do not apply to your example. If all the
input were decorated with new equality and hash methods, then the
intersection step would return just a single element, not the two that
you specified.

Compare this with ints and floats which already have cross-type
equality and hash functions. Running, set([1]).intersection([1.0]),
would you expect set([1.0]) or set([1, 1.0])? Your example specified
the latter behavior which has the unexpected result where the
set intersection returns more elements than exist in either input.
Raymond

Oct 23 '07 #6

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

Similar topics

4
by: Avi Kak | last post by:
Hello: Please forgive me if my question is too silly or just not well-formed. Wesley Chun in his book (Core Python Programming) says that **everything** in Python is an object. So I became...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
2
by: Anders Borum | last post by:
Hello! I was looking at marking objects with a changed state, once properties have been changed. The reason behind this is, that I would like to enlist such objects for processing in a...
13
by: Immanuel Goldstein | last post by:
Obtained under the Freedom of Information Act by the National Security Archive at George Washington University and posted on the Web today, the 74-page "Information Operations Roadmap" admits that...
23
by: digitalorganics | last post by:
How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {}) newobj =...
9
by: tom arnall | last post by:
does anyone know of a utility to do a recursive dump of object data members? -- Posted via a free Usenet account from http://www.teranews.com
3
by: vasilip | last post by:
I noticed that DB2 does not support bitwise operations but I found some UDFs that seem to give this functionality.. There seems to be a consensus that bitoperations are not usefull (or bad...
8
by: Hussein B | last post by:
Hey, I noted that Python encourage the usage of: -- obj.prop = data x = obj.prop -- to set/get an object's property value. What if I want to run some logic upon setting/getting a property?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
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...
0
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...

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.