PYTHON - How to create a list containing each element from a previous list within another list

Asked

Viewed 38 times

-3

I think the easiest way to describe the problem is to show the goal, otherwise it gets confused.

I need to turn something like this: [['3 -1 2 0 2 0 -1 -1', '4 2 4 5 1 0 0 1 1'], ['6 -8 -8 15 15 0', '5 -1 3 2']]

In something like this: [[['3 -1 2 0 2 0 -1 -1'], ['4 2 4 5 1 0 0 1 1']], [['6 -8 -8 15 15 0'], ['5 -1 3 2']]]

The idea seems simple, but I’m not able to separate it ever, without losing the separation that is in the first example. It is imperative that I have this separation in 3 levels (pq later I still have to separate the values within each list)

  • Supposing l be the list above, do [[[s[0]],[s[1]]] for s in l] test example: https://ideone.com/469ATT

1 answer

-1


good to write a code very quickly here according to what you wanted, I hope to have helping

arrays = [['3 -1 2 0 2 0 -1 -1', '4 2 4 5 1 0 0 1 1'], ['6 -8 -8 15 15 0', '5 -1 3 2']]

newArrays = list()
temp = list()

for array in arrays:
    for item in array:
        temp.append([item])

    newArrays.append(temp.copy())
    temp.clear()
    
print(newArrays)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.