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


Groups > comp.lang.python > #20058

Re: Cycle around a sequence

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!feeder3.eweka.nl!81.171.88.15.MISMATCH!eweka.nl!lightspeed.eweka.nl!194.109.133.84.MISMATCH!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.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'lawrence': 0.09; 'portions': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '2.7.2': 0.16; 'better?': 0.16; 'pythonwin': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'from:addr:web.de': 0.23; 'import': 0.27; 'bit': 0.28; "i'm": 0.28; 'print': 0.29; 'definition': 0.30; 'collections': 0.30; 'list': 0.32; 'instead': 0.33; 'header:X -Complaints-To:1': 0.34; 'running': 0.34; 'to:addr:python-list': 0.35; '...': 0.35; 'starting': 0.36; 'beginning': 0.36; 'received:org': 0.36; 'sequence': 0.37; 'could': 0.38; 'some': 0.38; 'i.e.': 0.39; 'to:addr:python.org': 0.40; 'back': 0.60; 'copyright': 0.62; 'further': 0.64; 'chain': 0.66; 'cycling': 0.84
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:36:30 +0100
Organization None
References <jgsi0t$org$1@dough.gmane.org>
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.5574.1328776805.27778.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328776805 news.xs4all.nl 6842 [2001:888:2000:d::a6]:47661
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20058

Show key headers only | View raw


Mark Lawrence wrote:

> I'm looking at a way of cycling around a sequence i.e. starting at some
> given location in the middle of a sequence and running to the end before
> coming back to the beginning and running to the start place.  About the
> best I could come up with is the following, any better ideas for some
> definition of better?

You could use a deque instead of a list and .rotate() that:

>>> from collections import deque
>>> d = deque(range(10))
>>> d.rotate(-4)
>>> d
deque([4, 5, 6, 7, 8, 9, 0, 1, 2, 3])

> 
> PythonWin 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
> (Intel)] on win32.
> Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin'
> for further copyright information.
>  >>> from itertools import chain
>  >>> a=range(10)
>  >>> g = chain((a[i] for i in xrange(4, 10, 1)), (a[i] for i in
>  >>> xrange(4))) for x in g: print x,
> ...
> 4 5 6 7 8 9 0 1 2 3
>  >>>

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


Thread

Re: Cycle around a sequence Peter Otten <__peter__@web.de> - 2012-02-09 09:36 +0100

csiph-web