On Tue, 2007-11-13 at 15:12 +0100, BJörn Lindqvist wrote:
> L = somelist
> 
> idx = 0
> while True:
>     item = L[idx]
>     # Do something with item
>     idx = (idx + 1) % len(L)
> 
> wouldn't it be cool if there was an itertool like this:
> 
> def circulate(L, begin = 0, step = 1):
>     idx = begin
>     while True:
>         yield L[idx]
>         idx = (idx  + step) % len(L)
> 
> for x in circulate(range(10)):
>     print 'at', x,'!'

For begin=0 and step=1, itertools.cycle does exactly that. For arbitrary
offsets or different steps, you'd have to combine cycle with islice.

-- 
Carsten Haese
http://informixdb.sourceforge.net


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to