Har*_*har 0 python arrays numpy list
我有一个浮动列表:
[1.0, 2.0, 3.0, 4.0]
Run Code Online (Sandbox Code Playgroud)
如何将此列表重塑为多维数组,例如:
[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [3.0, 3.0, 3.0], [4.0, 4.0 ,4.0]]
Run Code Online (Sandbox Code Playgroud)
不使用循环?是否可以使用numpy或任何其他?
如果您从 NumPy 数组开始,则可以使用numpy.repeat()然后reshape()获取所需的数组大小/形状:
> import numpy as np
> a = np.array([1.0, 2.0, 3.0, 4.0])
> np.repeat(a, 3).reshape(-1, 3)
array([[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.],
[4., 4., 4.]])
Run Code Online (Sandbox Code Playgroud)