Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '"""': 0.07; '__name__': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; '"__main__":': 0.16; 'better?': 0.16; 'doctest': 0.16; 'sequence,': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'header:In-Reply-To:1': 0.22; 'indices': 0.23; 'index': 0.24; "python's": 0.24; 'stuff': 0.26; 'import': 0.27; "i'm": 0.28; 'yield': 0.29; 'definition': 0.30; 'header:User- Agent:1': 0.33; 'instead': 0.33; 'header:X-Complaints-To:1': 0.34; 'from:addr:yahoo.co.uk': 0.34; 'running': 0.34; 'to:addr:python- list': 0.35; 'starting': 0.36; 'beginning': 0.36; 'received:org': 0.36; 'sequence': 0.37; 'but': 0.37; 'skip:" 10': 0.37; 'could': 0.38; 'some': 0.38; 'i.e.': 0.39; 'received:78': 0.40; 'to:addr:python.org': 0.40; 'back': 0.60; 'kinds': 0.64; 'designed': 0.69; 'winner': 0.76; 'cycling': 0.84; 'received:as13285.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Cycle around a sequence Date: Wed, 08 Feb 2012 22:47:04 +0000 References: <9pfeutFtjiU2@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-179-19.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:10.0) Gecko/20120129 Thunderbird/10.0 In-Reply-To: <9pfeutFtjiU2@mid.individual.net> X-Antivirus: avast! (VPS 120208-0, 08/02/2012), Outbound message X-Antivirus-Status: Clean 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328741236 news.xs4all.nl 6906 [2001:888:2000:d::a6]:36899 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20036 On 08/02/2012 14:25, Neil Cerutti wrote: > On 2012-02-08, 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? > > Python's indices were designed for these kinds of shenanigans. > > def rotated(seq, n): > """Iterate through all of seq, but starting from index n. > > >>> ", ".join(str(n) for n in rotated(range(5), 3)) > '3, 4, 0, 1, 2' > """ > > i = n - len(seq) > while i< n: > yield seq[i] > i += 1 > > if __name__ == "__main__": > import doctest > doctest.testmod() > > If you have merely an iterable instead of a sequence, then look > to some of the other clever stuff already posted. > The winner :) -- Cheers. Mark Lawrence.