Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个 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功能:
numpy.roll
a = np.array([0,1,2,3,4]) b = np.roll(a,1) print(b) >>> [4 0 1 2 3]
另请参阅此问题。
numpy.concatenate([a[-1:], a[:-1]]) >>> array([4, 0, 1, 2, 3])
试试这个
b = a[-1:]+a[:-1]