import numpy as np
arr1 = np.array([0, 1, 2, 3]).reshape(1, 4)
arr2 = np.array([0, 1, 2, 3, 4, 5]).reshape(1, 6)
arr3 = np.array([0, 1, 4, 3, 5]).reshape(1, 5)
list_off_arrs = [arr1, arr2, arr3]
# expected output:
# ndarray -> [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 13, 15]]
I want to take a list of numpy arrays that are all one dimensional and contain index values, and combine them as shown above. Is there a quick way to do something like this?
CodePudding user response:
Here's one approach:
import numpy as np
arr1 = np.array([0, 1, 2, 3]).reshape(1, 4)
arr2 = np.array([0, 1, 2, 3, 4, 5]).reshape(1, 6)
arr3 = np.array([0, 1, 4, 3, 5]).reshape(1, 5)
list_of_arrs = [arr1, arr2, arr3]
n = len(list_of_arrs)
for i in range(n):
for j in range(i):
list_of_arrs[i] = list_of_arrs[j].shape[1]
print(np.hstack(list_of_arrs))
Result:
[[ 0 1 2 3 4 5 6 7 8 9 10 11 14 13 15]]
CodePudding user response:
Use numpy.concatenate
>>> arr2 = arr1[0, -1] 1
>>> arr3 = arr2[0, -1] 1
>>> np.concatenate([arr1, arr2, arr3], axis=1)
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 13, 15]])