Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string list (with spaces instead of commas) into a list of lists

Tags:

python

list

I have the following string:

'[[ 0.03694573  0.01084746  0.01306414  0.00499198  0.00307188]\r\n [ 0.03780498  0.02610241  0.00967986  0.02228694 -0.01153102]\r\n [ 0.03837708  0.02111896  0.01370878 -0.00136839  0.01906253]\r\n [ 0.03490672 -0.02791057  0.08824896 -0.01991765  0.01964361]\r\n [ 0.0811892   0.28361901  0.21539196 -0.00259571  0.28737829]\r\n [ 0.20022041  0.16184418  0.25939959  0.00546446  0.36415219]\r\n [ 0.22920615  0.27439767  0.36991198  0.00624375  0.39911759]]'

As you can see, it comprises of a list of lists. However, each element within the lists are separated by tabs and not commas, and therefore ast.literal_eval() does not work. Does anyone have any ideas how best to convert this into a list of lists?

BJR ​

like image 313
BillyJo_rambler Avatar asked Oct 23 '25 01:10

BillyJo_rambler


1 Answers

Here's one way using re.sub:

import re
literal_eval(','.join(re.sub(r'(?<=\d)(\s+)(?=-?\d)', ',', s).splitlines()))

[[0.03694573, 0.01084746, 0.01306414, 0.00499198, 0.00307188],
 [0.03780498, 0.02610241, 0.00967986, 0.02228694, -0.01153102],
 [0.03837708, 0.02111896, 0.01370878, -0.00136839, 0.01906253],
 [0.03490672, -0.02791057, 0.08824896, -0.01991765, 0.01964361],
 [0.0811892, 0.28361901, 0.21539196, -0.00259571, 0.28737829],
 [0.20022041, 0.16184418, 0.25939959, 0.00546446, 0.36415219],
 [0.22920615, 0.27439767, 0.36991198, 0.00624375, 0.39911759]]
like image 135
yatu Avatar answered Oct 24 '25 17:10

yatu