Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 582
Brian Avatar asked Oct 20 '25 09:10

Brian


2 Answers

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:

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]]]
like image 137
yatu Avatar answered Oct 23 '25 00:10

yatu


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.

like image 43
EdKenbers Avatar answered Oct 22 '25 23:10

EdKenbers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!