Not quite the same approach as above, but this was my take on the question. I recreated the dictionary of keys as intervals, and if DIA
falls in that interval it would return that value, rather than the use of the closest.
Also you might want to change 1 to 1.0, to make sure that your value returns a floating number instead of an integer. (I'm assuming that is what you are after).
D_MINSLOPE = {100:60, 150:100, 225:300, 300:400, 375:550, 450:700, 525:750,
600:900, 675:1050, 750:1200, 825:1380, 900:1600, 1050:2000,
1200:2400, 1350:2800, 1500:3250, 1650:3700, 1800:4200}
def change_dict(dictionary):
"""
Changes the dictionary to contain intervals as keys
Returns:
{(1651, 1800): 4200, (301, 375): 550,
(1201, 1350): 2800, (451, 525): 750,
(751, 825): 1380, (526, 600): 900,
(1, 100): 60, (676, 750): 1200,
(1051, 1200): 2400, (1351, 1500): 3250,
(826, 900): 1600, (901, 1050): 2000,
(376, 450): 700, (151, 225): 300,
(101, 150): 100, (1501, 1650): 3700,
(601, 675): 1050, (226, 300): 400}
"""
new_dict = {}
temp = [0] + sorted(D_MINSLOPE.keys())
i = 0
while i < len(D_MINSLOPE.keys()):
key = temp[i+1]; value = D_MINSLOPE[key]
new_key = (temp[i] + 1, temp[i+1])
new_dict[new_key] = value
i += 1
return new_dict
def minslope(DIA):
DIA_mm = round(DIA * 1000)
D_MINSLOPE_2 = change_dict(D_MINSLOPE) # Change dictionary keys
# Find the key in which DIA_mm falls under
key = None
for i in D_MINSLOPE_2:
if (i[0] <= DIA) and (i[1] >= DIA):
key = i
break
# Change 1 to 1.0, as it needs to be a float number to output decimals
# otherwise it would probably output 0 as an integer.
return 1.0/D_MINSLOPE_2[key]