When working with data in Python, you often encounter operations that determine whether a number is in an interval. We could use if else to do this, but since we're using Python, we want to find out if there's an off-the-shelf wheel we can use. In fact, we can use the interval library to do what we need to do.
Basis of interval judgment
The most basic interval determination operation is to create a few intervals and then use in to determine if a number exists within the interval. The code is as follows:
from interval import Interval zoom_2_5 = Interval(2, 5) print(zoom_2_5) >> [2..5] print(2 in zoom_2_5) >> True print(6 in zoom_2_5) >> False
As we can see from the code above, we first use Interval to create a set, and then we want to compare the number of in can get the result. However, as we all know, our sets have separate intervals and closed intervals. The code above creates a set of intervals [2, 5], so what if we want to create a set such as (2, 5]?
See the code below:
zoom_o2_5 = Interval(2, 5, lower_closed=False) print(zoom_o2_5) >> (2..5] print(2 in zoom_o2_5) >> False zoom_o2_o5 = Interval(2, 5, closed=False) print(zoom_o2_o5) >> (2..5)
As you can see from the code above, when using Interval to create a collection, using the lower_closed parameter, we can set the lower limit of the collection interval to a non-closed interval, i.e., an open interval, so that when we compare whether or not 2 is in the interval, the return result is False. similarly, if you want the upper limit of the interval to be set to an open interval, set upper_closed to False, and if you want to create an open interval directly, then you can just set closed to False. Similarly, if you want the upper limit of the interval to be open, you can set upper _closed to False, and if you want to create an open interval directly, you can set closed to False.
Manipulation of Collections
Interval has three methods of operating on collection intervals, join overlaps adjacent_to , and the following demonstrates the use of these three methods below:
zoom_1_3 = Interval(1, 3) zoom_1_5 = Interval(1, 5) zoom_o3_5 = Interval(3, 5, lower_closed=False) # join Merge two consecutive sets of intervals print(zoom_1_3.join(zoom_1_5)) >> [1..5] # overlaps Determine if two intervals are duplicates print(zoom_1_3.overlaps(zoom_1_5)) >> True print(zoom_1_3.overlaps(zoom_o3_5)) >> False # adjacent_to Determine if intervals are more adjacent than repetitive print(zoom_1_3.adjacent_to(zoom_o3_5)) >> True
wrap-up
The interval library also provides the IntervalSet package, which provides operations on multiple Interval, due to the relationship between the length and the different scenarios in the actual application of the specific use of the different, here will not repeat, to more in-depth understanding of the use of Ipython into the interactive mode and then use the help() method to see the specific use of different methods! This library provides detailed instructions. This is the end of this article, I hope it helps you.
Above this Python numerical interval processing _ on the interval library of the quick start details is all I have shared with you, I hope to be able to give you a reference, but also hope that you support me more.