0

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!

Ilya
  • 99
  • 8

7 Answers7

3

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()])
  • Very nice! Thanks for the explanation too. I then looped over it and made all the characters into integers and it's exactly what I needed. Thanks! – Ilya Mar 09 '14 at 19:26
  • `from` is a keyword, use other names for variable – alvas Mar 09 '14 at 19:29
  • Ok, changed now. Wasn't thinking about the keyword. Glad it does what you need. Can you please accept the answer? Thanks! :-) Edit: Just noticed you accepted another one. No probs! – confused_at_times Mar 09 '14 at 19:30
  • This is a very fast solution, despite the fact that ``split()`` is used multiple times, see my comment to alvas' answer – eyquem Mar 09 '14 at 21:58
3

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()])  
alvas
  • 115,346
  • 109
  • 446
  • 738
  • This solution is faster than mine, even if I define a compiled regex before using it in my solution. This solution runs in 92 % of the time of the more faster solution I could write. I'm surprised that using multiple times ``split()`` is anyway so rapid. I upvote – eyquem Mar 09 '14 at 21:56
2

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])
Selcuk
  • 57,004
  • 12
  • 102
  • 110
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']
eyquem
  • 26,771
  • 7
  • 38
  • 46
0

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)
barendt
  • 298
  • 3
  • 8
  • I think I prefer avoiding the duplicate call to `.split()` as in Selcuk's answer, even though the differences in speed are probably negligible – Ian Clark Mar 09 '14 at 19:16
0

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
newuser
  • 67
  • 3
  • 8
0

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'
DOSHI
  • 442
  • 2
  • 23
  • Why ``+s[0]`` and ``+'31'`` ? – eyquem Mar 09 '14 at 21:19
  • If you wrote ``fro = re.findall('(\d+)(?=,)',s)`` and ``to = re.findall('(?<=,)\d+',s)`` ,you would simplify your life. See in the doc what are the lookahead and lookbehin assertions in regexes. - WHat do you fear from ``,`` in a regex pattern ? It doesn't need to be escaped, it isn't a special character in a regex pattern. – eyquem Mar 09 '14 at 21:32