How to get the maximum value of a dictionary tuple

Question:

I have a dictionary like this:

dic={(0, 1): 0.059999999999999996,
 (0, 5): 0.13157894736842105,
 (0, 15): 0.23157894736842105,
 (1, 0): 0.049999999999999996,
 (5, 0): 0.13157894736842105,
 (5, 15): 0.049999999999999996,
 (15, 5): 0.23157894736842105}

I would like to get the maximum value for each element in the first coordinate of the vector and also the second element of the vector.

The output would be:
For 0 [First coordinate]:      (0, 5): 0.13157894736842105
For 0 [Second coordinate]:      (5, 0): 0.13157894736842105
For 1 [First coordinate]:       (1,0) 0.049999999999999996
For 1 [Second coordinate]:       (0,1) 0.059999999999999996
and so on.

I know that I can use something like this

max_keys = [k for k, v in dic.items() if v == max_value] 

but I am not able to get the correct way.

Asked By: DanielTheRocketMan

||

Answers:

def getMaxForX(number):
    return max([v for k, v in dic.items() if k[0] == number])

def getMaxForY(number):
    return max([v for k, v in dic.items() if k[1] == number])

I’m not sure how to fully implement these, but I think this is the list comprehension you’re looking for

e.g:

getMaxForX(0) => 0.23157894736842105,
getMaxForY(0) => 0.13157894736842105

If you wanted both the key and value for each maximum that’s a little different but still doable.

Answered By: Brandon Sturgeon

This can be done in a oneliner

For example, for 0 [First coordinate]:

print(max([(k, v) for k, v in filter(lambda x: x[0][0]==y, dic.items())], key=lambda x:x[1]))
Out[2]: ((0, 15), 0.23157894736842105)

But better is to put this into a function:

def get_max(dic, coord, val):
    return max(filter(lambda item: item[0][coord] == val, dic.items()), key=lambda x: x[1])

For 0 [First coordinate]:

print(get_max(dic, 0, 0))
Out[5]:  ((0, 5), 0.23157894736842105)
# or storing the key and the value:
key_max, value_max = get_max(dic, 0, 0)

For 0 [Second coordinate]:

print(get_max(dic, 1, 0))
Out[6]: ((5, 0), 0.13157894736842105)

and so on…

Hope that helped and happy coding!

Answered By: j-i-l

This problem isn’t well suited to a dictionary with tuple keys. You would need to iterate and maintain your own results dictionary to keep the maximum values for each index key.

Or you can use a 3rd party library such as Pandas which holds structured data and utilises contiguous memory blocks:

import pandas as pd

dic = {(0, 1): 0.059999999999999996, (0, 5): 0.13157894736842105,
       (0, 15): 0.23157894736842105, (1, 0): 0.049999999999999996,
       (5, 0): 0.13157894736842105, (5, 15): 0.049999999999999996,
       (15, 5): 0.23157894736842105}

df = pd.DataFrame.from_dict(dic, orient='index').reset_index()
df[['key0', 'key1']] = pd.DataFrame(df.pop('index').values.tolist())

res = pd.concat((df.groupby(f'key{i}')[0].max().rename(f'idx{i}')
                 for i in range(2)), axis=1)

print(res)

        idx0      idx1
0   0.231579  0.131579
1   0.050000  0.060000
5   0.131579  0.231579
15  0.231579  0.231579
Answered By: jpp