473,624 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,a ddr):
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(s elf,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
Nov 19 '07 #1
5 6319
On Mon, 19 Nov 2007 00:18:33 -0800, davenet wrote:
The objects are defined as follows:

class Block:
def __init__(self,a ddr):
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(s elf,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
Nov 19 '07 #2
On Mon, 19 Nov 2007 00:18:33 -0800, davenet wrote:
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,a ddr):
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(s elf,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(l ist_of_objects) :
for item in list_of_objects :
if item is smaller than all the other items:
return item
# And continue:
def find_smallest(l ist_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,
--
Steven.
Nov 19 '07 #3
On Mon, 19 Nov 2007 20:00:27 +1100, Ben Finney wrote:
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.

--
Steven.
Nov 19 '07 #4
davenet wrote:
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,a ddr):
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(min)
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.
>>def f(x) : return 3*x**2+10*x-4
>>min(f(x) for x in range(-100,100))
-12
>>min(range(-100,100), key=f)
-2
>>f(-2)
-12

Nov 19 '07 #5
Boris Borcic wrote:
davenet wrote:
>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,a ddr):
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(min)
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.
>>def f(x) : return 3*x**2+10*x-4
>>min(f(x) for x in range(-100,100))
-12
>>min(range(-100,100), key=f)
-2
>>f(-2)
-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
Nov 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1030
by: RDJ | last post by:
=== This is a AoooAoooA project (^_^) === DictObjSys (DOS): Dictionary Objects System V.0.0.1 (2004 Oct 26) Copyright (C) 2004 RDJ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
8
3070
by: starman7 | last post by:
i have a table with objects in categories and their positions. there will be several rows with category 400, and they will have various positions, i want to delete only the row with the lowest position. i can select the row i want to delete, but don't know how to delete just this row. here's my select:
5
1944
by: jayipee | last post by:
hi to all. can anybody help me figuring out how to get the minimum value from this list. log1 354 log2 232 log3 155 from the above list i want to have a return value "log3" since this is the lowest value in the list.
2
2013
sonic
by: sonic | last post by:
Does anyone know what I can do to this function to get it to drop the lowest value? Thanks for any insight. void sort(double* score, int size) { int startScan; int minIndex; double minValue;
30
5232
by: =?Utf-8?B?VGhvbWFzVDIy?= | last post by:
Hello, I have an array that holds 4 values ( they contain random numbers). I would like to find the lowest value (if there is a tie i would like to find one of the tie.) then remove that value. I am new to Programming and C#. Thanks for any help you can provide Thomas
15
17517
by: timothytoe | last post by:
Situation: I have an array of objects. I want to find the maximum value of a given numeric value that exists in each of the objects. Suppose the array of objects is called "stat" and the numeric value I want to check is called "member". Obviously, a for loop would do it: var maximum=stat.member;
7
2654
by: Margie | last post by:
With information gathered from a question I posted 3 weeks ago, I made the following query for my movie database: SELECT Nation.Nation, LinkFilmNation., Count(*) AS Total, Avg(Film.) AS FROM Film INNER JOIN Nation INNER JOIN LinkFilmNation ON Nation. = LinkFilmNation.) ON Film. = LinkFilmNation. GROUP BY Nation.Nation, LinkFilmNation., Film.Status, Film.Ok HAVING (((Film.Status)="Seen") AND ((Film.Ok)=Yes)) ORDER BY Count(*) DESC; ...
7
7390
by: Alberto Fortuny | last post by:
Hey guys, any idea as to how to find the lowest value and the highest value in this 2D array? I made the functions to find the lowest and highest, but all im getting on execution is a 1 on both, no matter what number i input. Here's my code #include <cstdlib> #include <iostream> using namespace std; const int MONKEYS = 3; const int DAYS = 7; double average = 0;
2
3354
by: Gurpreet Singhh | last post by:
I need to select maximum of the Emp_id values from SQL server 2005 table.I was using the command which selects max value till 10 but after that it fails to pick max value of emp id from the table.(Actually it picks up Max value using by looking at 1st digit only,so till 9 its fine, but after that it shows 9>11 or 9>57 etc.So please tell me SQL query for finding max value in Emp id column.Thanx. MY sql query used:- select MAX(emp_id) from...
0
8249
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8348
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1797
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.