[Tutor] circular looping

Matthew Wood woodm1979 at gmail.com
Mon May 24 20:27:43 CEST 2010


This is a GREAT application for generators!

def cycle(seq, index):
    l = len(seq)
    for i in range(len(seq)):
        cursor = (i + index) % l
        yield seq[cursor]

Then you just pass it the starting index you want, and all is well in the
world.  :-)

Also, gratuitous use of the enumerate function in for-loops is grand:

for x_index, x in enumerate(myres):
    if not x in DON:
        continue
    for y_index, y in enumerate(cycle(myres)):
        print x, y
        if y in ALL:
            a[x_index][y_index] = 1

    print '-'


--

I enjoy haiku
but sometimes they don't make sense;
refrigerator?


On Mon, May 24, 2010 at 10:35 AM, Bala subramanian <
bala.biophysics at gmail.com> wrote:

> Friends,
> I have a list. I have to do some comparison of each item in the list with
> each other items in the list.
>
> from numpy import zeros
> a=zeros((5,5))
>
> myres=['R', 'N', 'L', 'C', 'M']
> DON=['R','N','L'] ; ALL=['R','L','M','S']
>
> for x in myres:
>         for y in myres:
>                 if x in DON:
>                           if y in ALL: a[res.index(x),res.index(y)] = 1
>                  else: continue
>
> 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
> ,,
>
> Thank you,
> Bala
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100524/4f0da66b/attachment-0001.html>


More information about the Tutor mailing list