Question

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.

Was it helpful?

Solution

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])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top