Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a tuple of tuples (or list of lists) of paired values into independent complete sets?

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()
like image 234
user1644030 Avatar asked Oct 06 '22 17:10

user1644030


1 Answers

You can use networkx for this. Constructing the graph:

>>> import networkx as nx
>>> megalist = [['a', 'b'], ['a', 'd'], ['b', 'd'],['b', 'f'], ['r', 's'], ['t', 'r']]
>>> G = nx.Graph()
>>> G.add_edges_from(megalist)

Then to get the list of subgrahs:

>>> subgraphs = nx.connected_components(G)
>>> subgraphs
[['a', 'b', 'd', 'f'], ['s', 'r', 't']]
>>> [G.edges(subgraph) for subgraph in subgraphs]
[[('a', 'b'), ('a', 'd'), ('b', 'd'), ('b', 'f')], [('s', 'r'), ('r', 't')]]
like image 73
jterrace Avatar answered Oct 09 '22 10:10

jterrace