Possible Duplicate:
Python: simple list merging based on intersections
I am trying to classify objects. Each object is identified by a unique identifier property called id. So my classification logic goes like this. First i prepare a list of objects and then the classification function takes 2 objects at a time and returns a frozenset containing their id. So if object1 and object5 are in the same category a frozenset(id1,id5) is returned. Now i keep adding these frozensets to a set so in the end i have a set like this
matched_set=(
frozenset(id1,id2),
frozenset(id9,id3),
frozenset(id9,id2),
frozenset(id24,id22),
frozenset(id1,id23),
frozenset(id25,id24),
frozenset(id30,id24)
)
Now because objects with id1 and id2 are in the same category, objects with id9 and id3 are in the same category, objects with id9 and id2 are in the same category, objects with id1,id2,id3,id9 should be in same category. So i should have a set like this set(id1,id2,id3,id9)
Can someone provide an algorithm to do so?
Thanks