Finding closest value in a dictionary
Since the values for a given a are strictly increasing with successive i values, you can do a binary search for the value that is closest to your target.
While it's certainly possible to write your own binary search code on your dictionary, I suspect you'd have an easier time with a different data structure. If you used nested lists (with a as the index to the outer list, and i as the index to an inner list), you could use the bisect module to search the inner list efficiently.
I have a dictionary, T, with keys in the form k,i with an associated value that is a real number (float). Let's suppose I choose a particular key a,b from the dictionary T with corresponding value V1—what's the most efficient way to find the closest value to V1 for a key that has the form a+1,i, where i is an integer that ranges from 0 to n? (k,a, and b are also integers.) To add one condition on the values of items in T, as i increases in the key, the value associated to T[a+1,i] is strictly increasing (i.e. T[a+1,i+1] > T[a+1,i]).
I was planning to simply run a while loop that starts from i = 0 and compares the valueT[a+1,i] to V1. To be more clear, the loop would simply stop at the point at which np.abs(T[a+1,i] - V1) < np.abs(T[a+1,i+1] - V1), as I would know the item associated to T[a+1,i] is the closest to T[a,b] = V1. But given the strictly increasing condition I have imposed, is there a more efficient method than running a while loop that iterates over the dictionary elements? i will go from 0 to n where n could be an integer in the millions. Also, this process would be repeated frequently so efficiency is key.