Get Maximum Element Of List Of List If Condition Is Met

22 September 2019 - 1 answer

I have list of lists:

`````` [[[10, 15, 200, 220], [10, 15, 200, 220], 0.0],
[[10, 15, 200, 220], [20, 25, 200, 230], 17.320508075688775],
[[110, 150, 240, 300], [10, 15, 200, 220], 190.3286631067428],
[[110, 150, 240, 300], [100, 150, 230, 300], 14.142135623730951],
[[110, 150, 240, 300], [110, 150, 240, 300], 0.0]]
``````

I want to get maximum value of each element in a list, if the third real number is less than a threshold (let's say 50). In other words in the above case, the desired resulst would be:

1) filter results based on the threshold, e.g.

`````` [[[10, 15, 200, 220], [10, 15, 200, 220], 0.0],
[[10, 15, 200, 220], [20, 25, 200, 230], 17.320508075688775],
[[110, 150, 240, 300], [100, 150, 230, 300], 14.142135623730951],
[[110, 150, 240, 300], [110, 150, 240, 300], 0.0]]
``````

2) get maximum of each element in list:

``````[20, 25, 200, 230]
[110, 150, 240, 300]
``````

``````>>> L = [[[10, 15, 200, 220], [10, 15, 200, 220], 0.0],
...  [[10, 15, 200, 220], [20, 25, 200, 230], 17.320508075688775],
...  [[110, 150, 240, 300], [10, 15, 200, 220], 190.3286631067428],
...  [[110, 150, 240, 300], [100, 150, 230, 300], 14.142135623730951],
...  [[110, 150, 240, 300], [110, 150, 240, 300], 0.0]]
``````

You can filter the lists of lists on the last element with a list comprehension:

``````>>> [M for M in L if M[-1]<50]
[[[10, 15, 200, 220], [10, 15, 200, 220], 0.0], [[10, 15, 200, 220], [20, 25, 200, 230], 17.320508075688775], [[110, 150, 240, 300], [100, 150, 230, 300], 14.142135623730951], [[110, 150, 240, 300], [110, 150, 240, 300], 0.0]]
``````

Now, to merge the previous lists, you can use a zip (I added a filter on `0` that is implied by the desired output):

``````>>> [[max(*xs) for xs in zip(*M[:-1])] for M in L if 0<M[-1]<50]
[[20, 25, 200, 230], [110, 150, 240, 300]]
``````

Explanation: the idea is to zip the lists, e.g.:

``````>>> list(zip([10, 15, 210, 220], [10, 25, 200, 250]))
[(10, 10), (15, 25), (210, 200), (220, 250)]
``````

And to take the greatest element of tuples:

``````>>> [max(x,y) for x,y in zip([10, 15, 210, 220], [10, 25, 200, 250])]
[10, 25, 210, 250]
``````

I used the star operator to make the code more flexible: it doesn't rely on the number of lists:

``````>>> list(zip([10, 15, 210, 220], [10, 25, 200, 250], [15, 20, 210, 260]))
[(10, 10, 15), (15, 25, 20), (210, 200, 210), (220, 250, 260)]
>>> [max(*xs) for xs in zip([10, 15, 210, 220], [10, 25, 200, 250], [15, 20, 210, 260])]
[15, 25, 210, 260]
``````