how can this iterator be optimized?
Basilisk96
basilisk96 at gmail.com
Wed Feb 11 20:22:26 EST 2009
Hello all,
I have the following function that uses an intermediate iterator
"rawPairs":
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...?
Cheers,
Basilisk96
More information about the Python-list
mailing list