我正在使用 Python3,但脚本与此版本不兼容,我遇到了一些错误。现在我对 cmp
参数有疑问。这是代码
def my_cmp(x,y):
counter = lambda x, items: reduce(lambda a,b:a+b, [list(x).count(xx) for xx in items])
tmp = cmp(counter(x, [2,3,4,5]), counter(y, [2,3,4,5]))
return tmp if tmp!=0 else cmp(len(x),len(y))
for i, t in enumerate([tmp[0] for tmp in sorted(zip(tracks, self.mapping[idx][track_selection[-1]].iloc[0]), cmp=my_cmp, key=lambda x:x[1])]):
img[i,:len(t)] = t
如果能帮助我解决 Python3 中的这个错误,我将不胜感激。
最佳答案
来自 python 文档
In Python 2.7, the functools.cmp_to_key() function was added to the functools module.
python 3 中也可用的函数。
只需用 cmp_to_key 包装你的 cmp 函数
from functools import cmp_to_key
...
...key=cmp_to_key(my_cmp)...
关于python - 类型错误 : 'cmp' is an invalid keyword argument for this function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28502774/