If I have a list of strings:
[\' 3.2 4.5 7.8 9.2 4.3 4.7 5.2\',
\' 3.1 4.1 1.3 8.2 4.1 3.2 3.1\',
\' 3.1 4.2 5.7 3.2 4.1 3.0 1.9\']
How c
Use
l = [' 3.2 4.5 7.8 9.2 4.3 4.7 5.2',
' 3.1 4.1 1.3 8.2 4.1 3.2 3.1',
' 3.1 4.2 5.7 3.2 4.1 3.0 1.9']
print( [map(float, i.strip().split()) for i in l] )
Output:
[[3.2, 4.5, 7.8, 9.2, 4.3, 4.7, 5.2], [3.1, 4.1, 1.3, 8.2, 4.1, 3.2, 3.1], [3.1, 4.2, 5.7, 3.2, 4.1, 3.0, 1.9]]
str.split()
to split element by spacemap(float, 'YOURLIST')
to apply float on all elements. You can use below code:
numbers = [' 3.2 4.5 7.8 9.2 4.3 4.7 5.2',
' 3.1 4.1 1.3 8.2 4.1 3.2 3.1',
' 3.1 4.2 5.7 3.2 4.1 3.0 1.9']
print([[float(number) for number in number_list.split()] for number_list in numbers])