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


Groups > comp.lang.python > #40735

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

References <CAEH=cXXf8WVDjUsq2YFvf338+V3hgXChqQaNzpKGGVHf9QrMLQ@mail.gmail.com>
Date 2013-03-07 01:38 -0800
Subject Re: iterating over a list as if it were a circular list
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.3000.1362649116.2939.python-list@python.org> (permalink)

Show all headers | View raw


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

On Mar 7, 2013 1:29 AM, "Sven" <svenito@gmail.com> wrote:
>
> Stupid keyboard shortcuts, sent it too early. Apologies
>
>
> 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.
<snip>
> Additionally, what if I wanted to pull a random element from N, but I
want to ensure all elements from N have been used before starting to pick
already chosen random elements again.
> So far I thought of duplicating the list and removing the randomly chosen
elements from the list, and when it's empty, re-copying it. But that seems
a little "wrong" if you know what I mean.

Just iterate over the list in order, and random.shuffle() the list each
time you reach the end of it.

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


Thread

Re: iterating over a list as if it were a circular list Chris Rebert <clp2@rebertia.com> - 2013-03-07 01:38 -0800

csiph-web