I am trying to compare elements in keys in a dictionary. I would like to first compare all first elements of the keys, and then if there are more than one with the highest value then compare the second element in those keys.
For example:
>>> my_dict = { 111: [50, 2, 34], 122: [50 , 4, 45], 133: [40, 1, 12], 144: [20, 5, 77]}
1) Search first elements for highest value. Result should be:
('111', '122')
2) Search second elements of '111' and '112' for lowest value. Result should be:
('111')
I found this discussion to find the key with the max value. One solution to return multiple keys that have the same max is:
>>> stats = {'a':1000, 'b':3000, 'c': 100, 'd':3000}
>>> [key for key,val in stats.iteritems() if val == max(stats.values())]
['b', 'd']
The problem here is that these keys only have one element. I need to figure out how to do it for keys with multiple elements.
I suppose I can return the first elements of each key like this:
>>> [item[0] for item in my_dict.values()]
['50', '50', '40', '20']
But then how do I compare them? Thanks in advance for your help!