Intersection of Object Attributes in Python
<p>So I made 2 sets of objects, like such.</p>

<pre><code>class 4digits:
    def __init__(self, value):
        self.value = value
        self.first = (value - (value % 100)) / 100
        self.last = value % 100
</code></pre>

<p>These are all 4 digits objects, so it would be like...</p>

<pre><code>object.value = 1234
object.first = 12
object.last = 34
</code></pre>

<p>if I had 2 sets, set1 and set2, I'd like to update set1 and set2 to only contain the objects where set1 has a last attribute that matches to set2's first attribute.</p>

<p>Such as...
1234 in set1 will match to 3456 in set 2.</p>

<p>I could do this as a list instead of a set pretty easily but a set feels a lot more efficient if I could figure out the procedure, but not sure how I can return an intersection of object's attributes. Any help.</p>

<p>Edited: Because I was asked for code, and my hypothetical wasn't exactly what I was actually doing.</p>

<p>Thanks to Jez I got this answer:</p>

<pre><code>def lastinfirst(list1, list2):
    firsts = set( l2.first for l2 in list2)
    newlist1 = [ l1 for l1 in list1 if l1.last in firsts]
    return newlist1

def firstinlast(list1, list2):
    lasts = set( l1.last for l1 in list1)
    newlist2 = [ l2 for l2 in list2 if l2.first in lasts]
    return newlist2
</code></pre>