473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

constucting a lookup table

I'm new to Python and want to contruct a "lookup table" which would be
similar to a spreadsheet in that a value is read along the first
column, then along the top row, and the intersection of the two gives a
value that is hard-coded, i.e. not mathmatically related. An example
would be a table with HEIGHT in the first column, WEIGHT in the first
row, and estimated SUITSIZE with the body of the table.

Which Python type would be most appropriate and/or efficient?

thanks

May 16 '06 #1
5 4388
How about a dictionary, keyed on tuples of (height, weight)?

mh*****@comcast.net wrote:
I'm new to Python and want to contruct a "lookup table" which would be
similar to a spreadsheet in that a value is read along the first
column, then along the top row, and the intersection of the two gives a
value that is hard-coded, i.e. not mathmatically related. An example
would be a table with HEIGHT in the first column, WEIGHT in the first
row, and estimated SUITSIZE with the body of the table.

Which Python type would be most appropriate and/or efficient?

thanks


May 16 '06 #2
mh*****@comcast.net wrote:
I'm new to Python and want to contruct a "lookup table" which would be
similar to a spreadsheet in that a value is read along the first
column, then along the top row, and the intersection of the two gives a
value that is hard-coded, i.e. not mathmatically related. An example
would be a table with HEIGHT in the first column, WEIGHT in the first
row, and estimated SUITSIZE with the body of the table.

Which Python type would be most appropriate and/or efficient?

thanks

I would not worry about being "efficient" at this point. There are not
enough combinations of height/weight to make even a linear search through
a list be a performance problem. One way would be to set up a dictionary
that is keyed on height/weight and have it return suit size. To work
properly you would need to express height in inches and weight in pounds
(in US measurements). Something like:

hwdict={(60, 80): <suite size for 5 ft 0 inch person weighing 80 lbs>,
(61, 80): <suite size for 5 ft 1 inch person weighing 80 lbs>,
(62, 80): <suite size for 5 ft 0 inch person weighing 80 lbs>,
May 16 '06 #3

Larry Bates wrote:
mh*****@comcast.net wrote:
I'm new to Python and want to contruct a "lookup table" which would be
similar to a spreadsheet in that a value is read along the first
column, then along the top row, and the intersection of the two gives a
value that is hard-coded, i.e. not mathmatically related. An example
would be a table with HEIGHT in the first column, WEIGHT in the first
row, and estimated SUITSIZE with the body of the table.

Which Python type would be most appropriate and/or efficient?

thanks

I would not worry about being "efficient" at this point. There are not
enough combinations of height/weight to make even a linear search through
a list be a performance problem. One way would be to set up a dictionary
that is keyed on height/weight and have it return suit size. To work
properly you would need to express height in inches and weight in pounds
(in US measurements). Something like:

hwdict={(60, 80): <suite size for 5 ft 0 inch person weighing 80 lbs>,
(61, 80): <suite size for 5 ft 1 inch person weighing 80 lbs>,
(62, 80): <suite size for 5 ft 0 inch person weighing 80 lbs>,
.
.
.
(88, 400): <suite size for 7 ft 4 inch person weighing 400 lbs>}

This would be fast because lookup would consist of only (not tested):

try: suitsize=hwdict[(heightininches, weightinpounds)]
except IndexError:
print "height/weight combination not found in lookup table"
-Larry Bates


thanks...much appreciated

May 17 '06 #4
BTW, just for kicks to compare languages, how would one implement the
same "lookup" table using C/C++? It would be a good lesson in high
level vs. lower level programming....

May 17 '06 #5
mh*****@comcast.net wrote:
BTW, just for kicks to compare languages, how would one implement the
same "lookup" table using C/C++? It would be a good lesson in high
level vs. lower level programming....

Something vaguely like:

unsigned char hwdict[89 - 60][401 - 80];

int
setsize(int height, int weight, int size)
{
int result; /* non-zero (old size) if changes an already set size */

assert(60 <= height && height<= 88 && 80 <= weight && weight<= 400);
result = hwdict[height - 60][weight - 80];
hwdict[height - 60][weight - 80] = size;
return result;
}

int
getsize(int height, int weight, int size)
{
assert(60 <= height && height<= 88 && 80 <= weight && weight<= 400);
return hwdict[height - 60][weight - 80];
}

--Scott David Daniels
sc***********@acm.org
May 17 '06 #6

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

Similar topics

11
by: John Collyer | last post by:
Hi, In assembly language you can use a lookup table to call functions. 1. Lookup function address in table 2. Call the function Like: CALL FUNCTION
1
by: James E | last post by:
I have a question about best practices of how to deal with lookup data from my C# apps. On a couple of occasions I have come across a problem where I have to automate inserting a record into a...
9
by: Koen | last post by:
Hi all, My application uses a lot of lookup tables. I've splitted the frontend (forms, reports, etc) from the backend (data). The database has around 10 different users. The values in the...
3
by: my-wings | last post by:
I've been reading about how evil Lookup fields in tables are, but I've got to be missing something really basic. I know this subject has been covered before, because I've just spent an hour or two...
1
by: Zachary Turner | last post by:
I want to make a Lookup Field based on another Lookup field. In other words, I have this table A with two fields: ID and Name, where ID is an Autonumber and Name is a friendly name. Then I have a...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
1
by: Paul H | last post by:
Say I have a table called tblPeopleInfo, one of the fields in the table is called FavouriteFruit. The FavouriteFruit field is a lookup field and will contain Apples, Oranges, Grapes etc..The list...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
0
by: =?Utf-8?B?RU1hbm5pbmc=?= | last post by:
(I originally posted this to the data access newsgroup but received no replies) I've got an Access 2003 mdb that I'm converting to VB.Net. I'm having trouble with getting the main data source to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.