Skip to content
Advertisement

Convert string array to array of array in python

I have input like this

logs = ['88 99 200','88 99 300','99 32 100','12 12 15']

How to convert it into an array of array in Python like this?

logs = [[88 99 200],[88 99 300],[99 32 100],[12 12 15]]

Advertisement

Answer

Use str.split with map :

list(map(str.split, logs))


Out[36]:
[['88', '99', '200'],
 ['88', '99', '300'],
 ['99', '32', '100'],
 ['12', '12', '15']]

If you want to convert each item to int:

list(map(lambda x: [int(i) for i in x], map(str.split, logs)))


Out[37]: [[88, 99, 200], [88, 99, 300], [99, 32, 100], [12, 12, 15]]
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement