To get just the maximum value and not the entire object you can use a generator expression:
print(max(node.y for node in path.nodes))
Answer from Mark Byers on Stack Overflowpython - Get the object with the max attribute value in a list of objects - Stack Overflow
python - Get the object with the max attribute's value in a list of objects - Stack Overflow
How to find maximum values from a list which contains objects in Python? - Stack Overflow
python - Get a list of objects with the maximum attribute value in a list of objects - Stack Overflow
To get just the maximum value and not the entire object you can use a generator expression:
print(max(node.y for node in path.nodes))
There is an important difference for when to use the "Pythonic" style #1 versus lambda style #2:
max(node.y for node in path.nodes) # (style #1)
versus
max(path.nodes, key=lambda item: item.y) # (style #2)
If you look carefully you can see that style #1 returns the maximum value for the attribute y while style #2 returns the node that has maximum attribute y. These two are not the same and code usage is important in case you want to iterate over the attribute values or iterate over the objects that holds that attribute.
Example:
class node():
def __init__(self,x):
self.x = x
self.y = self.x + 10
node_lst = [node(1), node(2), node(3), node(4), node(5)]
print([(e.x,e.y) for e in node_lst])
>>> [(1, 11), (2, 12), (3, 13), (4, 14), (5, 15)]
Now:
maxy = max(node.y for node in node_lst)
print(maxy)
>>> 15
max_node = max(node_lst, key=lambda node: node.y)
print(max_node.y)
>>> 15
You can use the max function with a key:
max_obj = max(your_list, key=lambda p: p.x if p.x > p.y else p.y)
To get the maximum value, you can do
max_val = max_obj.x if max_obj.x > max_obj.y else max_obj.y
print(max_val)
Or (as OP suggests)...
max_val = max(max_obj.x, max_obj.y)
print(max_val)
While you can use max() and key like @Coldspeed said, in the long term I believe a better solution would be to overload the less than and greater than operators for your Point class. That way you only have to implement the logic once. Also, if you decide to change the logic in the future, you'll only have to change it in one place:
>>> class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __gt__(self, point):
return self.x > point.x or self.y > point.y
def __lt__(self, point):
return self.x < point.x or self.y < point.y
>>> points = [Point(1,3), Point(5,2), Point(8,0)]
>>> point = max(points)
>>> point.x
8
>>>
if you want to get the maximum value you can use a simple ternary condition:
max_val = point.x if point.x > point.y else point.y
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]
The max() function can be used to find the value of the highest score.
To get the objects whose score matches that value, you could do a list comprehension like in @juanpa.arrivillaga's answer, or use something like filter() on the list to return only the items matching your criterion.
top_score = max(front_Ar, key=attrgetter('score')).score
max_ind = list(filter(lambda x: x.score == top_score, front_Ar))