【问题标题】:Merging pair set to list [duplicate]合并对设置为列表[重复]
【发布时间】:2020-02-27 12:59:45
【问题描述】:

我有输入集或列表,例如

输入:

[[0,1],[0,2],[1,3],[4,5],[6]] or [[0,1],[0,2],[1,2],[1,3],[4,5],[6]]

我希望输出为

[[0,1,2,3],[4,5],[6]]

基本上它会创建包含共享对的列表。数据类型可以不同。我正在写python。欢迎使用伪代码或 python 代码。谢谢

【问题讨论】:

  • 你尝试过什么,它到底有什么问题?它也是一个集合还是一个列表(你显示后者) - 这是一个很大的区别。
  • 如果使用 set 或 tuple 更容易,我可以使用和更改数据类型。
  • @Zek'iB.Ulu,[[6, 0, 1],[0,2],[1,3],[4,5],[6]] 的结果是什么?

标签: python algorithm list set


【解决方案1】:

经过实验我找到了答案:

def pairs_to_whole(touching_pairs:list):
    out = []
    while len(touching_pairs)>0:
        first, *rest = touching_pairs
        first = set(first)

        lf = -1
        while len(first)>lf:
            lf = len(first)

            rest2 = []
            for r in rest:
                if len(first.intersection(set(r)))>0:
                    first |= set(r)
                else:
                    rest2.append(r)     
            rest = rest2

        out.append(first)
        touching_pairs = rest
    return out

【讨论】:

    【解决方案2】:

    一种可能是使用递归:

    def get_groups(d, l, s=[]):
       if not (r:=[i for i in d if any(j in l for j in i) and i not in s]):
          yield list(set(l))
          if (new_r:=[i for i in d if i not in s]):
             yield from get_groups(d, new_r[0], s=s+[new_r[0]])
       else:
          yield from get_groups(d, l+[i for k in r for i in k], s=s+r)
    
    vals = [[0,1],[0,2],[1,2],[1,3],[4,5],[6]]
    print(list(get_groups(vals, vals[0])))
    

    输出:

    [[0, 1, 2, 3], [4, 5], [6]]
    

    【讨论】:

    • 感谢您的评论!但是代码给出了 RecursionError: maximum recursion depth exceeded in comparison
    • @Zek'iB.Ulu 你能发布你尝试过的输入样本吗?
    • 我只是运行你的整个代码。我想输入样本必须是 vals
    • @Zek'iB.Ulu Strange,它在两个样本以及 RomanPerekhrest 建议的 [[6, 0, 1],[0,2],[1,3],[4,5],[6]] 上都运行良好。你用的是什么版本的 Python?
    【解决方案3】:

    您可以使用集合字典来表示使用最小值作为键的组。这将允许您通过直接访问由其成员组成的组来逐步合并组。逐步消除合并的子组(直到不再删除)将为您留下所需的分组。

    from collections import deque
    def groupPairs(pairs):
        links    = dict()          # each value paired to a set of larger values
        addLinks = deque()         # values to be added to a group
        for a,*b in map(sorted,pairs):
            links[a] = set()       # create empty group for each lowest value
            addLinks.append((a,b)) # queue addition of paired values
        while addLinks: 
            a,values = addLinks.popleft()  # next addition of pairs 
            if not values or a not in links: continue
            links[a].update(values)       # add the paired values to the group
            addLinks.append((a,[c for b in values for c in links.pop(b,[])])) # remove and queue more
        return [ [k,*v] for k,v in links.items() ]
    
    P = [[0,1],[0,2],[1,3],[4,5],[6]]
    gp = groupPairs(P)
    print(gp)
    # [[0, 1, 2, 3], [4, 5], [6]]
    
    P = [[0,1],[0,2],[1,2],[1,3],[4,5],[6]]
    gp = groupPairs(P)
    print(gp)
    # [[0, 1, 2, 3], [4, 5], [6]]
    
    P = [[6, 0, 1],[0,2],[1,3],[4,5],[6]]
    gp = groupPairs(P)
    print(gp)
    # [[0, 1, 2, 3, 6], [4, 5]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      相关资源
      最近更新 更多