[Tutor] circular looping
Alan Gauld
alan.gauld at btinternet.com
Mon May 24 19:57:13 CEST 2010
"Bala subramanian" <bala.biophysics at gmail.com> wrote
> But here the value of y changes sequentially. I want y to cycle
> through the
> list something like the following. Is there any function to do such
> circular
> iteration.
> cycle 1 y's value should be 'N', 'L', 'C', 'M, 'R' index 1,2,3,4,0
> of myres
> cycle 2 y's value should be 'L', 'C', 'M, 'R', 'N' index 2,3,4,0,1
> ,,
> cycle 3 y's value should be 'C', 'M', 'R', 'N','L' index 3,4,0,1,2
> ,,
The usual trick to get cyclic indices is to use the mod operator
with the length of the list (and maybe adding a constant)
So
L = "RNLCM"
y = 0 -> (y+1) % len(L) -> 1
y = 1 -> 2
...
y = 4 -> 0
y = 5 -> 1
etc....
Does that help?
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list