Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Help: Quick way to test if value lies within a list of lists ofranges?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Paddy, post: 1845367"] Would this help? '''{'a': [[5, 12], [27, 89], [120, 137]], 'b': [[6, 23], [26, 84], [200, 222]], 'c': [[2, 22], [47, 60], [67, 122]]} ''' from pprint import pprint as pp name2ranges = { 'a': [[48,60], [27,89], [5,12], [120,137]], 'b': [[78,84], [26,79], [200,222], [6,23], [72,74]], 'c': [[67,122],[2,22], [47,60]] } for name, ranges in name2ranges.iteritems(): # sort ranges name2ranges[name].sort() # Coalesce overlapping ranges. # If the max of one range is >= the min of the next range then # the adjacent ranges are combined ln = len(ranges) newranges = [] i = 0 while i< ln: mn,mx = ranges[i] j = i+1 while j< ln and ranges[j][0] <=mx: mx = max(mx, ranges[j][1]) j += 1 newranges.append([mn,mx]) i = j name2ranges[name] = newranges def names4val(val): " find the names whose ranges intersect the value" from bisect import bisect_left names = [] for name,ranges in name2ranges.iteritems(): bs = bisect_left(ranges, [val,val]) if (bs and ranges[bs-1][0] <= val <= ranges[bs-1][1] ) or ( bs <len(ranges) and ranges[bs][0] <= val <= ranges[bs][1] ): names.append(name) names.sort() return names #### Cheers, Pad.[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Help: Quick way to test if value lies within a list of lists ofranges?
Top