在Python 2.x中,我可以将自定义函数传递给sorted和.sort函数。
>>> x=['kar','htar','har','ar']
>>>
>>> sorted(x)
['ar', 'har', 'htar', 'kar']
>>>
>>> sorted(x,cmp=customsort)
['kar', 'htar', 'har', 'ar']
因为在我的语言中,辅音按照这个顺序出现
"k","kh",....,"ht",..."h",...,"a"
但在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
有没有替代方案,还是我需要自己编写排序函数?
注意:我简化了使用“k”,“kh”等。实际字符是Unicode甚至更加复杂,有时元音在辅音之前和之后,我已经编写了自定义比较函数,这部分没问题。唯一的问题是我无法将自定义比较函数传递给sorted或.sort。
sorted(x)
吗? - SilentGhost