-4

I want to display a list with order modulo N, for exemple:

With N =6, I have a list l[k]=[1, 2, 3, 4, 5, 6], so I can display its revers l[-k]=[6,5,4,3,2,1] by the instruction l[::-1].

But now I want to display l[(-k)mod N] which is [1,6,5,4,3,2] and then l[(1-k)mod N] which is [2,1,6,5,4,3] and so on.

Is there any instruction in python for display a list like that?

miinguyen
  • 3
  • 2
  • 1
    What have you tried? Are you having a specific problem with an attempt? – OMGtechy Apr 24 '17 at 13:43
  • 1
    Shouldn't `6 mod 6` be `0` not `1` for the first index of the first desired list? – MooingRawr Apr 24 '17 at 13:45
  • Refer to [this question](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation), and use two concatenated slices ; I don't think you can wrap around the array's bounds in a more easy fashion. – Aaron Apr 24 '17 at 13:46
  • @MooingRawr the index k start from 0 to N-1 so the new index of `l[(-k) mod N]` are `l [0 5 4 3 2 1]` – miinguyen Apr 24 '17 at 14:01
  • @Aaron thanks for the link, it's very useful – miinguyen Apr 24 '17 at 14:02

1 Answers1

0
>>> l = [1,2,3,4,5,6]
>>> N = len(l)
>>> revL = l[::-1]
>>> revL
[6, 5, 4, 3, 2, 1]
>>> for i in range(1,N):
...     print revL[-i:] + revL[:(N-i)]
...     
[1, 6, 5, 4, 3, 2]
[2, 1, 6, 5, 4, 3]
[3, 2, 1, 6, 5, 4]
[4, 3, 2, 1, 6, 5]
[5, 4, 3, 2, 1, 6]
Nilanjan
  • 176
  • 8
  • Is there any advantage to performing the list reversal as a first step? I would have used a negative step in the two slices. – Aaron Apr 24 '17 at 13:50