Search code examples
pythonarrayslistsplit

Split each string in list and store in multiple arrays


I have a list that looks like this: ['1 0 0','2 0.5 0.25','3 1 0.5','4 1.5 0.75']

I want to end up with these three arrays: [1,2,3,4] and [0,0.5,1,1.5] and [0,0.25,0.5,0.75]

i.e. I want the first value of each list item and store it in an array, and do the same with the second and third values.

I tried this

for i in coordinates[:]:
    number,x,y=i.split(' ')

also tried using number[] and number.append but none of these seem to work


Solution

  • Creating three new list, and going through a input:

    input = ['1 0 0','2 0.5 0.25','3 1 0.5','4 1.5 0.75']
    x_list = []
    y_list = []
    z_list = []
    
    for i in input:
        x, y, z = i.split(' ')
        x_list.append(x)
        y_list.append(y)
        z_list.append(z)