Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > iterating over a list as if it were a circular list

Reply
Thread Tools

iterating over a list as if it were a circular list

 
 
Sven
Guest
Posts: n/a
 
      03-07-2013
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

 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      03-07-2013
In article <(E-Mail Removed)>,
Sven <(E-Mail Removed)> 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))
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
iterating over a list as if it were a circular list Sven Python 3 03-08-2013 05:59 PM
Re: iterating over a list as if it were a circular list Chris Rebert Python 0 03-07-2013 09:38 AM
Re: iterating over a list as if it were a circular list Sven Python 0 03-07-2013 09:34 AM
Re: iterating over a list as if it were a circular list Chris Rebert Python 0 03-07-2013 09:31 AM
Re: iterating over a list as if it were a circular list Chris Angelico Python 0 03-07-2013 09:28 AM



Advertisments