Re: [Tutor] Alternating patterns

2006-03-29 Thread Kent Johnson
kevin parks wrote:
>>From: Kent Johnson <[EMAIL PROTECTED]>
>>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!)

The best introduction to new features is usually in the What's New 
document accompanying the release where the feature was added. Of course 
it helps to know when the feature was added...here are some generator 
examples:
http://www.python.org/doc/2.2.3/whatsnew/node5.html

Generators are excellent for encapsulating the generation of a sequence 
when there is state that must be maintained between elements. For 
example here is a generator that takes a sequence argument, and yields 
this sequence of sequences:
   the original sequence
   the original sequence with the first element incremented by one
   the original sequence
   the original sequence with the second element incremented by one
   etc until each element has been incremented

In [2]: def change_each(seq):
...: seq = list(seq) # Copy and ensure it's a list
...: for i in range(len(seq)):
...: yield seq
...: seq[i] += 1
...: yield seq
...: seq[i] -= 1
...:
...:

In [3]: s = [1, 3]

In [5]: for n in change_each(s):
...: print n
...:
...:
[1, 3]
[2, 3]
[1, 3]
[1, 4]

If you wanted to repeat this sequence indefinitely you could just wrap 
it with itertools.cycle().

The module docs for itertools contain quite a few examples:
http://docs.python.org/lib/itertools-recipes.html

itertools.cycle() is pretty simple, it just loops endlessly over the 
sequence you give it.

The itertools docs shows equivalent Python functions for each of the 
itertools functions. Most of them are implemented using generator 
functions so by looking at them you can learn about itertools and 
generators at the same time.
http://docs.python.org/lib/itertools-functions.html

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternating patterns

2006-03-28 Thread Pujo Aji
Hi,you can also use simple way of iterating using modus:    L = [1,2]    for i in range(6):    print L[i%len(L)]121212Cheers,pujo
On 3/29/06, kevin parks <[EMAIL PROTECTED]> wrote:
>>>> -->> Message: 10> Date: Tue, 28 Mar 2006 22:43:38 -0500> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] Alternating patterns> Cc: tutor@python.org> Message-ID: <[EMAIL PROTECTED]>> 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.>> Kentokay.. i am painfully unaware of generators, iterators, sets, genexesand 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 yetcover these newer constructs in detailitertools looks very cool are there any toots on the above and onSets & itertools? It really seems like it would help to know these for
my work... That liddo bit right up there with itertools.cycle alreadyhas me a drooling... (so helpful that would be!)-kp--___Tutor maillist  -  
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternating patterns

2006-03-28 Thread kevin parks
>
>
>
> --
>
> Message: 10
> Date: Tue, 28 Mar 2006 22:43:38 -0500
> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] Alternating patterns
> Cc: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> 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--





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternating patterns

2006-03-28 Thread Kent Johnson
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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Alternating patterns

2006-03-28 Thread kevin parks
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  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor