Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.034 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'def': 0.13; 'received:209.85.214.174': 0.13; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'head:': 0.16; '\xa0for': 0.16; 'wrote:': 0.18; 'thanks,': 0.19; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'message-id:@mail.gmail.com': 0.29; 'yield': 0.29; 'pm,': 0.29; 'chris': 0.30; 'thu,': 0.32; 'too': 0.33; 'try:': 0.34; 'to:addr:python-list': 0.35; 'received:209.85.214': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.38; 'think': 0.38; 'should': 0.38; 'except': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.40; 'otten': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=fud/2Rx86nldr9/IonJGkh/JqNS2ivuNaWBd8hSog1c=; b=tZl0yToYF6QYZMQarFxJyaV220iD0K+ipTCB+6L0ATqha/55omUv21l1zc4Cj2lRtH UqQ7c6QicLPiyauc8LKODog5apeJ+pRzpQwO6DqGbRb9ew4DRhdwO7TtRmuwxtKDx1xu D8L+QJ0Codr/ZFByRWEtomknuKq1zQNLLbc2Y= MIME-Version: 1.0 In-Reply-To: References: <4f3343b2$0$1615$c3e8da3$76491128@news.astraweb.com> Date: Thu, 9 Feb 2012 21:34:45 +1100 Subject: Re: Cycle around a sequence From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328783688 news.xs4all.nl 6901 [2001:888:2000:d::a6]:44249 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20064 On Thu, Feb 9, 2012 at 7:33 PM, Peter Otten <__peter__@web.de> wrote: > Chris Angelico wrote: > >> def cycle(seq,n): >> =A0 =A0 =A0 =A0 seq=3Diter(seq) >> =A0 =A0 =A0 =A0 lst=3D[next(seq) for i in range(n)] >> =A0 =A0 =A0 =A0 try: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 while True: yield next(seq) >> =A0 =A0 =A0 =A0 except StopIteration: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 for i in lst: yield i > > I think that should be spelt > > def cycle2(seq, n): > =A0 =A0seq =3D iter(seq) > =A0 =A0head =3D [next(seq) for i in range(n)] > =A0 =A0for item in seq: > =A0 =A0 =A0 =A0yield item > =A0 =A0for item in head: > =A0 =A0 =A0 =A0yield item Thanks, yeah, don't know what I was thinking :) Too much C work lately! ChrisA