集合列表看起来像这个
[{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}]
由于在列表中的集合之间找到了交集,如果找到交集,则需要将其作为一个集合。结果看起来像
[{77411, 77412, 77413, 77414, 77415, 77416, 77417,77418},
{82528, 82529, 82530, 82531, 82532, 82533, 82534},
{83209, 83210, 83212, 83213, 83214, 83215, 83216}]
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)
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