Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20015 > unrolled thread
| Started by | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| First post | 2012-02-08 08:26 +0000 |
| Last post | 2012-02-08 08:26 +0000 |
| 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.
Re: Cycle around a sequence Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-08 08:26 +0000
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2012-02-08 08:26 +0000 |
| Subject | Re: Cycle around a sequence |
| Message-ID | <mailman.5538.1328689595.27778.python-list@python.org> |
On 08/02/2012 01:26, Dennis Lee Bieber wrote: > On Wed, 08 Feb 2012 01:10:28 +0000, Mark Lawrence > <breamoreboy@yahoo.co.uk> 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? >> >> 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 >>>>> > > How large a sequence and, more important, is it fully known at the > start... > >>>> a = range(20) >>>> a > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>>> a_shift = a[5:] + a[:5] >>>> a_shift > [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4] >>>> > > IOWs, just slice and join: tail first, then front-end. > The sequences are small and the start is always known but the function that uses this is called thousands of times so I was trying to avoid building lists if at all possible. -- Cheers. Mark Lawrence.
Back to top | Article view | comp.lang.python
csiph-web