【问题标题】:Separate nested list into groups with disjoint elements将嵌套列表分成具有不相交元素的组
【发布时间】:2020-06-17 16:32:32
【问题描述】:

我有一个看起来像这样的列表

my_list = [[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 12]]

我想找到将列表分成两组的最佳方法,这样每组中的各个元素就不会重叠。例如,在上面的示例中,两个组将是

group1 = [[1, 2, 3, 4], [4, 5, 6, 7]]
group2 = [[9, 10, 11, 12]]

这是因为 9、10、11、12 从未出现在 group1 的任何项目中。

【问题讨论】:

    标签: python list set networkx graph-theory


    【解决方案1】:

    类似于Combine lists with common elements,解决此问题的一种方法是从嵌套列表中定义一个图,将每个子列表作为path,并且 寻找connected components:

    import networkx as nx
    
    my_list = [[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 12]]
    
    G=nx.Graph()
    for l in my_list:
        nx.add_path(G, l)
    components = list(nx.connected_components(G))
    # [{1, 2, 3, 4, 5, 6, 7}, {9, 10, 11, 12}]    
    
    groups = []
    for component in components:
        group = []
        for path in my_list:
            if component.issuperset(path):
                group.append(path)
        groups.append(group)
    

    groups
    # [[[1, 2, 3, 4], [4, 5, 6, 7]], [[9, 10, 11, 12]]]
    

    【讨论】:

      【解决方案2】:

      这应该可以解决问题:

      my_list = [[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 12]]
      
      
      grp1 = []
      grp2 = []
      
      for i in range(len(my_list)):
          j = i+1
          if my_list[i] not in grp1:
              for ele in my_list[i]:
                  try:
                      if ele in my_list[j]:
                          grp1.append(my_list[i])
                          grp1.append(my_list[j])
                  except:
                      pass
          else:
              continue
      
      for lst in my_list:
          if lst not in grp1:
              grp2.append(lst)
          else:
              continue
      
      print(grp1)
      print(grp2)
      

      【讨论】:

      • 您正在硬编码组的数量。
      • @AnnZen OP 从未回答要求提供更具体说明的评论。
      【解决方案3】:

      这应该解决办法:

      my_list = [[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 12], [22, 17, 16, 15], [9, 88, 39, 58]]
      
      group1 = []
      group2 = []
      
      def contains(list1, list2):
          for item in list1:
              if item in list2:
                  return True
          return False
      
      counter = 0
      while counter < len(my_list):
          actualinnerlist = my_list[counter]
          actualmy_list = my_list[:counter] + my_list[counter+1:]
          print(f"\nactualinnerlist item: {actualinnerlist}")
          print(f"actualmy_list item: {actualmy_list}")
      
          for innerlist in actualmy_list:
              print(f"Actual Inner List: {actualmy_list}")
              print(f"Inner List: {innerlist}")
              if contains(actualinnerlist, innerlist):
                  group1.append(actualinnerlist)
                  counter += 1
                  break
          else:
              group2.append(actualinnerlist)
              counter += 1
      
      print (f"Group1: {group1}")
      print (f"Group2: {group2}")
      

      它只是比较列表,但在 while 循环中对列表进行切片以不与同一元素进行比较。

      【讨论】:

      • 您正在硬编码组的数量。
      猜你喜欢
      • 2013-01-28
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多