gpt4 book ai didi

python - 如何旋转形状为 (n,) 或 (n,1) 的 numpy 数组中的数字?

转载 作者:行者123 更新时间:2023-11-28 19:44:47 24 4
gpt4 key购买 nike

假设我有一个 numpy 数组:

>>> a 
array([0,1,2,3,4])

我想“旋转”它以获得:

>>> b
array([4,0,1,2,3])

什么是最好的方法?

我一直在转换为双端队列并返回(见下文)但是有更好的方法吗?

b = deque(a)
b.rotate(1)
b = np.array(b)

最佳答案

只需使用 numpy.roll功能:

a = np.array([0,1,2,3,4])
b = np.roll(a,1)
print(b)
>>> [4 0 1 2 3]

另见 this question .

关于python - 如何旋转形状为 (n,) 或 (n,1) 的 numpy 数组中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15969708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com