私信  •  关注

kaya3 Paulius

kaya3 Paulius 最近创建的主题
kaya3 Paulius 最近回复了
4 年前
回复了 kaya3 Paulius 创建的主题 » python对象的自定义过滤函数

可以按分数排序、转换为dict(以便最大分数为dict值)和转换回元组列表:

class Object:
    def __init__(self, score):
        self.score = score
    def __repr__(self):
        return f'<Object {self.score}>'
    def __gt__(self, other):
        return self.score > other.score


pairs = [(1, Object(1)), (1, Object(1)), (3, Object(7)), (9, Object(4)), (9, Object(3))]
filtered_pairs = list(dict(sorted(pairs)).items())