I'd like to represent a set of integer ranges using Python where the set could be modified dynamically and tested for inclusion. Specifically I want to apply this to address ranges or line numbers in a file.
I could define the range of addresses I cared about to include:
200 - 400
450 - 470
700 - 900
Then I want to be able to add a potentially overlapping range to the set such that when I add 460 - 490 the set becomes:
200 - 400
450 - 490
700 - 900
But then be able to delete from the set where I could exclude the range 300 - 350 and the set becomes:
200 - 300
350 - 400
450 - 490
700 - 900
Finally, I want to be able to iterate over all integers included in the set, or test whether the set contains a particular value.
I'm wondering what the best way to do this is (particularly if there's something built into Python).
range(200, 400)andlst[200:400]and so on, which includes 200 but not 400. But the point is that you have to know which of the four possibilities you want before implementing it. - abarnert