I’m looking for a way to easily determine if all not None items in a list occur in a single continuous slice. I’ll use integers as examples of not None items.
For example, the list [None, None, 1, 2, 3, None, None] meets my requirements for continuous integer entries. By contrast, [1, 2, None, None, 3, None] is not continuous, because there are None entries between integers.
Some more examples to make this a clear as possible.
Continuous:
[1, 2, 3, None, None]
[None, None, 1, 2, 3]
[None, 1, 2, 3, None]
Not Continuous:
[None, 1, None, 2, None, 3]
[None, None, 1, None, 2, 3]
[1, 2, None, 3, None, None]
My first approach was to use variables to keep track of whether or not we had come across a None yet, and whether or not we had come across an int yet — this ends up with a highly nested and very difficult to follow series of if/else statements embedded in a for loop. (On top of the ugliness, I confess I haven’t gotten it to work in every case).
Anyone know an easier way to figure out if the not None items in a list occur in a single continuous slice?
Here are a couple of examples. You can use
next(seq)to get the next item from an iterator. I’ll put a mark pointing to the next item after eachexample1:
example2: