在Python 2.x中,我可以将自定义函数传递给sorted和.sort函数
>>> x=['kar','htar','har','ar']
>>>
>>> sorted(x)
['ar', 'har', 'htar', 'kar']
>>>
>>> sorted(x,cmp=customsort)
['kar', 'htar', 'har', 'ar']
Run Code Online (Sandbox Code Playgroud)
因为,在我的语言中,配偶带有这个命令
"k","kh",....,"ht",..."h",...,"a"
Run Code Online (Sandbox Code Playgroud)
但在Python 3.x中,看起来我无法传递cmp关键字
>>> sorted(x,cmp=customsort)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'cmp' is an invalid keyword argument for this function
Run Code Online (Sandbox Code Playgroud)
有没有其他选择,还是我应该编写自己的排序函数?
注意:我通过使用"k","kh"等进行了简化.实际字符是Unicodes甚至更复杂,有时元组前后有元音,我已经完成了自定义比较功能,所以那部分还可以.唯一的问题是我无法将自定义比较函数传递给sorted或.sort