Intersection of lists/sets -- with a catch

George Sakkis gsakkis at rutgers.edu
Tue Oct 18 19:11:01 EDT 2005


"Carl Banks" <invalidemail at aerojockey.com> wrote:

> Howabout something like this (untested):
>
> class CmpProxy(object):
>     def __init__(self,obj):
>         self.obj = obj
>     def __eq__(self,other):
>         return (self.obj.att_a == other.obj.att_b
>                 and self.obj.att_b == other.obj.att_b)
>     def __hash__(self):
>         return hash((self.obj.att_a,self.obj.att_b))
>
> set_a = set(CmpProxy(x) for x in list_a)
> set_b = set(CmpProxy(y) for y in list_b)
> overlaps = [ z.obj for z in set_a.intersection(set.b) ]

Or more generally:

class Comparable(object):
    def __init__(self, obj, key):
        self.obj,self._key = obj,key
    def __hash__(self):
        return hash(self._key(self.obj))
    def __eq__(self, other):
        return self._key(self.obj) == self._key(other.obj)


if __name__ == '__main__':
    some_list = ["hello", "world"]
    another_list = ["HeLlO", "word"]
    key = str.lower
    some_set = set(Comparable(x,key) for x in some_list)
    another_set = set(Comparable(x,key) for x in another_list)
    print "overlaps:", [x.obj for x in some_set.intersection(another_set)]
    print "unique_some:", [x.obj for x in some_set.difference(another_set)]
    print "unique_another:", [x.obj for x in another_set.difference(some_set)]


George





More information about the Python-list mailing list