Reputation: 8798
Maybe my title can be misleading, but let me clarify my question:
Here's a list:
chr1:100-200 100 100
chr1:350-500 150 250
chr1:780-880 100 350
chr1:900-950 50 400
So basically the first column is coordinate range, second column is the interval length (substraction between the two coordinates from the first column), third column is the accumulation for the interval length.
Now I have a number, say 120. What I need to do is: since 100 < 120 < 250 (to compare my object with the key), and the key should point to the value "chr1:350-500"; similarly, if my number is 360, since 350 < 360 < 400, the value should be :"chr1:900-950"
Hopefully I've made it clear. I guess I should use dictionary or hashtable to solve the problem, but here involves compare my object with keys; and I don't know how to do it.
Many thanks
Upvotes: 0
Views: 107
Reputation: 798746
Actually, no.
>>> ranges = ['chr1:100-200', 'chr1:350-500', 'chr1:780-880', 'chr1:900-950']
>>> accums = [100, 250, 350, 400]
>>> ranges[bisect.bisect_left(accums, 120)]
'chr1:350-500'
>>> ranges[bisect.bisect_left(accums, 360)]
'chr1:900-950'
Upvotes: 2