Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to determine whether not null list entries are 'continuous'

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?

like image 348
Clay Wardell Avatar asked Feb 06 '13 04:02

Clay Wardell


People also ask

How do you check if an array has all the same values Python?

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.

How do you check if a list is null in Python?

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.

How do you check if something is not null 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.

How do you check if an item is not part of a list?

“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.


1 Answers

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 
like image 166
John La Rooy Avatar answered Sep 19 '22 06:09

John La Rooy