Skip to content
Advertisement

Find tuple in list of tuples in python

I have a list of tuple (simulationtime,efficiency) and I want to find the tuple in which the maximum efficiency is stated with respect to the minimum simulation time. Any hints on how that could be done? A small part of the otherwise very large list:

[(109.00537427472713, 0.8), (109.00588429136333, 0.85), (109.00649436705454, 0.86), (110.00055419961151, 0.86), (110.00122432147343, 0.86), (110.00172424060818, 0.86), (110.00236418239592, 0.86), (110.00292411815163, 0.86)]

I want the output to be (109.00649436705454, 0.86) from the list since it corresponds to max efficiency at min simulation time. Right now I am doing:

next(v for v in AoD_log if v[1] >= 0.85)

but the maximum achievable efficiency: index[1] of the tuple is not always known.

Advertisement

Answer

your_list = [(109.00537427472713, 0.8), (109.00588429136333, 0.85), (109.00649436705454, 0.86), (110.00055419961151, 0.86), (110.00122432147343, 0.86), (110.00172424060818, 0.86), (110.00236418239592, 0.86), (110.00292411815163, 0.86)]
max_efficiency = max(your_list, key=lambda x : x[1])[1]
min_simulation_time = min((x for x in your_list if x[1]==max_efficiency))

Output:

(109.00649436705454, 0.86)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement