constucting a lookup table
Larry Bates
larry.bates at websafe.com
Tue May 16 11:36:04 EDT 2006
mhodkin at 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
More information about the Python-list
mailing list