issue with cmp in Python3

2.5k views Asked by At

I am using an open source Python code. I think it has been written for Python 2, when I run it in Python3.3 I get this error:

TypeError: must use keyword argument for key function

pointing to these lines of code:

probs = [(word, pool[word]) for word in words if word in pool]
probs.sort(lambda x,y: cmp(y[1],x[1]))

Also similar part of the code:

       for pname, pprobs in pools.items():
        p = self.getProbs(pprobs, tokens)
        if len(p) != 0:
            res[pname] = self.combiner(p, pname)
    res = res.items()
    res.sort(lambda x,y: cmp(y[1], x[1]))

gives the same error.

I am a beginner in Python, so I appreciate if somebody can tell me how should I change the code.

1

There are 1 answers

4
simonzack On BEST ANSWER

cmp has been depracated. Use key instead.

probs = [(word, pool[word]) for word in words if word in pool]
probs.sort(key=lambda x: x[1])