By passing a callback function to max
to get a finer search, no further iterations are required.
y_max = max(dummy_list, key=lambda p: (p[0], 'Y'))[0]
print(y_max)
By decoupling the pairs and classify them wrt to the Y
,N
values
d = {}
for k, v in dummy_list:
d.setdefault(v, []).append(k)
y_max = max(d['Y'])
By a zip
-decoupling one can use a mask-like approach using itertools.compress
values, flags = zip(*dummy_list)
y_max = max(it.compress(values, map('Y'.__eq__, flags)))
print(y_max)
A basic for
-loop approach
y_max = dummy_list[0][0]
for i, c in dummy_list:
if c == 'Y':
y_max = max(y_max, i)
print(y_max)
EDIT: benchmark results.
Each data list is shuffle
d before execution and ordered from fastest to slowest. The functions tested are those given by the users and the given identifier (I hope) should make easy to recognize the right one.
Test repeated 100-times with data with 11 terms (original amount of data)
max_gen ms: 8.184e-04
for_loop ms: 1.033e-03
dict_classifier ms: 1.270e-03
zip_compress ms: 1.326e-03
max_key ms: 1.413e-03
max_filter ms: 1.535e-03
pandas ms: 7.405e-01
Test repeated 100-times with data with 110 terms (10 x more data)
max_key ms: 1.497e-03
zip_compress ms: 7.703e-03
max_filter ms: 8.644e-03
for_loop ms: 9.669e-03
max_gen ms: 9.842e-03
dict_classifier ms: 1.046e-02
pandas ms: 7.745e-01
Test repeated 100-times with data with 110000 terms (10000 x more data)
max_key ms: 1.418e-03
max_gen ms: 4.787e+00
max_filter ms: 8.566e+00
dict_classifier ms: 9.116e+00
zip_compress ms: 9.801e+00
for_loop ms: 1.047e+01
pandas ms: 2.614e+01
When increasing the amount of data the "performance classes" change but max_key
seems to be not affected.