I've written a sort in Python2, I'm trying to convert it into Python3 which asks for a key and says no more cmp function is available :
test.sort(lambda x, y: cmp(x[2],y[2]) or cmp(x[4], y[4]) or cmp(y[9], x[9]))
Any advices ?
Best regards,
I've written a sort in Python2, I'm trying to convert it into Python3 which asks for a key and says no more cmp function is available :
test.sort(lambda x, y: cmp(x[2],y[2]) or cmp(x[4], y[4]) or cmp(y[9], x[9]))
Any advices ?
Best regards,
Copyright © 2021 Jogjafile Inc.
The official python 3 documentation explains in this section the proper way of converting this from python 2 to 3.
The original
cmp
function simply does something likeThat is, it's equivalent to
sign(x-y)
, but also supports strings and other data types.However, your problem is that the current function of
sort
doesn't work with a comparison function with two arguments, but with a singlekey
function of one argument. Python providesfunctools.cmp_to_key
to help you convert it, so, do something like