[Tutor] Re: rotating a list
Gregor Lingl
glingl at aon.at
Sat Sep 20 17:06:54 EDT 2003
Andrei schrieb:
> ... Could you explain why incrementing n is a superior solution?
Superior or not?!
Here a definitely different solution:
>>> def rotate(x):
x[:-1],x[-1] = x[1:],x[0]
>>> x = range(12)
>>> print x, id(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 11554712
>>> for i in range(12):
rotate(x)
print x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1]
[3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2]
[4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3]
[5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4]
[6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5]
[7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6]
[8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7]
[9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8]
[10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> print id(x)
11554712
>>>
Of course, which one you use depends on
what you need
Regards,
Gregor
More information about the Tutor
mailing list