Home > OS >  How to find maximum values of specific data type in list of tuples and update dictionary with these
How to find maximum values of specific data type in list of tuples and update dictionary with these

Time:10-01

If I have the dictionary

max_values = {
    'int': 0,
    'float': 0
}

and I have such a list of tuples

mylist = [(1, 'int'), (3, 'int'), (2, 'float'), (4, 'float')]

How can I find the maximum value of the specific data_type and update the dictionary values?

The dictionary should be

max_values = {
    'int': 3,
    'float': 4
}

How to find maximum values and create new list?

new_list = [(3, 'int'), (4, 'float')]

CodePudding user response:

for value, data_type in mylist:
    max_values[data_type] = max(value, max_values[data_type])

CodePudding user response:

pandas is great for this especially if you have a large dataset:

import pandas as pd

mylist = [(1, 'int'), (3, 'int'), (2, 'float'), (4, 'float')]

a = pd.DataFrame(mylist).groupby(1).max().to_dict()[0]

Or you can use itertools.groupby:

from itertools import groupby
from operator import itemgetter

get_val, get_type = map(itemgetter, (0, 1))
max_values = {k: max(map(get_val, g)) for k, g in groupby(mylist, get_type)}

I used operator.itemgetter as it's more efficient than a lambda expression.

CodePudding user response:

You can first generate the dictionary with checking the maximum value of key for each type . Then using list comprehension you can generate the new list as shown below.

for key, typ in mylist:
    if key > max_values[typ]:
        max_values[typ] = key
print(max_values)
new_list = [(x,y) for y,x in list(max_values.items())]
print(new_list)

This will give outputs for dictionary and list as below.

{'int': 3, 'float': 4}
[(3, 'int'), (4, 'float')]

CodePudding user response:

It can be a one liner:

new_list = [max(mylist, key=lambda item: item[0] if item[1]==tp else 0) for tp in {v for k, v in mylist}]

You can find the types by creating a set

types = {v for _, v in mylist}
# {'int', 'float'}

you can find only the elements with a specific type by:

int_vals = [(val, tp) for val, tp in mylist if tp == 'int']

and you can find the max of the list above by telling max that you're interested in the max of the val part:

max(int_vals, key=lambda item: item[0])

you can combine the last two steps with:

max_int_val = max(mylist, key=lambda item: item[0] if item[1] == 'int')

and then you just need to iterate over the types to create the new list

new_list = []
for tp in types:
    new_list.append(max(mylist key=lambda item: item[0] if item[1] == tp)

which is an expanded version of the one-liner at the top.

If you also need the max_values dict, you can get it by:

max_values = {v: k for k, v in new_list}
  • Related