Home > database >  find max value in a list of sets for an element at index 1 of sets
find max value in a list of sets for an element at index 1 of sets

Time:04-30

I have a list like this:

dummy_list = [(8, 'N'),
 (4, 'Y'),
 (1, 'N'),
 (1, 'Y'),
 (3, 'N'),
 (4, 'Y'),
 (3, 'N'),
 (2, 'Y'),
 (1, 'N'),
 (2, 'Y'),
 (1, 'N')]

and would like to get the biggest value in 1st column of the sets inside where value in the 2nd column is 'Y'.

How do I do this as efficiently as possible?

CodePudding user response:

You can use max function with generator expression.

>>> dummy_list = [(8, 'N'),
...  (4, 'Y'),
...  (1, 'N'),
...  (1, 'Y'),
...  (3, 'N'),
...  (4, 'Y'),
...  (3, 'N'),
...  (2, 'Y'),
...  (1, 'N'),
...  (2, 'Y'),
...  (1, 'N')]
>>>
>>> max(first for first, second in dummy_list if second == 'Y')
4

CodePudding user response:

You can use pandas for this as the data you have resembles a table.

import pandas as pd

df = pd.DataFrame(dummy_list, columns = ["Col 1", "Col 2"]) 
val_y = df[df["Col 2"] == "Y"]
max_index = val_y["Col 1"].idxmax()

print(df.loc[max_index, :])

First you convert it into a pandas dataframe using pd.DataFrame and set the column name to Col 1 and Col 2.

Then you get all the rows inside the dataframe with Col 2 values equal to Y.

Once you have this data, just select Col 1 and apply the idxmax function on it to get the index of the maximum value for that series.

You can then pass this index inside the loc function as the row and : (every) as the column to get the whole row.

It can be compressed to two lines in this way,

max_index = df[df["Col 2"] == "Y"]["Col 1"].idxmax()
df.loc[max_index, :]

Output -

Col 1    4
Col 2    Y
Name: 1, dtype: object

CodePudding user response:

max([i[0] for i in dummy_list if i[1] == 'Y'])

CodePudding user response:


max([i for i in dummy_list if i[1] == 'Y'])

output: (4, 'Y')

or


max(filter(lambda x: x[1] == 'Y', dummy_list))

output: (4, 'Y')

CodePudding user response:

You can use the key argument of max to filter the data by passing a function. It returns a term of the same type so you need to further select [0].

max(dummy_list, key=lambda p: p[1] == 'Y')[0]
  • Related