Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40727
| Date | 2013-03-07 09:27 +0000 |
|---|---|
| Subject | iterating over a list as if it were a circular list |
| From | Sven <svenito@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2994.1362648463.2939.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
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.
So far I've got (pseudo code)
i = 0
for point in points:
put N[i] at point
if i > len(N):
i = 0
is this the most pythonic way to accomplish this?
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.
--
./Sven
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
iterating over a list as if it were a circular list Sven <svenito@gmail.com> - 2013-03-07 09:27 +0000
Re: iterating over a list as if it were a circular list Alexander Blinne <news@blinne.net> - 2013-03-08 00:49 +0100
Re: iterating over a list as if it were a circular list Alexander Blinne <news@blinne.net> - 2013-03-08 00:50 +0100
Re: iterating over a list as if it were a circular list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-08 17:59 +0000
csiph-web