如何合并列表中所有相交的元组?

时间:2019-05-29 21:26:07

标签: python python-3.x

我有一个元组列表,并且希望将相交的所有元组合并在一起。从数学上讲,每个元组都是一个集合,但是在高级代码中却没有这样键入。

我当前的解决方案包括将每个元组与其他所有元组进行比较,并合并直到不存在重叠。有没有更有效的方法或Pythonic的方法来完成此任务?

当前解决方案(按预期工作):

    def overlap(tup_list): #pass a list of tuples
        """
        For a list of tuples, merge together any tuples that intersect at at least one element.
        Returns a reduced set of tuples.
        """
        set_list = [tuple(sorted(set(x))) for x in tup_list] #first-order cleanup: reduce duplication within elements by making each element a set. Sorted for consistent order of tuple elements. Then converted back to tuple so hashable.
        superset = set(set_list) #second-order clean up: reduce duplication of tuples by making governing list a set
        reduced = False #init "reduced" bool
        while reduced == False: #run until fully reduced
            op = False #init/reset "operations performed" bool
            for i in superset.copy():
                for j in superset.copy():
                    if set(i) == set(j): #if same set, skip
                        continue
                    elif set(i).isdisjoint(set(j)): #if sets don't overlap, skip
                        continue
                    else: #sets do overlap, append union; remove individuals
                        new_member = tuple(set(i).union(set(j))) #union tuple
                        if new_member not in superset:
                            superset.add(new_member)
                            op = True
                        if i in superset and i != new_member:
                            superset.remove(i)
                            op = True
                        if j in superset and j != new_member:
                            superset.remove(j)
                            op = True
            if op == False:
                reduced = True #if no operations performed, consider to be fully reduced
        return(superset)

示例:

input: [(1,2,3),(4,5),(6,7,1)]
returns: {(4, 5), (1, 2, 3, 6, 7)}
input: [(1,2,3),(1,2,3),(4,5,6),(3,4),(10,)]
returns: {(10,), (1, 2, 3, 4, 5, 6)}

(符合预期)

0 个答案:

没有答案