how can this iterator be optimized?

Paul McGuire ptmcg at
Sat Feb 14 10:19:45 EST 2009

On Feb 11, 7:22 pm, Basilisk96 <basilis... at> wrote:
> where "func" is a single-argument function that returns either a
> string or None, but is an expensive call.
> I am pretty sure that the sorted() construct cannot be improved much
> further, but...
> ...does anyone have ideas on improving the "rawPairs" iterator so that
> it calls "func(s)" only once per iteration?  Perhaps a lambda
> construct, but I am not sure how to go about it...?
> Cheers,
> Basilisk96

If func is expensive, you could try memoizing it.  Then subsequent
"calls" just do arg lookups.  Michele Simianato has posted a good
memoizing decorator on the Python wiki.

-- Paul

More information about the Python-list mailing list