Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '[0,': 0.09; 'from:addr:timgolden.me.uk': 0.09; 'from:name:tim golden': 0.09; 'lawrence': 0.09; 'message-id:@timgolden.me.uk': 0.09; 'portions': 0.09; '16,': 0.15; '2.7.2': 0.16; 'better?': 0.16; 'bieber': 0.16; 'front-end.': 0.16; 'len': 0.16; 'pythonwin': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'cc:addr:python-list': 0.16; 'wed,': 0.17; 'wrote:': 0.18; '>>>': 0.18; 'cc:no real name:2**0': 0.21; 'trying': 0.21; 'header :In-Reply-To:1': 0.22; 'feb': 0.22; 'important,': 0.23; 'slice': 0.23; 'cc:2**0': 0.26; 'function': 0.27; 'import': 0.27; 'lists': 0.28; 'bit': 0.28; "i'm": 0.28; 'print': 0.29; 'cc:addr:python.org': 0.29; 'definition': 0.30; 'one)': 0.30; 'tail': 0.30; 'tjg': 0.30; 'does': 0.32; 'header:User-Agent:1': 0.33; 'lee': 0.34; 'running': 0.34; '17,': 0.34; '...': 0.35; 'starting': 0.36; 'beginning': 0.36; 'sequence': 0.37; 'but': 0.37; 'uses': 0.38; 'received:192': 0.38; 'could': 0.38; 'some': 0.38; 'i.e.': 0.39; 'possible.': 0.39; 'called': 0.40; 'might': 0.40; 'back': 0.60; 'range': 0.61; 'more': 0.61; 'copyright': 0.62; 'thousands': 0.62; 'from:addr:mail': 0.64; 'further': 0.64; 'chain': 0.66; '11,': 0.68; '19,': 0.68; 'dennis': 0.73; 'cycling': 0.84; 'join:': 0.84; 'to:none': 0.93 Date: Wed, 08 Feb 2012 08:36:16 +0000 From: Tim Golden User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20120129 Thunderbird/10.0 MIME-Version: 1.0 CC: python-list@python.org Subject: Re: Cycle around a sequence References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328690181 news.xs4all.nl 6860 [2001:888:2000:d::a6]:35727 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20016 On 08/02/2012 08:26, Mark Lawrence wrote: > On 08/02/2012 01:26, Dennis Lee Bieber wrote: >> On Wed, 08 Feb 2012 01:10:28 +0000, 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? >>> >>> 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. > I'm not an itertools expert, but does this do what you want? (Untested - I might well be off by one) import itertools sequence = range (100) split = 70 rslice = itertools.islice (sequence, split, len (sequence)) lslice = itertools.islice (sequence, split) repeater = itertools.cycle (itertools.chain (rslice, lslice)) TJG