Finding lowest value in dictionary of objects, how?

davenet dtschoepe at gmail.com
Mon Nov 19 03:18:33 EST 2007


Hi,

I'm new to Python and working on a school assignment.

I have setup a dictionary where the keys point to an object. Each
object has two member variables. I need to find the smallest value
contained in this group of objects.

The objects are defined as follows:

class Block:
   def __init__(self,addr):
      self.addr = addr
      self.age = 0

My dictionary is defined as:
   blocks = {}

I have added 1000 (will hold more after I get this working) objects. I
need to find the lowest age in the dictionary. If there is more than
one age that is lowest (like if several of them are '1', etc), then I
can just pick randomly any that equal the lowest value. I don't care
which one I get.

I saw the following code here but I don't know how to use this sample
to get at the values I need in the blocks object.

def key_of_lowest(self,addr)
   lowest = min(self.blocks.values())
   return [k for k in self.blocks if self.blocks[k]==val][0]

This one returns the lowest value Object, but not the lowest value of
age in all the Objects of the table.

I hope someone can help me figure out the syntax for what I'm trying
to do.

Thanks in advance.

David



More information about the Python-list mailing list