Re: [Tutor] List comp question

2007-11-03 Thread Ricardo Aráoz
Eric Brunson wrote: > Ricardo Aráoz wrote: >> Kent Johnson wrote: >> >>> I am building a list like this: >>> >>> tree = [] >>> for top in tops: >>> l2 = level2(top) >>> if l2: >>> tree.append((top, l2)) >>> >>> I would really like to turn this into a list

Re: [Tutor] List comp question

2007-11-03 Thread Eric Brunson
Ricardo Aráoz wrote: > Kent Johnson wrote: > >> I am building a list like this: >> >> tree = [] >> for top in tops: >> l2 = level2(top) >> if l2: >> tree.append((top, l2)) >> >> I would really like to turn this into a list comprehension: >> >> tree = [ (t

Re: [Tutor] List comp question

2007-11-03 Thread Michael Langford
I decided you probably should also have a cleanup function since garbage collection won't work now unless you explicitly clean the function. This approach works, and also works if you call the function again after you've called cleanup (it just runs the function 1 more time, then again, returns the

Re: [Tutor] List comp question

2007-11-02 Thread Alan Gauld
"Kent Johnson" <[EMAIL PROTECTED]> wrote > I would really like to turn this into a list comprehension: > > tree = [ (top, level2(top)) for top in tops if level2(top) ] > > but the call to level2() is expensive enough that I don't want to > repeat > it. Is there any way to do this or am I stuck w

Re: [Tutor] List comp question

2007-11-02 Thread Ricardo Aráoz
Kent Johnson wrote: > I am building a list like this: > > tree = [] > for top in tops: > l2 = level2(top) > if l2: > tree.append((top, l2)) > > I would really like to turn this into a list comprehension: > > tree = [ (top, level2(top)) for top in tops if

Re: [Tutor] List comp question

2007-11-02 Thread Michael Langford
Decorate level2 with a decorator that caches: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425445 --Michael On 11/1/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > > I am building a list like this: > > tree = [] > for top in tops: > l2 = level2(top) > if

[Tutor] List comp question

2007-11-02 Thread Kent Johnson
I am building a list like this: tree = [] for top in tops: l2 = level2(top) if l2: tree.append((top, l2)) I would really like to turn this into a list comprehension: tree = [ (top, level2(top)) for top in tops if level2(top) ] but the call to level2() is