Skip to main content
 首页 » 编程设计

python之使用 numpy 连接 2 个列表

2025年05月04日15bhlsheji

a = np.array([[0, 1, 2, 3], [4, 5, 6, 7]], dtype=int) 
b = np.array([[8], [9]], dtype=int) 

想要的结果:

alist = [[0, 1, 2, 3, 8], [4, 5, 6, 7, 9]] # as np.array

我试过:

np.concatenate(alist,blist) 
 
np.concatenate((alist,blist)) 
 
np.concatenate(alist, blist[0]) 
 
for a,b in zip(alist,blist): np.concatenate(a,b) 
 
alist = [*map(np.concatenate, alist, blist)])         

这给我带来了各种错误消息,我试图通过使用下一个试用版来修复。到目前为止没有任何效果。

请您参考如下方法:

您只是缺少 axis=1 关键字参数。

np.concatenate((a, b), axis=1) 

通常 np.concatenate在轴 0 上工作(沿着数组向下)。但在这种情况下,您希望沿轴 1 连接(穿过数组)。参见 the glossary获取更多信息。