how to make a generator use the last yielded value when it regains control

Michael Spencer mahs at telcopartners.com
Sat Apr 8 18:49:11 EDT 2006


John Salerno wrote:
> Ben Cartwright wrote:
> 
>> Definitely go for (1).  The Morris sequence is a great candidate to
>> implement as a generator.  As a generator, it will be more flexible and
>> efficient than (2).
> 
> Actually I was just thinking about this and it seems like, at least for 
> my purpose (to simply return a list of numbers), I don't need a 
> generator. My understanding of a generator is that you do something to 
> each yielded value before returning to the generator (so that you might 
> not return at all), but since I'm not handling the individual numbers, 
> just getting a list, it seems I don't need them to be yielded. Of 
> course, a generator would allow the process to be done over and over, I 
> suppose, which is what I wanted, I just couldn't figure out how to keep 
> using the new values.

itertools.groupby makes this very straightforward:

 >>> from itertools import groupby
...
 >>> def lookandsay(seed):
...     seed = str(seed)
...     while 1:
...         seed = "".join("%s%s" % (len(list(group)), item)
...             for item, group in groupby(seed))
...         yield seed
...
 >>>
 >>> seq = lookandsay(1)
 >>> seq.next()
'11'
 >>> seq.next()
'21'
 >>> seq.next()
'1211'
 >>> seq.next()
'111221'
 >>> seq.next()
'312211'

If you want to get just part of the infinite series, use itertools.islice:

 >>> from itertools import islice
 >>> list(islice(lookandsay(1),10))
['11', '21', '1211', '111221', '312211', '13112221', '1113213211', 
'31131211131221', '13211311123113112211', '11131221133112132113212221']
 >>> list(islice(lookandsay(1),10,20))
['3113112221232112111312211312113211', 
'1321132132111213122112311311222113111221131221', 
'11131221131211131231121113112221121321132132211331222113112211', 
'311311222113111231131112132112311321322112111312211312111322212311322113212221', 
'132113213221133112132113311211131221121321131211132221123113112221131112311332111213211322211312113211', 
'11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221', 
'31131122211311123113321112131221123113112211121312211213211321322112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211', 
'1321132132211331121321231231121113112221121321132122311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112111331121113122112132113213211121332212311322113212221', 
'11131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211', 
'311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311122122111312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221131112311311121321122112132231121113122113322113111221131221']
 >>>


HTH
Michael




More information about the Python-list mailing list