将列表作为一组重复的数字分配给numpy数组列



我有一个名为formattedData的numpy数组,它有大约1000行,当前设置为包含:

-999 -999 -999
-999 -999 -999
...
-999 -999 -999

我有另一个变量timeList,它是一个长度为5:的列表

1
2
3
4
5

我想用timeList中的数据覆盖formattedData中的第三列,这些数据一遍又一遍地重复,直到数组结束。我知道timeList将始终与formattedData匹配精确的次数,例如:

-999 -999 1
-999 -999 2
-999 -999 3
-999 -999 4
-999 -999 5
-999 -999 1
-999 -999 2
-999 -999 3
-999 -999 4
-999 -999 5
...
-999 -999 1
-999 -999 2
-999 -999 3
-999 -999 4
-999 -999 5

我试过各种各样的方法,但都没能奏效。有人能帮我指出正确的方向吗?

谢谢!

您可以使用np.tile来重复您的列表,然后将其放入数组的第三列。重复次数已预先计算:

num_repeats = len(formattedData) // len(timeList)
to_place = np.tile(timeList, reps=num_repeats)
formattedData[:, 2] = to_place

TRY:

formatteddata[:, 2] = np.tile(
timelist, formatteddata.shape[0] // timelist.shape[0])

相关内容

最新更新