答案 0 :(得分:37)
为什么不只是roll
带有负数?
>>> import numpy as np
>>> a = np.arange(10)
>>> np.roll(a,2)
array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
>>> np.roll(a,-2)
array([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])
答案 1 :(得分:8)
你可以使用负移位
a = np.arange(10)
print(np.roll(a, 3))
print(np.roll(a, -3))
返回
[7,8,9,0,1,2,3,4,5,6]
[3,4,5,6,7,8,9,0,1,2]