Can we unroll a loop?

View: New views
2 Messages — Rating Filter:   Alert me  

Can we unroll a loop?

by Tino Dai-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Everybody,

     I am wondering about a better approach to doing this:

     for obj in groups:
       rVs = rVs + Event.objects.get(group=obj)
    rVs.sort()

     Instead what I'm looking for is to have a construct that would expand out to this:
    
    rVs =  Event.objects.get(group=groups[0]) | Event.objects.get(group=groups[1]) \
            | ... | Event.objects.get(group=groups[n-1]) | Event.objects.get(group=groups[n])

     I don't know if this is even possible. I have looked at the built in map function as well as
some of the itertools functions, but nothing seems to fit the bill. Is this even possible and
if so, how?

Thanks in advance!
-Tino


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Can we unroll a loop?

by Kent Johnson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Nov 2, 2009 at 3:52 PM, Tino Dai <oberoc@...> wrote:
> Hi Everybody,
>
>      I am wondering about a better approach to doing this:
>
>      for obj in groups:
>        rVs = rVs + Event.objects.get(group=obj)
>     rVs.sort()

Assuming rVs is a list and Event.objects.get(group=obj) is also a
list, you can use
rVs = sum((Event.objects.get(group=obj) for obj in groups), [])

Kent
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor