Suppose I have the following list of lists:
intervals = [[3, 5], [11,25], [39,40], [45, 48]]
Beginning with the start number, 0 for this example, all the way to the end number, 50 for this example, my goal is to create a list of lists with ranges of every number not in the current list of list. For example, the list I want to produce would like like this:
nonIntervals = [[0, 3], [5, 11], [25, 39], [40, 45], [48, 50]]
I have already been working on an implementation to do just this but it has issues with certain test cases:
for index in range(len(intervals)):
nonIntervals.append([start, intervals[index][0]])
start = intervals[index][1]
if not(nonIntervals[index][1] == end):
nonIntervals.append([intervals[index][1], end])
With this example I run into some problems where it produces intervals I do not need. For example, if I used the following interval:
intervals = [[0, 5], [11,25], [39,40], [45, 48]]
I get this interval when running the loop: nonIntervals = [[0, 0], [5, 11], [25, 39], [40, 45], [48, 50]]
Does anyone know how I can avoid the nonIntervals list containing the [0,0] list? For some reason, it include the start of the array when I want to ignore it and begin with [5,11]. The same thing applies for when the end is at the end of the list.
For example: intervals = [[0, 5], [11,25], [39,40], [45, 50]]
When running my program it returns, nonIntervals = [[0, 0], [5, 11], [25, 39], [40, 45], [50, 50]]
I am expecting the last list in this list of lists to be [40, 45]. If anyone knows how to correct my loop or proper if-statements I need to implement I'd greatly appreciate it.
ifcondition before everynon-intervals.append()statement, and do not append if the items that you're gonna append are the same