简体   繁体   中英

Find the maximum value in a dictionary with a fixed element in a tuple as a key

I've searched high and low on this forum, but I haven't come across any solution for this particular problem. Suppose I have a dictionary looking like this:

d = {}

d['a', 0] = 0.12
d['a', 1] = 0.23
d['a', 2] = 0.53
d['b', 4] = 0.23
d['b', 5] = 0.40
d['b', 6] = 0.39

How can I find the key corresponding with the maximum value for a fixed first element? For example, if I want to find the 2nd element of the tuple which gives me a maximum value for fixed 'b' , how should I do that? (in this case, it should return 5 because 0.40 is the biggest value).

I tried max(d, key=d.get)[1] , but that gives me the key for the maximum of all values, so 2. Any ideas?

You have to basically iterate all the key/value pairs:

def find_max_second_key(d, first_key):
    return max((v, k2) for (k1, k2), v in d.items() if k1 == first_key)[1]

d = {}

d['a', 0] = 0.12
d['a', 1] = 0.23
d['a', 2] = 0.53
d['b', 4] = 0.23
d['b', 5] = 0.40
d['b', 6] = 0.39

print(find_max_second_key(d, 'a'))
# 2
print(find_max_second_key(d, 'b'))
# 5

Alternatively, you could transform your dictionary into a nested structure. That way you would only need to iterate through the items with the matching first key.

d = {}

d['a', 0] = 0.12
d['a', 1] = 0.23
d['a', 2] = 0.53
d['b', 4] = 0.23
d['b', 5] = 0.40
d['b', 6] = 0.39

d_nested = {}
for (k1, k2), v in d.items():
    d_nested.setdefault(k1, {})[k2] = v
print(d_nested)
# {'a': {0: 0.12, 1: 0.23, 2: 0.53}, 'b': {4: 0.23, 5: 0.4, 6: 0.39}}

print(max(d_nested['a'], key=d_nested['a'].get))
# 2
print(max(d_nested['b'], key=d_nested['b'].get))
# 5

Try this :

print([i[1] for i in d if d[i] == max([k for j,k in d.items() if j[0] == 'a'])][0])

It returns the maximum value of the possible second items of the dictionary keys (tuple) for which the value is maximum. If we search for those dictionary values which has keys with first element as 'a' , we get 0.12, 0.23, 0.53 . Among them 0.53 is the highest, so we print the second item of the tuple(key) which holds the value 0.53 .

if you want to sort on the bases of a value in key in a dictionary

def func(d,key):
    new_dic = {}
    for k,v in d.items():       
        if key in k :
            new_dic.update({k:v})
    return max(new_dic, key=lambda x:new_dic[x])

d = {}

d['a', 0] = 0.12
d['a', 1] = 0.23
d['a', 2] = 0.53
d['b', 4] = 0.23
d['b', 5] = 0.40
d['b', 6] = 0.39



print(func(d, 'b'))
# output ('b', 5) 

else if you want an overall result use

max(d,key=lambda x:d[x])

You can use the following key:

d = {('a', 0): 0.12, ('a', 1): 0.23, ('a', 2): 0.53, ('b', 4): 0.23, ('b', 5): 0.4, ('b', 6): 0.39}

k = 'b'
max(d, key=lambda x: d[x] if x[0] == k else float("-inf"))
# ('b', 5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM