在二维数组的每一行的末尾附加一个列表 [英] append a list at the end of each row of 2D array

查看:27
本文介绍了在二维数组的每一行的末尾附加一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在二维数组 (a) 的每一行的末尾附加一个列表/一维数组 (b)

I want to append a list/1d array (b) at the end of each row of a 2d array (a)

输入:

a = np.array([[1, 1], [2, 2], [3, 3]])
b = np.array([4, 4])

想要的:

array([[1, 1, 4, 4],
       [2, 2, 4, 4],
       [3, 3, 4, 4]])

我的代码:

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.

推荐答案

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)

这篇关于在二维数组的每一行的末尾附加一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆