Home > OS >  Find tuple in list of tuples in python
Find tuple in list of tuples in python

Time:12-01

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.

CodePudding user response:

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)

CodePudding user response:

Building off of @Manlai A’s answer:

Try this:

next(v for v in AoD_log if v[1] == max(AoD_log, key=lambda x: x[1]))
  • Related