Reputation: 351
There is a way to find the key that denotes the highest value in a dictionary (in Python) as per this question. I would like to do it a bit differently though.
Imagine we have a dictionary D, say:
D = {'a' : 1 , 'q' : 2, 'b' : 3, 'c': 2}
I would like to find the maximum value of the dictionary by looping over the values of the keys, each time comparing the values of two keys and then 'remember' the key that denotes highest value in a local variable. In the end, I should have found the key and its maximum value in D. In this case, we should have something like this:
compare('a', 'q') --> remember q
compare('q', 'b') --> remember b
compare('b', 'c') --> remember b
The maximum key is now 'b' with value 3.
But how do I compare the values of the keys in the for loop? How can I do something like:
for k,v in D.iteritems() :
if (dictitem) > (dictitem + 1) :
remember = dictitem
else :
remember = dictitem + 1
But now something that actually works?
Upvotes: 5
Views: 8381
Reputation: 1394
I think this could be another approach.You can use operator.itemgetter for that:
import operator
mydict = {'a' : 1 , 'q' : 2, 'b' : 3, 'c': 2}
max(mydict.iteritems(), key=operator.itemgetter(1))[0]
And instead of building a new list in memory use stats.iteritems(). The key parameter to the max() function is a function that computes a key that is used to determine how to rank items.This will work for cases, where there are two max instread of one. Like in the case of
mydict = { 'a' : 1 , 'q' : 2, 'b' : 3, 'c': 2 , 'd' : 7 , 'e' : 8 , 'd' : '4' }
The output in this case would be 'd'. As one of the values of d is max.
Upvotes: 1
Reputation: 1242
Since you want to do this in a for loop, you could try zenpoy's approach. But make sure to use better initialization for my_max_val
like MattNewville has done.
Here is the method similar to the structure you have provided in your question. `
D = {'a' : 1 , 'q' : 2, 'b' : 3, 'c': 2}
li = D.keys()
j = len(li)
for i in range(0,j-1):
if D[li[i]]>D[li[i+1]]:
maxi = D[li[i]]
else:
maxi = D[li[i+1]]
Upvotes: 0
Reputation:
zenpoy's approach starts with a value that may not be a valid result. If the question had been to find the key with minimum value, the approach would fail. What you would want (aside from using max(D, key=D.get) approach) is
D = {'a' : 1 , 'q' : 2, 'b' : 3, 'c': 2}
my_max_val = None
for k,v in D.items():
if my_max_val is None:
my_max_val=v
my_max_key=k
elif v > my_max_val:
my_max_val=v
my_max_key=k
Upvotes: 0
Reputation: 404
It looks like you want fnd the maxium number in for loop ranther than use built-in function like max, i write code as follow, i hope it's useful to you.
D= {'a' : 1 , 'q' : 2, 'b' : 3, 'c': 2}
for index, k in enumerate(D.iterkeys()):
if not index:
remember = k
else:
if D[k] > D[remember]:
remember = k
print remember
Upvotes: 0
Reputation: 20126
@henices's answer is great, and you should use it. But to give you a general note on how to implement a max function:
D = {'a' : 1 , 'q' : 2, 'b' : 3, 'c': 2}
my_max_val = 0
for k,v in D.items():
if v > my_max_val:
my_max_val=v
my_max_key=k
>>> my_max_val
3
>>> my_max_key
'b'
Upvotes: 2
Reputation: 349
>>> D = {'a' : 1 , 'q' : 2, 'b' : 3, 'c': 2}
>>> max(D, key=D.get)
'b'
Upvotes: 9