获取对象列表中具有最大属性值的对象列表 [英] Get a list of objects with the maximum attribute value in a list of objects

查看:61
本文介绍了获取对象列表中具有最大属性值的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

略有不同以前的问题.我在这里找到: front_Ar 是具有 score 属性的对象列表.

Slightly different from previous questions. I have found here: front_Ar is a list of objects with a score attribute.

我正在尝试获取得分最高的所有对象的列表.我尝试过:

I am trying to get a list of all objects with the highest score. I have tried:

maxind = []
maxInd.append(max(front_Ar, key=attrgetter('score')))

仅存储一个对象(可能是找到的第一个对象).知道怎么做吗?

which stored only one object (presumably the first one it found). Any idea how can this be done?

推荐答案

首先找到最高分数,然后根据该分数过滤列表:

Find the max score first, then filter the list based on that score:

max_score = max(front_Ar, key=attrgetter('score')).score
max_ind = [obj for obj in front_Ar if obj.score == max_score]

这篇关于获取对象列表中具有最大属性值的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆