Y0s*_*tsu 2 python numpy python-3.x
我有一个 NumPy 数组,我想将第一个元素移动到最后。我知道如何通过转换为列表并执行插入操作来做到这一点,但我想避免这种情况并以更好和优化的方式进行。
假设这是我的数组,我想将 0 移到最后:
x = [[ 0 388.13 19.346 412.38 39.594]
[ 0 388.15 8.5168 416 38.972]
[ 0 223.14 156.21 317.91 193.46]]
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过转换为列表以这种方式做到这一点:
x = [[ 0 388.13 19.346 412.38 39.594]
[ 0 388.15 8.5168 416 38.972]
[ 0 223.14 156.21 317.91 193.46]]
Run Code Online (Sandbox Code Playgroud)
有没有办法在不将其转换为列表(使用 numpy)的情况下做到这一点?而且,我正在做的方式效率不高,即使将代码转换为列表,也有什么可能的方法可以使我的代码在速度方面高效?
预期输出:
x = [[388.13 19.346 412.38 39.594 0]
[388.15 8.5168 416 38.972 0]
[223.14 156.21 317.91 193.46 0]]
Run Code Online (Sandbox Code Playgroud)
你也可以尝试np.hstack:
np.hstack([x[:, 1:], x[:, :1]])
Run Code Online (Sandbox Code Playgroud)
甚至np.concatenate:
np.concatenate([x[:, 1:], x[:, :1]], axis=1)
Run Code Online (Sandbox Code Playgroud)
甚至np.c_
np.c_[x[:, 1:], x[:, :1]]
Run Code Online (Sandbox Code Playgroud)
以上所有内容为您提供:
array([[388.13 , 19.346 , 412.38 , 39.594 , 0. ],
[388.15 , 8.5168, 416. , 38.972 , 0. ],
[223.14 , 156.21 , 317.91 , 193.46 , 0. ]])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
460 次 |
| 最近记录: |