473,287 Members | 3,295 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,287 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 1414
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?...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.