我有以下数组:

[9.975 9.976 9.977 9.978 9.979 9.98  9.981 9.982 9.983 9.984 9.985 9.986
9.987 9.988 9.989 9.99  9.991 9.992 9.993 9.994]


现在,我想将这些值复制到同一行的n列中。结果应如下所示:

[[9.975 9.975 9.975],
 [9.976 9.976 9.976],
 .....
 [9.994 9.994 9.994]]


你知道这怎么可能吗?

提前致谢。

最佳答案

由于您使用的是numpy,请使用np.repeat + np.reshape

>>> np.repeat(arr, 3).reshape(-1, 3)
array([[9.975, 9.975, 9.975],
       [9.976, 9.976, 9.976],
       [9.977, 9.977, 9.977],
       [9.978, 9.978, 9.978],
       [9.979, 9.979, 9.979],
       [9.98 , 9.98 , 9.98 ],
       [9.981, 9.981, 9.981],
       [9.982, 9.982, 9.982],
       [9.983, 9.983, 9.983],
       [9.984, 9.984, 9.984],
       [9.985, 9.985, 9.985],
       [9.986, 9.986, 9.986],
       [9.987, 9.987, 9.987],
       [9.988, 9.988, 9.988],
       [9.989, 9.989, 9.989],
       [9.99 , 9.99 , 9.99 ],
       [9.991, 9.991, 9.991],
       [9.992, 9.992, 9.992],
       [9.993, 9.993, 9.993],
       [9.994, 9.994, 9.994]])

08-24 16:53