问题
I want to append a list/1d array (b) at the end of each row of a 2d array (a)
input:
a = np.array([[1, 1], [2, 2], [3, 3]])
b = np.array([4, 4])
desired out:
array([[1, 1, 4, 4],
[2, 2, 4, 4],
[3, 3, 4, 4]])
my code:
temp = []
for i in range(len(a)):
c = np.hstack((a[i], b))
temp.append(c)
d = np.vstack(temp)
is there any better and fast solution for this.
回答1:
a = np.array([[1, 1], [2, 2], [3, 3]])
b = np.array([4, 4])
c = np.tile(b[np.newaxis,:], (a.shape[0],1))
d = np.concatenate((a,c), axis=1)
来源:https://stackoverflow.com/questions/25530223/append-a-list-at-the-end-of-each-row-of-2d-array