I have a dictionary with IP addresses and their free memory space.
I'm trying to save an IP to one variable and the next highest one to another variable.
masterdict: {'192.168.100.102': '1620', '192.168.100.103': '470', '192.168.100.101': '1615'}
print masterdict
rmip = max(masterdict, key=masterdict.get)
print "<br>rmip<br>"
print rmip
del masterdict [rmip]
print "<br>masterdict after deletion<br><br>"
print masterdict
nnip = max(masterdict, key=masterdict.get)
print "<br>nnip<br>"
print nnip
The values in your dictionary are strings so max
is comparing them as strings. If you want to compare them as integers, then you need to use the key function to convert the strings to integers...:
rmip = max(masterdict, key=lambda key: int(masterdict[key])
Or change the values to int
and then use your current max statement:
masterdict = {k: int(v) for k, v in masterdict.items()}
rmip = max(masterdict, key=masterdict.get)