Home > Software engineering >  Converting list of arrays into list of lists in Python
Converting list of arrays into list of lists in Python

Time:07-30

I have a list I which contains numpy arrays. I want to convert these arrays into lists as shown in the current and expected outputs.

import numpy as np
I=[np.array([[0, 1],
        [0, 2],
        [1, 3],
        [4, 3],
        [2, 4]]),
 np.array([[0, 1],
        [0, 2],
        [1, 3],
        [4, 3],
        [3, 4],
        [2, 5]])]

for i in range(0,len(I)):
    arI1=[]
    I1=I[i].tolist()
    arI1.append(I1)
    I1=list(arI1)
    print(I1)

The current output is

[[[0, 1], [0, 2], [1, 3], [4, 3], [3, 4], [2, 5]]]

The expected output is

[[[0, 1], [0, 2], [1, 3], [4, 3], [2, 4]],
[[0, 1], [0, 2], [1, 3], [4, 3], [3, 4], [2, 5]]]

CodePudding user response:

You can use numpy:

import numpy as np

I1 = np.array(I1, dtype=object).tolist()
  • Related