RE: List loops

Discussion in 'Python' started by Hamilton, William, Oct 9, 2007.

  1. > From: Tommy Grav
    >
    > Hi everyone,
    >
    > I have a list of objects where I have want to do two loops.
    > I want to loop over the list and inside this loop, work on all
    > the elements of the list after the one being handled in the outer
    > loop. I can of course do this with indexes:
    >
    > >>> alist = range(3)
    > >>> for i in xrange(len(alist)):

    > ... for j in xrange(i+1,len(alist)):
    > ... print i,j,alist,alist[j]
    > ...
    > 0 1 0 1
    > 0 2 0 2
    > 1 2 1 2
    > >>>

    >
    >
    > Is there a way to do this without using indexes?
    >


    You have to use indices because you are printing the indices. Given
    that, the following loop does what it looks like you are trying to do.

    >>> alist = range(3)
    >>> for index, i in enumerate(alist):

    for jndex, j in enumerate(alist[index:]):
    print index, jndex, i, j


    0 0 0 0
    0 1 0 1
    0 2 0 2
    1 0 1 1
    1 1 1 2
    2 0 2 2
    >>>



    --
    -Bill Hamilton
    Hamilton, William, Oct 9, 2007
    #1
    1. Advertising

  2. Hamilton, William

    John Machin Guest

    On 10/10/2007 1:33 AM, Hamilton, William wrote:
    >> From: Tommy Grav
    >>
    >> Hi everyone,
    >>
    >> I have a list of objects where I have want to do two loops.
    >> I want to loop over the list and inside this loop, work on all
    >> the elements of the list after the one being handled in the outer


    The man said "after" ...

    >
    >>>> alist = range(3)
    >>>> for index, i in enumerate(alist):

    > for jndex, j in enumerate(alist[index:]):


    .... so you need index+1 ...

    > print index, jndex, i, j
    >
    >
    > 0 0 0 0


    .... to avoid the above unwanted output.
    John Machin, Oct 9, 2007
    #2
    1. Advertising

  3. On 10/9/07, John Machin <> wrote:
    > On 10/10/2007 1:33 AM, Hamilton, William wrote:
    > >> From: Tommy Grav
    > >>
    > >> Hi everyone,
    > >>
    > >> I have a list of objects where I have want to do two loops.
    > >> I want to loop over the list and inside this loop, work on all
    > >> the elements of the list after the one being handled in the outer

    >
    > The man said "after" ...
    >
    > >
    > >>>> alist = range(3)
    > >>>> for index, i in enumerate(alist):

    > > for jndex, j in enumerate(alist[index:]):

    >
    > ... so you need index+1 ...
    >
    > > print index, jndex, i, j
    > >
    > >
    > > 0 0 0 0

    >
    > ... to avoid the above unwanted output.
    >


    Hey, if I got it right, he'd have no work to do himself. :)


    --
    -Bill Hamilton
    Bill Hamilton, Oct 10, 2007
    #3
    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. roopa
    Replies:
    6
    Views:
    723
    Jerry Coffin
    Aug 27, 2004
  2. Replies:
    10
    Views:
    496
    Caleb Hattingh
    Feb 8, 2005
  3. Tommy Grav

    List loops

    Tommy Grav, Oct 9, 2007, in forum: Python
    Replies:
    1
    Views:
    333
    John Machin
    Oct 9, 2007
  4. Carlos Grohmann
    Replies:
    11
    Views:
    880
    sturlamolden
    Dec 19, 2009
  5. Me
    Replies:
    2
    Views:
    229
Loading...

Share This Page