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


Groups > comp.lang.python > #40726 > unrolled thread

iterating over a list as if it were a circular list

Started bySven <svenito@gmail.com>
First post2013-03-07 09:23 +0000
Last post2013-03-07 08:37 -0500
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  iterating over a list as if it were a circular list Sven <svenito@gmail.com> - 2013-03-07 09:23 +0000
    Re: iterating over a list as if it were a circular list Roy Smith <roy@panix.com> - 2013-03-07 08:37 -0500

#40726 — iterating over a list as if it were a circular list

FromSven <svenito@gmail.com>
Date2013-03-07 09:23 +0000
Subjectiterating over a list as if it were a circular list
Message-ID<mailman.2993.1362648214.2939.python-list@python.org>

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

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.
So far I've got

for point in points:


-- 
./Sven

[toc] | [next] | [standalone]


#40763

FromRoy Smith <roy@panix.com>
Date2013-03-07 08:37 -0500
Message-ID<roy-21CD9E.08370707032013@70-1-84-166.pools.spcsdns.net>
In reply to#40726
In article <mailman.2993.1362648214.2939.python-list@python.org>,
 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.
> So far I've got
> 
> for point in points:

I'm not completely following what you're trying to do, but I think what 
you're looking for is some combination of zip() and itertools.cycle().

pairs = zip(P, cycle(N))

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web