基于重复项目的组Python列表

时间:2015-08-17 19:01:37

标签: python algorithm grouping

这个问题与这个Group Python list of lists into groups based on overlapping items非常相似,事实上它可以被称为重复。

基本上,我有一个子列表列表,其中每个子列表包含一些整数(这个数字在子列表中是不一样的)。我需要对共享一个或多个整数的所有子列表进行分组。

我之所以要问一个新的单独问题,是因为我试图让Martijn Pieters great answer没有运气。

这是MWE:

def grouper(sequence):
    result = []  # will hold (members, group) tuples

    for item in sequence:
        for members, group in result:
            if members.intersection(item):  # overlap
                members.update(item)
                group.append(item)
                break
        else:  # no group found, add new
            result.append((set(item), [item]))

    return [group for members, group in result]


gr = [[29, 27, 26, 28], [31, 11, 10, 3, 30], [71, 51, 52, 69],
      [78, 67, 68, 39, 75], [86, 84, 81, 82, 83, 85], [84, 67, 78, 77, 81],
      [86, 68, 67, 84]]

for i, group in enumerate(grouper(gr)):
    print 'g{}:'.format(i), group

我得到的输出是:

g0: [[29, 27, 26, 28]]
g1: [[31, 11, 10, 3, 30]]
g2: [[71, 51, 52, 69]]
g3: [[78, 67, 68, 39, 75], [84, 67, 78, 77, 81], [86, 68, 67, 84]]
g4: [[86, 84, 81, 82, 83, 85]]

最后一组g4应与g3合并,因为其中的列表共享项818384,甚至一个重复的元素应该足以让它们合并。

我不确定我是否应用了错误的代码,或者代码是否有问题。

2 个答案:

答案 0 :(得分:1)

如果您将每个子列表转换为一个集合,而不是因为您对内容而不是订单感兴趣,那么听起来就像设置合并一样,因此集合是最佳的数据结构选择。请参阅:http://rosettacode.org/wiki/Set_consolidation

答案 1 :(得分:1)

您可以将要执行的合并描述为集合合或连接组件问题。我倾向于使用现成的集合合并算法,然后根据特定情况进行调整。例如,IIUC,您可以使用类似

的内容
def consolidate(sets):
    # http://rosettacode.org/wiki/Set_consolidation#Python:_Iterative
    setlist = [s for s in sets if s]
    for i, s1 in enumerate(setlist):
        if s1:
            for s2 in setlist[i+1:]:
                intersection = s1.intersection(s2)
                if intersection:
                    s2.update(s1)
                    s1.clear()
                    s1 = s2
    return [s for s in setlist if s]

def wrapper(seqs):
    consolidated = consolidate(map(set, seqs))
    groupmap = {x: i for i,seq in enumerate(consolidated) for x in seq}
    output = {}
    for seq in seqs:
        target = output.setdefault(groupmap[seq[0]], [])
        target.append(seq)
    return list(output.values())

给出了

>>> for i, group in enumerate(wrapper(gr)):
...     print('g{}:'.format(i), group)
...     
g0: [[29, 27, 26, 28]]
g1: [[31, 11, 10, 3, 30]]
g2: [[71, 51, 52, 69]]
g3: [[78, 67, 68, 39, 75], [86, 84, 81, 82, 83, 85], [84, 67, 78, 77, 81], [86, 68, 67, 84]]

(因使用词典而无法保证顺序。)

相关问题