简体   繁体   English

将嵌套列表分成具有不相交元素的组

[英]Separate nested list into groups with disjoint elements

I have list of list that looks like this我有一个看起来像这样的列表

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

and I would like to find what's the best way to split the list into two groups so that the individual elements in each group are not overlapping.我想找到将列表分成两组的最佳方法,以便每组中的各个元素不重叠。 For instance, in the example above the two groups would be例如,在上面的示例中,两组将是

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

and this is because 9, 10, 11, 12 never appear in any of the items of group1 .这是因为 9, 10, 11, 12 从未出现在group1的任何项目中。

Similarly to Combine lists with common elements , a way to go about this could be to define a graph from the nested list, taking each sublist as a path , and looking for the connected components :与将列表与公共元素组合起来类似,关于此问题的 go 的一种方法可能是从嵌套列表中定义一个图,将每个子列表作为路径,并寻找连接的组件

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]]]

This should do the trick:这应该可以解决问题:

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)

This should solution work:这应该解决方案工作:

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}")

It just compares the lists but slicing the list in the while loop to not compare with the same element.它只是比较列表,但在 while 循环中对列表进行切片,以便不与相同的元素进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM