Skip to content Skip to sidebar Skip to footer

Python Concatenate Arrays In A List

I have a list of arrays of same size. The list 'z' holds: >>> z[0] Out[24]: array([ -27.56272878, 952.8099842 , -3378.58996244, 4303.9692863 ]) >>> z[1] Out[2

Solution 1:

Won't this do it?

znew = np.vstack((z[0],z[1]))

Solution 2:

Use .extend to build up the new list:

concatenated = []
for l in z:
    concatenated.extend(l)

Solution 3:

If you have :

z= [np.array([  -27.56272878,   952.8099842 , -3378.58996244,  4303.9692863 ]),
   np.array([  -28,   952 , -36244,  2863 ])]

You can try to concatenate them then reshape the new array using the number of arrays concatenated (len(z)) and the length of each array (len(z[0]) as you said they all have the same length) :

In [10]: new = np.concatenate([i for i in z]).reshape((len(z), len(z[0])))

In [11]: new.shape
Out[11]: (2, 4)

In [12]: print(new)
[[ -2.75627288e+01   9.52809984e+02  -3.37858996e+03   4.30396929e+03]
 [ -2.80000000e+01   9.52000000e+02  -3.62440000e+04   2.86300000e+03]]

Solution 4:

Just use extend() and that should be it. Basically extends the initial array with new values.

arr1= [  -27.56272878,   952.8099842 , -3378.58996244,  4303.9692863 ]
arr2= [  -28,   952 , -36244,  2863 ]
arr1.extend(arr2)printarr1>> [-27.56272878, 952.8099842, -3378.58996244, 4303.9692863, -28, 952, -36244, 2863]

Post a Comment for "Python Concatenate Arrays In A List"