[perl-python] exercise: partition a list by equivalence

Brian Beck exogen at gmail.com
Fri Feb 18 20:13:10 EST 2005


Brian Beck wrote:
 > [code]

Whoops, that should say:

def merge(pairs):
     pairs = set(tuple(sorted(p)) for p in pairs)
     merged = []
     # Each loop will result in a new, complete sublist in the result.
     while pairs:
         p = set(pairs.pop())
         remove = set([])
         for pair in pairs:
             pairSet = set(pair)
             if pairSet & p:
                 p |= pairSet
                 remove.add(pair)
         pairs -= remove
         merged.append(list(p))
     return merged

--
Brian Beck
Adventurer of the First Order



More information about the Python-list mailing list