Question

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!

Was it helpful?

Solution

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()])

OTHER TIPS

First use split to access individual numbers separated by space. Then split each number by comma. And then access the two integer delimited by comma with zip :

>>> x = """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"""
>>> y, z = zip(*[i.split(',') for i in x.split()])  

You can use the split method, like this:

edges = "0,32 0,33 0,34 1,3 1,34 0,..."

edge_from = []
edge_to = []

pairs = edges.split(" ")
for pair in pairs:
    nums = pair.split(",")
    edge_from.append(nums[0])
    edge_to.append(nums[1])
import re

st = '0,32 0,33 0,34 1,3 1,34 0,34 2,4 2,28 2,29'

fa = re.split('\D+',st)
# \D means 'every character different from a digit
edge_to   = fa[0::2]
edge_from = fa[1::2]

print 'st:\n%s\n\n'    \
      'fa:\n%s\n\n'    \
      'edge_to  : %s\n'\
      'edge_from: %s'  \
      % (st,fa,edge_to,edge_from)

result

st:
0,32 0,33 0,34 1,3 1,34 0,34 2,4 2,28 2,29

fa:
['0', '32', '0', '33', '0', '34', '1', '3', '1', '34', '0', '34', '2', '4', '2', '28', '2', '29']

edge_to  : ['0', '0', '0', '1', '1', '0', '2', '2', '2']
edge_from: ['32', '33', '34', '3', '34', '34', '4', '28', '29']

How about this:

from_list = list()
to_list = list()
for pair in edges.split(" "):
    from_list.append(pair.split(",")[0])
    to_list.append(pair.split(",")[1])
print "From: " + " ".join(from_list)
print "To: " + " ".join(to_list)

My method is better i think :

d = "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" 

tab = ''.join(x if x.isalnum() else ' ' for x in d).split() 
From = tab[::2]
To = tab[::-2]
To.reverse() 
print From 
print To

Is this helpful?

import re
s = "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"
fro = re.findall('\d+\,',s)
to = re.findall('\,\d+\s',s)
f=""
t=""
for item in fro:
    item =item[:-1]+' '
    f = f+item
print 'From: '+f
for item in to:
    item =item[1:]
    t = t+item
print 'To: '+s[0]+' '+t+'31'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top