constucting a lookup table

M

mhodkin

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
 
L

Larry Bates

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
 
M

mhodkin

Larry said:
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
 
M

mhodkin

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

Scott David Daniels

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
(e-mail address removed)
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top