Quantcast
Viewing latest article 3
Browse Latest Browse All 4

Answer by Mario Ishac for Find out max value from a python dict

It sounds like you want to get the maximum value across each sub-key (the first item of each entry's value). To do that, you can use this:

from collections import defaultdictmax_values = defaultdict(lambda: (float('-inf'), None))for label, text, value in d.values():    max_values[label] = max(max_values[label], (value, text))

Using defaultdict here with a default value of (float('-inf'), None) allows us to compare new max values to old new values without having to check if a max value was recorded in the first place.

max_values ends up as:

{'1': (0.8, 'Toronto'), '2': (0.8871413469314575, 'Arkansas'), '3': (0.8, 'Toronto'), '4': (0.8, 'Turkey'), '5': (0.8, 'Hebron, Kentucky'),'6': (0.8, 'Ontario'), '7': (0.9545853137969971, 'London'), '8': (0.8454625606536865, 'UNITED STATES OF AMERICA'),'9': (0.8, 'Raleigh–Durham International Airport'), '10': (0.8, 'United States')}

Viewing latest article 3
Browse Latest Browse All 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>