重复数组后连接错误

Concatenate error after repeating array

我有5个数组我想重复600次然后做成一个大数组

lenE=600
E=array([49.73199462, 49.73799861, 49.74400261, 49.74894938, 49.7500066 ....])
Lat3E=[E]*lenE

我对所有数组 A、B、C、D 和 E 执行此操作。E 比其他数组短。当我尝试合并时,错误是:

lat=np.concatenate((Lat3A,Lat3B,Lat3C,Lat3D,Lat3E))
ValueError: all the input array dimensions except for the concatenation axis must match exactly.

这是因为 Lat3E 是 600 个数组的组合。它看起来像这样:

[array([49.73199462, 49.73799861,....)],array([49.73199462, 49.73799861,....]),array([49.73199462, 49.73799861,...)],...]

如何将这个 600 长数组变成 1 长数组来消除错误? 提前致谢。

您可以像这样连接多个数组列表:

lat = np.concatenate(Lat3A + Lat3B + Lat3C + Lat3D + Lat3E)

np.concatenate takes a sequence as input, so a list or a tuple will do. If you have multiple lists, you need to convert them to a single list, for example with + (or you could also do [*Lat3A, *Lat3B, *Lat3C, *Lat3D, *Lat3E], or use itertools.chain, ...) 然后传递给函数。

如果所有原始数组的大小都相同用 NumPy 解决相同问题的另一种方法只能是这样:

lens = np.array([lenA, lenB, lenC, lenD, lenE])
lat = np.tile(lens[:, np.newaxis], (1, 600, 1)).reshape((-1,))

您可以使用 np.repeat(E, lenE) 函数代替 Lat3E=[E]*lenE