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


Groups > comp.lang.python > #20058 > unrolled thread

Re: Cycle around a sequence

Started byPeter Otten <__peter__@web.de>
First post2012-02-09 09:36 +0100
Last post2012-02-09 09:36 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#20058 — Re: Cycle around a sequence

FromPeter Otten <__peter__@web.de>
Date2012-02-09 09:36 +0100
SubjectRe: Cycle around a sequence
Message-ID<mailman.5574.1328776805.27778.python-list@python.org>
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
>  >>>

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web