List rotation

Peter Abel PeterAbel at gmx.net
Thu Sep 30 15:05:42 CEST 2004


"M. Clift" <noone at here.com> wrote in message news:<cjg61p$uvn$1 at newsg3.svr.pol.co.uk>...
> 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']


I'm not quite sure if I understood you well, but let me
try to explain what I mean to have understood:

>>> myList=['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> myList
['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> # This is the myList in reverse order
>>> myList.reverse()
>>> myList
['G', 'F', 'E', 'D', 'C', 'B', 'A']
>>> # But I think that's not what you want, so we reverse it back
>>> myList.reverse()
>>> myList
['A', 'B', 'C', 'D', 'E', 'F', 'G']

>>> # Rotate a list with a given number of steps to rotate

>>> def rotateList(aList,trans=1,direction=1):
... 	""" aList    : the list to rotate (!! a list and not a tuple)
... 	    trans    : the number of steps to rotate
... 	    direction: =1 rotate it backward, 
... 	                means put items from the end to the front
... 	               =0 rotate it forward
... 	"""
... 	if type(aList) != list:
... 		print 'I need a list, you gave me a %s' % str(type(aList))
... 		return
... 	if direction:
... 		# rotate it backwards
... 		while trans>0:
... 			aList.insert(0,aList.pop())
... 			trans-=1
... 	else:
... 		while trans>0:
... 			aList.append(aList.pop(0))
... 			trans-=1
... 
>>> 
>>> myList
['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> rotateList(myList,3)
>>> myList
['E', 'F', 'G', 'A', 'B', 'C', 'D']
>>> rotateList(myList,3,direction=0)
>>> myList
['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> 

Some bad examples:
>>> rotateList(1,3)
I need a list, you gave me a <type 'int'>

>>> rotateList((1,2,3),3)
I need a list, you gave me a <type 'tuple'>
>>> 

BTW some remarks:
Doen't use the word *list* for a name, it's reserved.
Use List, myList, anyList or l or anything else.
For this task I wouldn't use tuples, cause they are imutable.
If you have a sequence with many items, you would need much memory space.
This is the reason why I didn't create a new list and give
it back with return but work on the given list.
The list.methodes *insert*, *pop*, *reverse*, *append*  and some
others are working on the given list and doen't create a new one.

Hope I could help you a bit.

Regards
Peter



More information about the Python-list mailing list