user8892141
user8892141

Reputation: 69

Return key with highest value

I have the following graph:

graph = {0 : {5:6, 4:8},
1 : {4:11},
2 : {3: 9, 0:12},
3 : {},
4 : {5:3},
5 : {2: 7, 3:4}}

I am trying to return the key that has the highest value in this graph. The expected output in this case would be 2 as key 2 has the highest value of 12.

Any help on how I can achieve this would be greatly appreciated.

Upvotes: 3

Views: 564

Answers (6)

RoadRunner
RoadRunner

Reputation: 26315

You can also try flattening your dictionary into a list of tuples then take the max of the tuple with the highest second value:

from operator import itemgetter

graph = {
    0: {5: 6, 4: 8},
    1: {4: 11},
    2: {3: 9, 0: 12},
    3: {},
    4: {5: 3},
    5: {2: 7, 3: 4},
}

result = max(((k, v) for k in graph for v in graph[k].values()), key=itemgetter(1))

print(result)
# (2, 12)

print(result[0])
# 2

Upvotes: 0

slider
slider

Reputation: 12990

You can also create (max_weight, key) tuples for each key and get the max of those:

max_val = max((max(e.values()), k) for k, e in graph.items() if e)
# (12, 2)

print(max_val[1])
# 2

Note that we don't need a custom key function for max here because the first value in the tuple is the one we want max to consider.

Upvotes: 1

Sufiyan Ghori
Sufiyan Ghori

Reputation: 18753

You can also use dict comprehension to flat the dictionary and then print the max key,

graph = {0 : {5:6, 4:8},
         1 : {4:11},
         2 : {3: 9, 0:12},
         3 : {},
         4 : {5:3},
         5 : {2: 7, 3:4}}

flat_dcit = {k:a for k, v in graph.items() for a in v.values()}
print(max(flat_dcit.keys(), key=(lambda k: flat_dcit[k])))

# output,
2

Upvotes: 0

John R
John R

Reputation: 1508

The recursive solution is below. Does not make assumptions about depth of your tree. Only assumes that data types are either int, float or dict

import type 

def getLargest(d):
    def getLargestRecursive(d):    
        if type(d) == “dict”:
            getLargestRecursive(d)
        elif not largest or d > largest:
              largest = d
    largest = None
    getLargestRecursive(d)
    return largest


largestValues = [getLargest(k) for k in graph.keys]

answer = largestValues.index(max(largestValues))

Upvotes: 0

Andreas
Andreas

Reputation: 2521

Assuming it's all positive numbers

graph = {0 : {5:6, 4:8},
         1 : {4:11},
         2 : {3: 9, 0:12},
         3 : {},
         4 : {5:3},
         5 : {2: 7, 3:4}}

highestKey = 0
max = 0

for key, value in graph.items():
    for key2, value2 in value.items():
        if (max < value2):
            max = value2
            highestKey = key

print(highestKey)

Upvotes: 3

Amadan
Amadan

Reputation: 198344

Find the key whose maximum value is maximal:

max((k for k in graph), key=lambda k: max(graph[k].values(), default=float("-inf")))

The empty elements are disqualified by the ridiculous maximum. Alternately, you can just pre-filter such keys:

max((k for k in graph if graph[k]), key=lambda k: max(graph[k].values()))

Upvotes: 7

Related Questions