iterating over a list as if it were a circular list

Discussion in 'Python' started by Sven, Mar 7, 2013.

  1. Sven

    Sven Guest

    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
     
    Sven, Mar 7, 2013
    #1
    1. Advertising

  2. Sven

    Roy Smith Guest

    In article <>,
    Sven <> 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))
     
    Roy Smith, Mar 7, 2013
    #2
    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:
    3
    Views:
    164
    Steven D'Aprano
    Mar 8, 2013
  2. Chris Angelico
    Replies:
    0
    Views:
    126
    Chris Angelico
    Mar 7, 2013
  3. Chris Rebert
    Replies:
    0
    Views:
    126
    Chris Rebert
    Mar 7, 2013
  4. Sven
    Replies:
    0
    Views:
    115
  5. Chris Rebert
    Replies:
    0
    Views:
    134
    Chris Rebert
    Mar 7, 2013
Loading...

Share This Page