Py学习  »  Python

python对象的自定义过滤函数

Seth Killian • 5 年前 • 1728 次点击  


# For the sake of example - assume that a hashing function is implemented based on the score

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

pairs = [(1, <Object 1>), (1, <Object 1>), (3, <Object 7>), (9, <Object 3>), (9, <Object 4>)]

filtered_pairs = [(1, <Object 1>), (3, <Object 7>), (9, <Object 4>)]

我知道我可以打电话 set

我知道我可以从itertools中进行groupby,并使用分数作为键实现排序,然后从每个组中获取最后一项,但我想知道是否有更有效的方法。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54697
 
1728 次点击  
文章 [ 4 ]  |  最新文章 5 年前