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?
You can convert the list to a set. A set cannot have duplicates. So if all the elements in the original list are identical, the set will have just one element. if len(set(input_list)) == 1: # input_list has all identical elements.
Solution 3: Using len() In this solution, we use the len() to check if a list is empty, this function returns the length of the argument passed. And given the length of an empty list is 0 it can be used to check if a list is empty in Python.
notnull is a pandas function that will examine one or multiple values to validate that they are not null. In Python, null values are reflected as NaN (not a number) or None to signify no data present. . notnull will return False if either NaN or None is detected. If these values are not present, it will return True.
“not in” operator − This operator is used to check whether an element is not present in the passed list or not. Returns true if the element is not present in the list otherwise returns false.
def contiguous(seq): seq = iter(seq) all(x is None for x in seq) # Burn through any Nones at the beginning any(x is None for x in seq) # and the first group return all(x is None for x in seq) # everthing else (if any) should be None.
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 each
example1:
seq = iter([None, 1, 2, 3, None]) # [None, 1, 2, 3, None] # next^ all(x is None for x in seq) # next^ any(x is None for x in seq) # next^ (off the end) return all(x is None for x in seq) # all returns True for the empty sequence
example2:
seq = iter([1, 2, None, 3, None, None]) # [1, 2, None, 3, None, None] # next^ all(x is None for x in seq) # next^ any(x is None for x in seq) # next^ return all(x is None for x in seq) # all returns False when 3 is encountered
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With