相关文章推荐

python sorted typeerror ' ' not supported between instances of 'nonetype' and 'nonetype'

这个错误消息表明你在使用 Python 的 sorted() 函数时出现了错误,其中的错误类型是 TypeError ,错误信息是「不支持 'nonetype' 和 'nonetype' 之间的比较」。

这通常是因为你传递给 sorted() 函数的参数中含有空值 None ,而 None 不支持大小比较,因此会导致这个错误。

为了解决这个问题,你可以在使用 sorted() 函数之前将 None 值从序列中移除,或者使用一个自定义的排序函数来指定如何比较这些值。

def compare_none(x, y):
    if x is None and y is None:
        return 0
    elif x is None:
        return 1
    elif y is None:
        return -1
    else:
        return x < y
# 使用自定义的比较函数
sorted(my_list, key=compare_none)

希望这些信息能帮到你。

  •  
    推荐文章