python - 重复 1D NumPy 数组中的值 "N"次

标签 python arrays numpy

我有以下数组:

[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]])

关于python - 重复 1D NumPy 数组中的值 "N"次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50579691/

相关文章:

python - 从 numpy 一维数组列表创建 numpy 二维数组

java - 当尝试通过读取文件打印二维数组时,我的输出收到 Null

python - 类型错误 : 'NoneType' object does not support item assignment

python - 删除水平下划线

python - 无法在我的脚本中运行循环

arrays - 将字符串拆分为 n 个子字符串的可能方法

javascript - 使用变量作为 JSON 的键

Python:汇总和聚合 DataFrame 中的组和子组

python - ValueError : `class_weight` must contain all classes in the data. 类{1,2,3}存在于数据中但不存在于 `class_weight`

python - python中同时循环两个for循环的问题