List rotation

M. Clift noone at here.com
Thu Sep 30 07:10:35 CEST 2004


Hi Steven,

Sorry, as I'm a beginner I don't think I should have used the title rotate
list, as that's not what I want.

If the list  ('a', 'd', 'b', 'a', 'd', 'c', 'b') was rotated once it would
of course give('d', 'b', 'a' etc...

What I'm looking for is that say if trans = 1, then the list would become
('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')

trans = 1

list = ('a', 'd', 'b', 'a', 'd', 'c', 'b')


I can think of the long/wrong way to do it as shown for trans = 3, but there
must be some simpler idea.


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']





More information about the Python-list mailing list