Sort with extra variables
Diez B. Roggisch
deets at nospam.web.de
Fri Mar 2 14:33:45 EST 2007
Thomas Dybdahl Ahle schrieb:
> I have a sort function in a python chess program.
> Currently it looks like this:
>
> def sortMoves (board, table, ply, moves):
> f = lambda move: getMoveValue (board, table, ply, move)
> moves.sort(key=f, reverse=True)
> return moves
>
> However I'd really like not to use the lambda, as it slows down the code.
>
> I've thought about saving the extra variables in the global space, but it
> really feals ugly.
>
> Do you have any ideas how I can sort these moves the fastest?
First of all, in your case it is somewhat strange to use
f = lambda ...
because then you could as well use
def f(move):
....
But that is just a general remark. Regarding the question: I don't see
how that could possibly become faster without much more insight into
what you are doing in getMoveValue. As it seems, it is dependend of a
lot of factors that change often, so caching it isn't a real option. And
I hope you are aware that the key-method is invoked only _once_ per
list-item!
Thus it is pretty efficient.
Diez
More information about the Python-list
mailing list