Chris L
Chris L

Reputation: 1061

interpolation of data in dictionary python 3

I have a python program which performs calculations using nested dictionaries. The problem is, if someone enters a value not in one of the dictionaries it won't work. I can either force the user to choose from the values but I'd rather perform interpolation to get the 'expected' value. I cannot figure out how to unpack these dictionaries, get them ordered, and perform the interpolation though.

Any help would be greatly appreciated. My code is below.

Dictionaries like this:

from decimal import *
pga_values = {
    "tee": {
        100:2.92, 120:2.99, 140:2.97, 160:2.99, 180:3.05, 200:3.12,       240:3.25, 260:3.45, 280:3.65,
    300:3.71, 320:3.79, 340:3.86, 360:3.92, 380:3.96, 400:3.99, 420:4.02, 440:4.08, 460:4.17,
    480:4.28, 500:4.41, 520:4.54, 540:4.65, 560:4.74, 580:4.79, 600:4.82
},
"fairway": {
    5:2.10,10:2.18,20:2.40,30:2.52,40:2.60,50:2.66,60:2.70,70:2.72,80:2.75,

ETC... (edited to be concise)

lie_types = set(pga_values.keys())
user_preshot_lie = input("What was your pre-shot lie type?")
user_preshot_distance_to_hole = Decimal(input('How far away from the hole   were you before your shot?'))
user_postshot_lie = input("What was your post-shot lie type?")
user_postshot_distance_to_hole = Decimal(input('How far away from the hole were you?'))
assert user_preshot_lie in lie_types
assert user_postshot_lie in lie_types

preshot_pga_tour_shots_to_hole_out = pga_values[user_preshot_lie][user_preshot_distance_to_hole]
postshot_pga_tour_shots_to_hole_out = pga_values[user_postshot_lie][user_postshot_distance_to_hole]

user_strokes_gained = Decimal((preshot_pga_tour_shots_to_hole_out -     postshot_pga_tour_shots_to_hole_out)-1)

print(user_strokes_gained)

Upvotes: 0

Views: 1510

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 882401

Given e.g to isolate the problem a bit:

tee = {
    100:2.92, 120:2.99, 140:2.97, 160:2.99, 180:3.05, 200:3.12, 240:3.25, 260:3.45, 280:3.65,
300:3.71, 320:3.79, 340:3.86, 360:3.92, 380:3.96, 400:3.99, 420:4.02, 440:4.08, 460:4.17,
480:4.28, 500:4.41, 520:4.54, 540:4.65, 560:4.74, 580:4.79, 600:4.82
}

you could have...:

import bisect
teekeys = sorted(tee)

def lookup(aval):
    where = bisect.bisect_left(teekeys, aval)
    lo = teekeys[where-1]
    hi = teekeys[where]
    if lo==hi: return tee[lo]
    delta = float(aval-lo)/(hi-lo)
    return delta*tee[hi] + (1-delta)*tee[lo]

So for example:

print(lookup(110))
2.955
print(lookup(530))
4.595

Not sure what you want to do if the value is <min(tee) or >max(tee) -- is raising an exception OK in such anomalous cases?

Upvotes: 1

Related Questions