Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取对象列表中具有最大属性值的对象列表_Python_List_Attributes_Max - Fatal编程技术网

Python 获取对象列表中具有最大属性值的对象列表

Python 获取对象列表中具有最大属性值的对象列表,python,list,attributes,max,Python,List,Attributes,Max,与…稍有不同。我在这里发现: front\u Ar是具有score属性的对象列表 我试图得到一份所有得分最高的物品的清单。 我试过: maxind = [] maxInd.append(max(front_Ar, key=attrgetter('score'))) 它只存储了一个对象(可能是它找到的第一个对象)。 你知道怎么做吗?先找到最大分数,然后根据该分数筛选列表: max_score = max(front_Ar, key=attrgetter('score')).score max_i

与…稍有不同。我在这里发现:
front\u Ar
是具有
score
属性的对象列表

我试图得到一份所有得分最高的物品的清单。 我试过:

maxind = []
maxInd.append(max(front_Ar, key=attrgetter('score')))
它只存储了一个对象(可能是它找到的第一个对象)。
你知道怎么做吗?

先找到最大分数,然后根据该分数筛选列表:

max_score = max(front_Ar, key=attrgetter('score')).score
max_ind = [obj for obj in front_Ar if obj.score == max_score]
max()
函数可用于查找最高分数的值

要获得分数与该值匹配的对象,可以执行@juanpa.arrivillaga的答案中所述的列表理解,或者在列表上使用类似于
filter()
的内容,仅返回符合条件的项

top_score = max(front_Ar, key=attrgetter('score')).score
max_ind = list(filter(lambda x: x.score == top_score, front_Ar))