0

I'm porting code from Python 2.7 to 3. 2to3 does not convert the following lines and can't seem to figure it out. Any help is appreciated.

subpaths.sort(
    lambda x, y :
        int(pyx.unit.tocm(x.arclen() - y.arclen()) /
            math.fabs(pyx.unit.tocm(x.arclen() - y.arclen()))) )
3
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable Example. Commented Jan 24, 2016 at 20:49
  • Without knowing what subpaths is and what x and y are, it's impossible to answer this question. Commented Jan 24, 2016 at 20:51
  • And yet it's answered... Commented Jan 26, 2016 at 2:37

1 Answer 1

2

The sort method of a list requires a key, which is a function of just one argument. You need to convert your lambda function to a function of a single argument. There is a shortcut for that provided by functools.cmp_to_key. Thus, what you probably need is:

import functools
subpaths.sort(key=functools.cmp_to_key(lambda x, y: ...))

Note that if I understand your code correctly, you can simply sort the list using the following key:

subpaths.sort(key=lambda x: pyx.unit.tocm(x.arclen()))
Sign up to request clarification or add additional context in comments.

1 Comment

This solution is definetly the quickest to implement. But, if OP has time, IMO he should consider replacing cmp lambda with written from scratch key lambda, for improved efficency and readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.