0

the list of sets looks like this

 [{77411, 77412, 77413, 77414, 77415, 77416, 77417},
 {77411, 77412, 77414, 77415, 77416, 77417, 77418},
 {82528, 82529, 82530, 82531, 82532, 82533, 82534},
 {83209, 83210, 83212, 83213, 83214, 83215, 83216}]

as there is intersection found in between the sets inside list, Need to make it a single set if intersection found. Result looks like

 [{77411, 77412, 77413, 77414, 77415, 77416, 77417,77418},
 {82528, 82529, 82530, 82531, 82532, 82533, 82534},
 {83209, 83210, 83212, 83213, 83214, 83215, 83216}]
Bharath_Raja
  • 622
  • 8
  • 16
  • It looks like the most complicated part is finding a [fast way to test for set intersection](https://stackoverflow.com/q/3170055/11659881). From there, it's a couple of for loops. Give those a try and if you find yourself stuck please edit the question to show what you've tried. – Kraigolas Jun 05 '21 at 17:03

2 Answers2

0
    lst = [{77411, 77412, 77413, 77414, 77415, 77416, 77417},
 {77411, 77412, 77414, 77415, 77416, 77417, 77418},
 {82528, 82529, 82530, 82531, 82532, 82533, 82534},
 {83209, 83210, 83212, 83213, 83214, 83215, 83216}]

for i in range(len(lst)):
    for j in range(i+1, len(lst)):
        if lst[i].intersection(lst[j]):
            lst[i] = lst[i].union(lst[j])
            lst[j] = lst[i]
result = list()
for item in lst:
    if item not in result:
        result.append(item)
0
import networkx as nx

G = nx.Graph()

# Add nodes and edges to graph
for l in lst:
    nx.add_path(G, l)
clubbed = list(nx.connected_components(G))
clubbed
Anshuman
  • 1
  • 1