I have paired values in a csv file. Neither of the paired values are necessarily unique. I would like to split this large list into independent complete sets for further analysis.
To illustrate, my "megalist" is like:
megalist = [['a', 'b'], ['a', 'd'], ['b', 'd'],['b', 'f'], ['r', 's'], ['t', 'r']...]
Most importantly, the output would preserve the list of paired values (i.e., not consolidate the values). Ideally, the output would eventually result in different csv files for individual analysis later. For example, this megalist would be:
completeset1 = [['a', 'b'], ['a', 'd'], ['b', 'd'], ['b', 'f']]
completeset2 = [['r', 's'], ['t', 'r']]
...
In a graph theory context, I'm trying to take a giant graph of mutually exclusive subgraphs (where the paired values are connected vertices) and split them into independent graphs that are more manageable. Thanks for any input!
Edit 1: This put me in a place from which I can move forward. Thanks again!
import sys, csv
import networkx as nx
megalist = csv.reader(open('megalistfile.csv'), delimiter = '\t')
G = nx.Graph()
G.add_edges_from(megalist)
subgraphs = nx.connected_components(G)
output_file = open('subgraphs.txt','w')
for subgraph in subgraphs:
output_line = str(G.edges(subgraph)) + '\n'
output_file.write(output_line)
output_file.close()