python - Selecting a value based on what range the value falls under -


i have problem dont know how solve. have julian day number (integer) , distance value (float) , have number julian day of interest (integer). want take actual julian day , fit julian day ranges (i.e julian day 1-14) can determine distance value specific julian day. julian day 10, fits in julian day range or 1 - 14, making distance = 0.9832. on appreciated. ve tried far use if else dont think work @ all. ideas welcome. (let me know if not clear m trying do!)

julian day distance 1 0.9832 15 0.9836 32 0.9878 46 0.9878 60 99.0900

assuming you're expecting performance across multiple lookups, you're trying can achieved using binary search algorithm.

in fact, searching "binary search" found a similar question.

if performance isn't issue, simple loop through day-distances work.

either way, i'd start of putting day-distances sequential data structure:

day_distances = [( 1,  0.9832),                  (15,  0.9836),                  (32,  0.9878),                  (46,  0.9878),                  (60, 99.0900),                 ] 

now, if it's simple loop i'm using (and don't care @ inefficiency) it'll like:

find_day = 10 found_distance = none  day, distance in reversed(sorted(day_distances)):     if day <= find_day:         found_distance = distance         break 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -