I have array [1,2,1,2,3,4,3,4,1,2]
I want to loop it x times, each times moving array every element 1 position forward:
So next loop will be:
2.[2,1,2,3,4,3,4,1,2,1]
3.
[1,2,3,4,3,4,1,2,1,2]
etc….
How can I manipulate array like this way?
EDIT:
What I thought, but maybe some better tricks:
Just go over the array with while loop and create new array with for cycle.
for i in range(11)
array[i] = array[i-1]
etc etc..its pseudo code
Using a List data structure isn’t an efficient way to do this. A Queue would be more appropriate. In any case:
Using a Queue
As I suggested, using a Queue (collections.deque):
Keeping the List
Alternatively (faster than the previous one):
Here you can change xrange for whatever you want to iterate over.
Timeit analysis:
Pop-Append
Slicing
Queue
With a little optimization, basically removing the __getattr__ call for append, pop and rotate:
Pop-Append
Queue