Finding lowest value in dictionary of objects, how?

Marc 'BlackJack' Rintsch bj_666 at
Mon Nov 19 09:57:54 CET 2007

On Mon, 19 Nov 2007 00:18:33 -0800, davenet wrote:

> 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.

In the example `min()` finds the object with the lowest `id()`.  To change
that you can implement the `__cmp__()` method on your `Block` objects.

	Marc 'BlackJack' Rintsch

More information about the Python-list mailing list