Skip to content

Instantly share code, notes, and snippets.

@elliottcordo
Created February 13, 2023 12:37
Show Gist options
  • Save elliottcordo/e440eb792617ba0f36b7c342550097dc to your computer and use it in GitHub Desktop.
Save elliottcordo/e440eb792617ba0f36b7c342550097dc to your computer and use it in GitHub Desktop.
import networkx as nx
Graphtype = nx.Graph()
with open('result.csv', "r") as data:
# skip the header
next(data, None)
# parse into the graph
G = nx.parse_edgelist(data, delimiter=',', create_using=Graphtype)
# append the individual subgraph nodes to a list
results = []
for x in (G.subgraph(c) for c in nx.connected_components(G)):
results.append(x.nodes())
parent = ''
with open('output.csv', "w") as output:
for group in results:
parent = list(group)[0]
for match in group:
output.write(','.join([parent, match]) + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment