-
-
Save workergnome/ce0f20651d17541fbddeb75713fabdb5 to your computer and use it in GitHub Desktop.
convert an adjacency list to edge list format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
""" | |
usage: | |
python adjacency-list-to-edges.py [adjacency-list-file] [edge-list-file] | |
example: | |
this program will help convert this: | |
[adjacency-list-file] | |
flower,fruit | |
carafe,flower,glass,window | |
flower,dew | |
into this: | |
[edge-list-file] | |
flower,fruit | |
carafe,flower | |
carafe,glass | |
carafe,window | |
flower,glass | |
flower,window | |
glass,window | |
flower,dew | |
""" | |
adjacency_list_filename = sys.argv[1] | |
edge_list_filename = sys.argv[2] | |
edge_list = [] | |
with open(adjacency_list_filename, 'r') as f: | |
for line in f: | |
line = line.rstrip('\n').split(',') | |
while len(line) > 0: | |
source = line[0] | |
for target in line[1:]: | |
edge_list.append("%s,%s" % (source, target)) | |
line.pop(0) | |
with open(edge_list_filename, 'w') as f: | |
f.write('%s\n' % ('\n'.join(edge_list))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment