Finding lowest value in dictionary of objects, how?
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
--
http://mail.python.org/mailman/listinfo/python-list
Re: Finding lowest value in dictionary of objects, how?
On Nov 19, 1:00 am, Ben Finney <[EMAIL PROTECTED]> wrote: > davenet <[EMAIL PROTECTED]> writes: > > I'm new to Python and working on a school assignment. > > Thank you for being honest about this. > > You should carefully read the policies on plagiarism for your > school. In general, the student is expected to use the resources of > their course material, the lecturer and tutor, and their own > creativity to come up with the answers -- *not* ask on the internet. > > -- > \ "I must have a prodigious quantity of mind; it takes me as much | > `\ as a week sometimes to make it up." -- Mark Twain, _The | > _o__)Innocents Abroad_ | > Ben Finney First of all, this is not a Python class I'm taking in college, it's an operating systems class. Second, we're learning about memory levels and this assignment was given to us with parts written in Python but we could have used any other language to implement the solution. Third, I've never used Python before so I'm not familiar with all the features available in the language. Hence, I came here to ask so I could spend time implementing the actual assignment instead of trying to figure out how Python works. Thanks to everyone else who tried to point me in the right direction. David -- http://mail.python.org/mailman/listinfo/python-list
