Python 2D table lookup by labels - Python

TAGS :
Viewed: 11 - Published at: a few seconds ago

[ Python 2D table lookup by labels ]

I have a two dimensional table here:

X axis is the width (labeled 24 to 68 in bold font), and Y axis is the height (18 to 84 in bold). I want the user to be able to enter via prompt:

``````width = input("Enter width: ")
height = input("Enter height: ")
``````

and lookup and print the corresponding value on this table.

My crude solution started off just defining a matrix:

``````list1 = [
[229, 252, 275],
[242, 266, 289],
[256, 280, 305]]
``````

and looking up the values:

``````if width == "20" and height == "32":
print(list1[0][0])
elif width == "20" and height == "36":
print(list1[0][1])
``````

This would end up being a ton of work. I have other tables like this which are even bigger. There's gotta be an easier way to look up a value on a table based on their column and row labels.

I've checked around and I couldn't find anything like what I need. I'd really like to keep this all Python if possible. Thanks :)

You could create functions to map widths/heights to list indices, so you could do something like `list1[widthIndex("20")][heightIndex("32")]`. I suggest functions because, from your example, not all widths/heights are directly represented in the table. This functions might look about as ugly as your `if`s, but at least there would only be 2n and not n^2 of them; nd since your table is rather regular, a formula involving dividing by 6 or 4 might make it pretty compact.