How To Add To Arrays Such That Matching Elements Become Their Own Arrays
I feel like there is quick way to do this with Numpy but I can't seem to find the function for it. I need to take three arrays: a = [1,2,3] b = [1,2,3] c = [1,2,3] Z = np.somefunc
Solution 1:
You can use np.dstack
:
>>> np.dstack((a,b,c))
array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]]])
Also as says @ Warren Weckesser said in comment since np.dstack
returns a 3d array as a better way you can use following way :
>>> np.array((a, b, c)).T
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
Solution 2:
If the input arrays are all 1-d, you can use np.column_stack
:
In [13]: np.column_stack((a,b,c))
Out[13]:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
Solution 3:
Try zip(a, b, c)
. Simple as that, no Numpy needed.
Solution 4:
Just transpose the matrix using standard python functions, like zip
which you can wrap in an array:
numpy.array(zip(*(a, b, c))) # zip unpacks the matrix, tranposes it, and becomes an array
Post a Comment for "How To Add To Arrays Such That Matching Elements Become Their Own Arrays"