I have two arrays A1
and A2
with shapes (1,3)
and (1,4)
respectively. I want to append these two arrays into a list. I present the expected output.
import numpy as np
A1=np.array([[128, 129, 131]])
A2=np.array([[128, 131, 132, 140]])
The expected output is
A=[array([[128, 129, 131]]), array([[128, 131, 132, 140]])]
CodePudding user response:
As numpy
allows fast numerical manipulations it expects all the arrays to be of the same shape, so in your case (i.e., A1.shape = 3
and A2.shape = 4
) you won't be able to convert the combined array into numpy
. The only thing you can do is to use the python
list
object, as pointed by @hapulj by:
C = [A1, A2]
but it certainty be slower than using numpy
due to the header appended by python
to represent the objects in the list (more on that here), so I'd recommend making sure your arrays are of the same length (e.g., by adding 0
if it make sense in your case), and then using something like:
import numpy as np
A1=np.array([[128, 129, 131, 0]])
A2=np.array([[128, 131, 132, 140]])
C = np.array([A1, A2], dtype=np.int16)
Output:
array([[[128, 129, 131, 0]],
[[128, 131, 132, 140]]], dtype=int16)
Cheers