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


Groups > comp.lang.python > #20057

Re: Cycle around a sequence

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.015
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'reference:': 0.07; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'entries': 0.15; '172': 0.16; 'head:': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'tmp': 0.16; 'wrote:': 0.18; 'memory': 0.21; 'feb': 0.22; 'from:addr:web.de': 0.23; 'index': 0.24; 'import': 0.27; 'yield': 0.29; 'pm,': 0.29; 'chris': 0.30; 'thu,': 0.32; 'header:X -Complaints-To:1': 0.34; 'loop': 0.34; 'steven': 0.34; 'try:': 0.34; 'to:addr:python-list': 0.35; 'list,': 0.36; 'received:org': 0.36; 'else,': 0.37; 'but': 0.37; 'think': 0.38; 'should': 0.38; 'data': 0.38; 'except': 0.39; 'or,': 0.40; 'to:addr:python.org': 0.40; 'huge': 0.61; 'more': 0.61; 'your': 0.61; '1000': 0.62; 'buy': 0.68; 'cycle;': 0.84; 'itertools,': 0.84; '10000': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Cycle around a sequence
Date Thu, 09 Feb 2012 09:33:13 +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>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084adf6.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5573.1328776409.27778.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328776409 news.xs4all.nl 6850 [2001:888:2000:d::a6]:36584
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20057

Show key headers only | 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