I have a list with NumPy arrays as elements that looks like this:
[array([ 0.2, -2.3, 5.3]),
array([-1.6, -1.7, 0.3]),
array([ 2.4, -0.2, -3.0]),
array([-4.1, -2.3, -2.7])]
and I want to convert it into 3 lists, each with elements from the columns of the above list. So the desired outcome looks like
list1 = [0.2, -1.6, 2.4, -4.1]
list2 = [-2.3, -1.7, -0.2, -2.3]
list3 = [5.3, 0.3, -3.0, -2.7]
1
You can achieve this by using NumPy’s transpose
or T
method like this:
transposed = np.array(array_list).T
list1, list2, list3 = transposed.tolist()
Full example
1
You can use zip
a = [
np.array([0.2, -2.3, 5.3]),
np.array([-1.6, -1.7, 0.3]),
np.array([2.4, -0.2, -3.0]),
np.array([-4.1, -2.3, -2.7])
]
With list comp in one line using zip
:
list1,list2,list3 = [list(x) for x in zip(*a)]
2
you can try
import numpy as np
a = [
np.array([0.2, -2.3, 5.3]),
np.array([-1.6, -1.7, 0.3]),
np.array([2.4, -0.2, -3.0]),
np.array([-4.1, -2.3, -2.7])
]
list1 = [arr[0] for arr in a]
list2 = [arr[1] for arr in a]
list3 = [arr[2] for arr in a]
# your result
print("list1:", list1)
print("list2:", list2)
print("list3:", list3)
Assuming your list of arrays has been defined as follows:
import numpy as np
array_list = [np.array([ 0.2, -2.3, 5.3]),
np.array([-1.6, -1.7, 0.3]),
np.array([ 2.4, -0.2, -3.0]),
np.array([-4.1, -2.3, -2.7])]
Then you can use the following one-liner:
list1, list2, list3 = np.transpose(array_list).tolist()
NumPy’s transpose()
function will implicitly convert array_list
into a two-dimensional NumPy array (so no need to do this explicitly) before transposing it (i.e. turning rows into columns and columns into rows). Calling tolist()
on the resulting array will turn it into a list of lists, with three elements (corresponding to the original three columns) on the topmost level. Assigning the result with list1, list2, list3 = ...
will “unpack” this three-element list into the three target variables.
If you are fine with the results (list1
, list2
, list3
) being again NumPy arrays rather than lists, you can omit tolist()
, thus only writing:
list1, list2, list3 = np.transpose(array_list)
You could stack the list into a 2D numpy array and transpose it.
import numpy as np
array_list = [np.array([0.2, -2.3, 5.3]),
np.array([-1.6, -1.7, 0.3]),
np.array([2.4, -0.2, -3.0]),
np.array([-4.1, -2.3, -2.7])]
stacked_array = np.vstack(array_list).T
list1, list2, list3 = stacked_array[0].tolist(), stacked_array[1].tolist(), stacked_array[2].tolist()
print(list1) print(list2) print(list3)
It would be more appropriate to convert the entire list into a python 2D list-of-lists instead of instantiating three new variables, anyway you can do that with
original_list = [np.array([ 0.2, -2.3, 5.3]),
np.array([-1.6, -1.7, 0.3]),
np.array([ 2.4, -0.2, -3.0]),
np.array([-4.1, -2.3, -2.7])]
list1 = [float(x[0]) for x in original_list]
list2 = [float(x[1]) for x in original_list]
list3 = [float(x[2]) for x in original_list]
Or you can convert the original list of lists into a numpy array, transpose it and convert it back into a list.
result = np.array(original_list).transpose().tolist()
list1 = result[0]
list2 = result[1]
list3 = result[2]