I'm trying to create a network, given the edge list. I have a list of the edges, but I need to have two arrays, "from" and "to", to create the edges.
This is the data I have:
0,32 0,33 0,34 1,3 1,34 0,34 2,4 2,28 2,29 1,3 3,5 3,8 3,28 2,4 4,6 4,7 4,8 3,5 5,7 5,8 5,9 4,6 6,8 6,9 5,9 4,7 5,7 7,9 5,9 8,10 3,8 8,28 4,8 5,8 6,8 8,10 5,9 8,28 6,9 7,9 6,8 8,10 8,10 8,28 10,12 7,9 6,8 8,10 11,13 8,28 10,12 7,9 6,8 8,10 10,12 8,28 12,14 7,9 6,8 8,10 11,13 8,28 13,15 13,17 13,19 8,10 12,14 8,28 14,16 14,17 13,19 8,10 13,15 8,28 15,17 14,17 13,19 8,10 14,16 8,28 15,17 14,17 13,19 8,10 13,17 8,28 14,17 15,17 13,19 8,10 18,21 8,28 14,17 15,17 13,19 8,10 13,19 8,28 19,21 15,17 13,19 8,10 20,22 8,28 20,25 15,17 13,19 8,10 18,21 8,28 19,21 15,17 13,19 8,10 20,22 8,28 22,24 22,25 13,19 8,10 23,25 8,28 22,24 22,25 13,19 8,10 22,24 8,28 24,26 22,25 13,19 8,10 20,25 8,28 22,25 23,25 25,27 8,10 24,26 8,28 26,28 26,30 26,31 8,10 25,27 8,28 27,29 27,30 27,31 28,30 2,28 28,31 3,28 8,28 26,28 28,30 2,29 28,31 27,29 29,31 26,28 28,30 26,30 28,31 27,30 28,30 26,28 31,33 26,31 28,31 27,31 28,31 29,31 31,33 0,32 28,31 27,31 28,31 29,31 31,33 0,33 28,31 31,33 28,31 29,31 31,33 0,34 28,31 1,34 28,31 29,31
I want the output to be:
From: 0 0 0 1 1 0 2 2 2 1 3 3 3 2 4 4....
To: 32 33 34 3 34 34 4 28 29 3 5 8 28 4 6 7....
If it's before the comma, it's in the from array. After the comma in the to array.
I've tried looping over and adding
edges = "0,32 0,33 0,34 1,3 1,34 0,34 2,4 2,28 2,29 1,3 3,5 3,8 3,28 2,4 4,6 4,7 4,8 3,5 5,7 5,8 5,9 4,6 6,8 6,9 5,9 4,7 5,7 7,9 5,9 8,10 3,8 8,28 4,8 5,8 6,8 8,10 5,9 8,28 6,9 7,9 6,8 8,10 8,10 8,28 10,12 7,9 6,8 8,10 11,13 8,28 10,12 7,9 6,8 8,10 10,12 8,28 12,14 7,9 6,8 8,10 11,13 8,28 13,15 13,17 13,19 8,10 12,14 8,28 14,16 14,17 13,19 8,10 13,15 8,28 15,17 14,17 13,19 8,10 14,16 8,28 15,17 14,17 13,19 8,10 13,17 8,28 14,17 15,17 13,19 8,10 18,21 8,28 14,17 15,17 13,19 8,10 13,19 8,28 19,21 15,17 13,19 8,10 20,22 8,28 20,25 15,17 13,19 8,10 18,21 8,28 19,21 15,17 13,19 8,10 20,22 8,28 22,24 22,25 13,19 8,10 23,25 8,28 22,24 22,25 13,19 8,10 22,24 8,28 24,26 22,25 13,19 8,10 20,25 8,28 22,25 23,25 25,27 8,10 24,26 8,28 26,28 26,30 26,31 8,10 25,27 8,28 27,29 27,30 27,31 28,30 2,28 28,31 3,28 8,28 26,28 28,30 2,29 28,31 27,29 29,31 26,28 28,30 26,30 28,31 27,30 28,30 26,28 31,33 26,31 28,31 27,31 28,31 29,31 31,33 0,32 28,31 27,31 28,31 29,31 31,33 0,33 28,31 31,33 28,31 29,31 31,33 0,34 28,31 1,34 28,31 29,31"
edge_from = []
edge_to = []
for c in edges:
c = int(c)
if edges[c] != "," or edges[c] != " ":
if edges[c + 1] == ",":
edge_from.append(edges[c])
else:
edge_to.append(edges[c])
I am fairly new to Python so forgive my simple mistakes. Thanks!
Ok I haven't tested this but hopefully it works. It assumes edge is a string. It uses a list comprehension, which is a compact way of building a list. This iterates over each component of edges and calls the built-in split method for Python Strings, splitting by a comma delimiter. Split should return a list of two values. Once completed, this gives a list where each compon ent is another list of two values ie [[33,3],...]. Calling zip() on the list with the asterix will convert the single list of list into two separate lists of all the first and last sub element values.
edge_from, edge_to = zip(*[edge.split(',') for edge in edges.split()])