List rotation

Remco Boerma remco at tomaatnet.nl
Thu Sep 30 11:04:16 CEST 2004


Hi,

 > ('b', 'a', 'c', 'b', 'a', 'd', 'c') .
 > For trans = 3 the list would be ('d', 'c', 'a', 'd', 'c', 'b', 'a')
 >
 > items = ('a', 'b', 'c', 'd')
 > items + 1 = ( 'b', 'c', 'd', 'a')
 > items + 2 = ( 'c', 'd', 'a', 'b')
 > items + 3 = ( 'd', 'a', 'b', 'c')

Is this what you're looking for?
 >>> items = [1,2,3,4,5,6]
 >>> items.append(items.pop(0))
 >>> items
[2, 3, 4, 5, 6, 1]
 >>> items.append(items.pop(0))
 >>> items
[3, 4, 5, 6, 1, 2]
 >>>

or, with the trans variable used:

 >>> trans=3
 >>> items = [1,2,3,4,5,6]
 >>> for t in xrange(trans):
...     items.append(items.pop(0))
...
 >>> items
[4, 5, 6, 1, 2, 3]
 >>>


M. Clift wrote:
> 
> for idx in range(len(items)):
>     if list[idx:idx + 1] == ['a']:
>        list[idx:idx + 1] = ['d']
>     if list[idx:idx + 1] == ['b']:
>        list[idx:idx + 1] = ['a']
That doesn't seem like a good option to me. .



More information about the Python-list mailing list