# [Tutor] Alternating patterns

kevin parks kp8 at mac.com
Wed Mar 29 07:33:10 CEST 2006

```>
>
>
> ------------------------------
>
> Message: 10
> Date: Tue, 28 Mar 2006 22:43:38 -0500
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] Alternating patterns
> Cc: tutor at python.org
> Message-ID: <442A026A.9010107 at tds.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> 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

okay.. i am painfully unaware of generators, iterators, sets, genexes
and a lot of the new stuff post 2.3 & 2.4 stuffs... my problem is that
i find the online docs kind of terse and few of the Python books yet
cover these newer constructs in detail....

itertools looks very cool.... are there any toots on the above and on
Sets & itertools? It really seems like it would help to know these for
my work... That liddo bit right up there with itertools.cycle already
has me a drooling... (so helpful that would be!)

-kp--

```

More information about the Tutor mailing list