Finding lowest value in dictionary of objects, how?

D

davenet

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
 
M

Marc 'BlackJack' Rintsch

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.

Ciao,
Marc 'BlackJack' Rintsch
 
B

Ben Finney

davenet said:
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.
 
S

Steven D'Aprano

Hi,

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

Thank you for your honesty.

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 = {}


One possible approach is to define your Block data-type so that it
defines less-than and greater-than comparisons. Then you can just ask
Python to find the minimum Block by passing a list (not a dictionary) of
Blocks to the function min().

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.

Question: you don't explain how you have added them to the dict. A dict
has a key and a value. What are the keys, and what are the values?

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.

That code doesn't make much sense. It looks like a method rather than a
function (the argument "self" is the giveaway). What is self.blocks and
what is val?

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

The first step you should do is write down how YOU would solve the
problem.

"Let's see now... if I had a list of objects, and I wanted to find the
smallest one, I would look at the first object, and compare it to all the
other objects. If it was smaller than or equal to every other object in
the list, I've found the smallest object and I'm finished!

If not, I'd take the second object, and compare it to all the other
objects. If it is smaller than or equal to everything else, I've found
the smallest object, and I'm finished.

If not, I would do the same for the third, and fourth, and fifth, and so
forth, until I've found the smallest object."

Now start converting it to Python, step by step:

# Start with English instructions:
with each item in the list of objects:
if item is smaller than all the other items:
item is the smallest, and we're done


# Turn it into a function:
function find the smallest(list of objects):
with each item in the list of objects:
if item is smaller than all the other items:
item is the smallest, and we're done


# Now start using Python syntax:
def find_smallest(list_of_objects):
for item in list_of_objects:
if item is smaller than all the other items:
return item


# And continue:
def find_smallest(list_of_objects):
for item in list_of_objects:
for x in list_of_objects:
if item <= x:
return item



What I've done there is re-write the min() function in one of the
slowest, most inefficient ways possible. If you try doing it by hand,
you'll quickly see it's VERY inefficient. The better ways should be
obvious once you actually do it. Then go through the process of writing
it as Python code.



Hope this helps,
 
S

Steven D'Aprano

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.

Plagiarism does not mean asking people for help. Nor does it mean using
resources other than the official course material.

It means passing off other people's work as your own.

Even in the sad, debased, commercialized world of the 21st century
university, learning outside of the class is not yet forbidden.
 
D

davenet

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
 
B

Boris Borcic

davenet said:
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.
Help on built-in function min in module __builtin__:

min(...)
min(iterable[, key=func]) -> value
min(a, b, c, ...[, key=func]) -> value

With a single iterable argument, return its smallest item.
With two or more arguments, return the smallest argument.
-12
 
S

Steven Bethard

Boris said:
davenet said:
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.
Help on built-in function min in module __builtin__:

min(...)
min(iterable[, key=func]) -> value
min(a, b, c, ...[, key=func]) -> value

With a single iterable argument, return its smallest item.
With two or more arguments, return the smallest argument.
-12

I'd like to second this one just so it doesn't get lost among all the
other responses. You definitely want to look into the key= argument of
min(). Your key function should look something like::

def key(block):
# return whatever attribute of block you care about

Then just modify this code::

lowest = min(self.blocks.values())

to use the key= argument as well. I don't think you need the list
comprehension at all.

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top