Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: List loops

Reply
Thread Tools

RE: List loops

 
 
Hamilton, William
Guest
Posts: n/a
 
      10-09-2007
> 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[i],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
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      10-09-2007
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.

 
Reply With Quote
 
 
 
 
Bill Hamilton
Guest
Posts: n/a
 
      10-09-2007
On 10/9/07, John Machin <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shouldn't list comprehension be faster than for loops? Carlos Grohmann Python 11 12-19-2009 02:18 PM
List loops Tommy Grav Python 1 10-09-2007 09:53 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
loops -> list/generator comprehensions jamesthiele.usenet@gmail.com Python 10 02-08-2005 03:53 AM
Difference Between List x; and List x(); , if 'List' is a Class? roopa C++ 6 08-27-2004 06:18 PM



Advertisments