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

Discussion in 'Python' started by Chris Rebert, Mar 7, 2013.

  1. Chris Rebert

    Chris Rebert Guest

    On Mar 7, 2013 1:29 AM, "Sven" <> 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.
     
    Chris Rebert, Mar 7, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sven
    Replies:
    1
    Views:
    166
    Roy Smith
    Mar 7, 2013
  2. Sven
    Replies:
    3
    Views:
    173
    Steven D'Aprano
    Mar 8, 2013
  3. Chris Angelico
    Replies:
    0
    Views:
    134
    Chris Angelico
    Mar 7, 2013
  4. Chris Rebert
    Replies:
    0
    Views:
    133
    Chris Rebert
    Mar 7, 2013
  5. Sven
    Replies:
    0
    Views:
    130
Loading...

Share This Page