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


Groups > comp.lang.python > #40734

Re: iterating over a list as if it were a circular list

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <svenito@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.026
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'iterate': 0.09; 'cc:addr :python-list': 0.10; 'assume': 0.11; 'itertools': 0.16; 'wrote:': 0.17; 'items.': 0.17; 'email addr:gmail.com&gt;': 0.20; 'import': 0.21; 'thanks.': 0.21; "i'd": 0.22; 'cc:2**0': 0.23; '&gt;': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'wondering': 0.26; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'chris': 0.28; 'run': 0.28; 'restart': 0.29; 'sensible': 0.29; 'points': 0.29; 'skip:& 10': 0.29; 'received:74.125.82': 0.33; 'point.': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'whatever': 0.35; 'there': 0.35; 'received:74.125': 0.36; 'be.': 0.36; 'late': 0.37; 'subject:: ': 0.38; 'more': 0.63; 'due': 0.66; '2013': 0.84; 'subject:over': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=7v3OAu9H8/kT/9EiPNGrJVzkyhT117IhZk/41sZ1wEo=; b=NoDpZYpLVUtqh9aI1mUPyf4YywCPSQtNNwmNhiMqAiniNSAFoDjFqSsFLcJVv9LqYS byBmhYKMi1BGBB8HjGtc2Tv0eardirDNV8QsjzLCKtuHLd05g2hFrPXyU3DiDyAyGQEF aC3fvrkM06+q47m849Tir/S0KV6M9zz4DMco+ZPM+Ws0lGisbnnxRqU7usqWT4pTB64X 8x5Yq0bz/jviklYbV1y4we4DfqNg/1SZpWAca5YDoRVvEhxwzfpnM1zuWIOpovn9dNAB BA6qzcsNhcqnmLNSl8WV4bIUaRbBmrtN65EYYuCn0BBZZS3jUh5tRkycQFWBRIRvp5kd zWOA==
MIME-Version 1.0
X-Received by 10.180.101.41 with SMTP id fd9mr26638456wib.20.1362648886665; Thu, 07 Mar 2013 01:34:46 -0800 (PST)
In-Reply-To <CAMZYqRRyrEGOmu_9OcUTLH7nZjNna=Z_XX_b4vHzf3ATxgehzw@mail.gmail.com>
References <CAEH=cXWXwOf6QnNur4V3HNBZ2i13gVnOiJDOOyCZs1Putc_mug@mail.gmail.com> <CAMZYqRRyrEGOmu_9OcUTLH7nZjNna=Z_XX_b4vHzf3ATxgehzw@mail.gmail.com>
Date Thu, 7 Mar 2013 09:34:46 +0000
Subject Re: iterating over a list as if it were a circular list
From Sven <svenito@gmail.com>
To Chris Rebert <clp2@rebertia.com>
Content-Type multipart/alternative; boundary=f46d041826c2d7eb8c04d7526a61
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2999.1362648888.2939.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362648888 news.xs4all.nl 6986 [2001:888:2000:d::a6]:36466
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:40734

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 7 March 2013 09:31, Chris Rebert <clp2@rebertia.com> wrote:

> On Mar 7, 2013 1:24 AM, "Sven" <svenito@gmail.com> wrote:
> >
> > I was wondering what the best approach for the following might be.
> >
> > Say you have a list P of points and another list N of other items. You
> can always assume that
> >
> > len(N) <= len(P)
> >
> > Now I would like to iterate over P and place one N at each point.
> However if you run out of N I'd like to restart from N[0] and carry on
> until all the points have been populated.
>
> Untested due to the late hour:
>
> import itertools
>
> for p, n in itertools.izip(P, itertools.cycle(N)):
>     # do whatever
>

I knew there was a more sensible way to do it. Thanks.




-- 
./Sven

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: iterating over a list as if it were a circular list Sven <svenito@gmail.com> - 2013-03-07 09:34 +0000

csiph-web