我有一个for-loop
反复调用roll
,我想反转创建数组的顺序。
我想我忽略了一些微不足道的方法,但到目前为止,我只找到了10000 3 5 种不这样做的方法。
In [1]: from numpy import roll
In [2]: c = range(5)
## The code I want to invert
In [3]: for i in range(len(c)):
...: c = roll(c, 1)
...: print c
[4 0 1 2 3]
[3 4 0 1 2]
[2 3 4 0 1]
[1 2 3 4 0]
[0 1 2 3 4]
## The result I want
[0 1 2 3 4]
[1 2 3 4 0]
[2 3 4 0 1]
[3 4 0 1 2]
[4 0 1 2 3]
## What I've tried:
In [4]: for i in range(len(c)):
...: c = roll(c, -1)
...: print c
[1 2 3 4 0]
[2 3 4 0 1] # <- false
[3 4 0 1 2]
[4 0 1 2 3]
[0 1 2 3 4]
In [5]: for i in reversed(range(len(c))):
...: c = roll(c, -i)
...: print c
[4 0 1 2 3] # <- false
[2 3 4 0 1]
[4 0 1 2 3]
[0 1 2 3 4]
[0 1 2 3 4]
In [6]: for i in reversed(range(len(c))):
c = roll(c, i)
print c
...:
[1 2 3 4 0]
[3 4 0 1 2] # <- false
[1 2 3 4 0]
[0 1 2 3 4]
[0 1 2 3 4]
In [7]: for i in range(len(c)):
...: c = roll(c, i)
...: print c
...:
[0 1 2 3 4]
[4 0 1 2 3] # <- false
[2 3 4 0 1]
[4 0 1 2 3]
[0 1 2 3 4]
In [8]: for i in range(len(c)):
...: c = roll(c, -i)
...: print c
...:
[0 1 2 3 4]
[1 2 3 4 0]
[3 4 0 1 2] # <- false
[1 2 3 4 0]
[0 1 2 3 4]