from functools import cmp_to_key
a = [56, 14, 11, 756, 34, 90, 11, 11, 65, 0, 11, 35]
def compare(a, b):
if a!= 11 or b != 11:
return a - b
print(sorted(a, key=cmp_to_key(compare)))
sao của mình nó k sort ra kết quả như này
[0, 14, 11, 34, 35, 56, 11, 11, 65, 90, 11, 756]
mà lại báo lỗi
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.1.4\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode
coro = func()
^^^^^^
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.1.4\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.1.4\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "E:\Code\python\sort_comparotor.py", line 9, in <module>
print(sorted(a, key=cmp_to_key(compare)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '<' not supported between instances of 'NoneType' and 'int'
mong các cao nhân giúp đỡ