I have a multi-list of objects as follows (simplified version)
listA = [[obj1(val=1),obj2(val=1)],[obj2(val=4),obj3(val=2)]]
listB = [[obj4(val=1),obj5(val=1)],[obj6(val=5),obj7(val=3)]]
listC = [[obj8(val=1),obj9(val=1)],[obj10(val=6),obj11(val=4)]]
I want to get a list of objects from the above multi-list which has the maximum value of a certain attribute by comparing the sub-lists of each multi-list. If the value of the attribute is the same for all the compared objects, it should get any one object.
output:
maxList = [obj1(value=1),obj10(val=6)]
There is a similar question to get object with maximum value of attribute from a list, but this case is for multi-list. I know this can be acheived with nested for loops, but there a must be a better way to do this with itertools and getattr ?
To simplify, let's demonstrate on regular integers. Adapt this approach to your object.
Given
import itertools as it
a = [[1, 1], [3, 2]]
b = [[1, 1], [5, 3]]
c = [[1, 1], [6, 3]]
Code
list(map(max, [list(it.chain(*col)) for col in zip(a, b, c)]))
# [1, 6]
Equivalently
[max([x for x in it.chain(*col)]) for col in zip(a, b, c)]
# [1, 6]