问题
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
.
回答1:
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]]]
回答2:
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)
回答3:
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.
来源:https://stackoverflow.com/questions/62433896/separate-nested-list-into-groups-with-disjoint-elements