Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20057

Re: Cycle around a sequence

From Peter Otten <__peter__@web.de>
Subject Re: Cycle around a sequence
Date 2012-02-09 09:33 +0100
Organization None
References <mailman.5525.1328663401.27778.python-list@python.org> <4f3343b2$0$1615$c3e8da3$76491128@news.astraweb.com> <CAPTjJmo2BkybBo8Kd_Hk5tapq_YrSHHCk0zJmYTO408YShGWFA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5573.1328776409.27778.python-list@python.org> (permalink)

Show all headers | View raw


Chris Angelico wrote:

> On Thu, Feb 9, 2012 at 2:55 PM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>> If your data is humongous but only available lazily, buy more memory :)
> 
> Or if you have a huge iterable and only need a small index into it,
> snag those first few entries into a list, then yield everything else,
> then yield the saved ones:

> def cycle(seq,n):
>         seq=iter(seq)
>         lst=[next(seq) for i in range(n)]
>         try:
>                 while True: yield next(seq)
>         except StopIteration:
>                 for i in lst: yield i

I think that should be spelt

def cycle2(seq, n):
    seq = iter(seq)
    head = [next(seq) for i in range(n)]
    for item in seq:
        yield item
    for item in head:
        yield item

or, if you are into itertools,

def cycle3(seq, n):
    seq = iter(seq)
    return chain(seq, list(islice(seq, n)))

$ python -m timeit -s'from tmp import cycle; data = range(1000); start=10' 
'for item in cycle(data, 10): pass'
1000 loops, best of 3: 358 usec per loop
$ python -m timeit -s'from tmp import cycle2; data = range(1000); start=10' 
'for item in cycle2(data, 10): pass'
1000 loops, best of 3: 172 usec per loop
$ python -m timeit -s'from tmp import cycle3; data = range(1000); start=10' 
'for item in cycle3(data, 10): pass'
10000 loops, best of 3: 56.5 usec per loop

For reference:

$ python -m timeit -s'data = range(1000); start=10' 'for item in 
data[start:] + data[:start]: pass'
10000 loops, best of 3: 56.4 usec per loop

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Cycle around a sequence Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-08 01:10 +0000
  Re: Cycle around a sequence Christoph Hansen <ch@radamanthys.de> - 2012-02-08 03:01 +0100
  Re: Cycle around a sequence Neil Cerutti <neilc@norwich.edu> - 2012-02-08 14:25 +0000
    Re: Cycle around a sequence Terry Reedy <tjreedy@udel.edu> - 2012-02-08 15:15 -0500
    Re: Cycle around a sequence Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-08 22:47 +0000
    Re: Cycle around a sequence Serhiy Storchaka <storchaka@gmail.com> - 2012-02-09 12:10 +0200
  Re: Cycle around a sequence Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-09 03:55 +0000
    Re: Cycle around a sequence Chris Angelico <rosuav@gmail.com> - 2012-02-09 15:16 +1100
    Re: Cycle around a sequence Peter Otten <__peter__@web.de> - 2012-02-09 09:33 +0100
    Re: Cycle around a sequence Chris Angelico <rosuav@gmail.com> - 2012-02-09 21:34 +1100

csiph-web