how can this iterator be optimized?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Feb 11 20:38:56 EST 2009
En Wed, 11 Feb 2009 23:22:26 -0200, Basilisk96 <basilisk96 at gmail.com>
escribió:
> def MakePairs(path):
> import os
> import operator
> join = os.path.join
> rawPairs = (
> (join(path, s), func(s))
> for s in os.listdir(path)
> if func(s) is not None and func(s).endswith("some criterion")
> )
> #Use the second item in the pair as the sort criterion
> result = sorted(rawPairs, key=operator.itemgetter(1))
> return result
>
> 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...?
Don't use a generator then. If you're going to finally return a list (and
sorted does exactly that), build a list right from the start:
def MakePairs(path):
join = os.path.join
result = []
append = result.append
for s in os.listdir(path):
key = func(s)
if key is not None and key.endswith("some criterion"):
append((join(path, s), key))
#Use the second item in the pair as the sort criterion
result.sort(key=operator.itemgetter(1))
return result
--
Gabriel Genellina
More information about the Python-list
mailing list