社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python对象的自定义过滤函数

Seth Killian • 5 年前 • 1709 次点击  


# 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
 
1709 次点击  
文章 [ 4 ]  |  最新文章 5 年前