Python: Split String into list of lists of lists -


i have string "2 2, 2 5, 3 1; 3 3, 4 4, 1 6; 1 1, 4 0;". ; dividing string 4 big chunks, next level , last whitespace. goal split string list of lists of lists got stuck. here code.

s = "2 2, 2 5, 3 1; 3 3, 4 4, 1 6; 1 1, 4 0;" t = [elem.strip() elem in s.split(";")] u = [] v = []  in range(0,len(t)):     u.append([elem.strip() elem in t[i].split(',')])  in range(0,len(u)):     j in range(0,len(u[i])):         v.append(u[i][j].split())  print("s = ", s) print("u = ", u) print("v = ", v) 

with output:

s =  2 2, 2 5, 3 1; 3 3, 4 4, 1 6; 1 1, 4 0; t =  ['2 2, 2 5, 3 1', '3 3, 4 4, 1 6', '1 1, 4 0', ''] u =  [['2 2', '2 5', '3 1'], ['3 3', '4 4', '1 6'], ['1 1', '4 0'], ['']] v =  [['2', '2'], ['2', '5'], ['3', '1'], ['3', '3'], ['4', '4'],       ['1', '6'], ['1', '1'], ['4', '0'], []] 

v not how want be. want ['2', '2'], ['2', '5'], ['3', '1'] contained in separate list (the others well).

what missing?

bonus question: string represents graph places separated ; represent node, places separated , represent neighbor represented neighbors node-number , distance (which separated whitespace)
there better way store graph in primitive datatype without building class?

use nested comprehension split further , further:

v = [[map(int, j.split()) j in i.split(',')] in s.split(';')] 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -