List Info

Thread: Alternating patterns




Alternating patterns
user name
2006-03-29 02:52:54
I have a set that i iterate over... but each time through it
i would 
like to alternate between the original set and a variation
of the set 
that has one of the members of the set altered (by + or - 1)

So if my original set is:

[0, 2, 4, 5, 7, 9, 11]

I would use that the first pass but on the second pass i
might like  
the third member (4,) to become 3, (-1) resulting in : [0,
2, 3, 5, 7, 
9, 11]

But then back again to the original  on the next pass (+1
back to 4,): 
[0, 2, 4, 5, 7, 9, 11]

and then back: [0, 2, 3, 5, 7, 9, 11] again, etc.

in other words i would like to alternate members of the set
back and 
forth. Usually only 1 (or sometimes 2,) member at time. i
could also 
imagine a needing(alter one, alter another, undo that, undo
the first 
back to the original set):

[0, 2, 4, 5, 7, 9, 11] --> [0, 2, 3, 5, 7, 9, 11] -->
[0, 2, 3, 5, 7, 
8, 10] --> [0, 2, 3, 5, 7, 9, 11] --> [0, 2, 4, 5, 7,
9, 11]

or:

original --> [0, 2, 4, 5, 7, 9, 11]
altered --> [0, 2, 3, 5, 7, 9, 11]
now back to 4, but change something else (like 11, is now
10):
[0, 2, 4, 5, 7, 9, 10]

etc...

How can one make such alternating patterns?

-kp--

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
Alternating patterns
user name
2006-03-29 03:43:38
kevin parks wrote:
> I have a set that i iterate over... but each time
through it i would 
> like to alternate between the original set and a
variation of the set 
> that has one of the members of the set altered (by + or
- 1)
> 
> So if my original set is:
> 
> [0, 2, 4, 5, 7, 9, 11]
> 
> I would use that the first pass but on the second pass
i might like  
> the third member (4,) to become 3, (-1) resulting in :
[0, 2, 3, 5, 7, 
> 9, 11]
> 
> But then back again to the original  on the next pass
(+1 back to 4,): 
> [0, 2, 4, 5, 7, 9, 11]
> 
> and then back: [0, 2, 3, 5, 7, 9, 11] again, etc.

> How can one make such alternating patterns?

itertools.cycle() will repeat a sequence indefinitely:
In [2]: from itertools import cycle

In [3]: i=cycle([1,2])

In [5]: for j in range(6):
    ...:     print i.next()
    ...:
    ...:
1
2
1
2
1
2

For non-repeating sequences I would look at writing a
generator function 
for the sequences.

Kent

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )