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

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


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


Good 'ol itertools.groupby to the rescue:

from itertools import groupbydef contiguous(seq):    return sum(1 for k,g in groupby(seq, lambda x: x is not None) if k) == 1

gives

>>> contiguous([1,2,3,None,None])True>>> contiguous([None, 1,2,3,None])True>>> contiguous([None, None, 1,2,3])True>>> contiguous([None, 1, None, 2,3])False>>> contiguous([None, None, 1, None, 2,3])False>>> contiguous([None, 1, None, 2, None, 3])False>>> contiguous([1, 2, None, 3, None, None])False

[edit]

Since there seems to be some discussion in the comments, I'll explain why I like this approach better than some of the others.

We're trying to find out whether there is one contiguous group of non-None objects, and

sum(1 for k,g in groupby(seq, lambda x: x is not None) if k)

counts the number of contiguous non-None objects, using the function in the stdlib which is designed for making collecting contiguous groups. As soon as we see groupby, we think "contiguous groups", and vice-versa. In that sense, it's self-documenting. This is basically the definition of my goal.

IMHO the only weakness is that it doesn't short-circuit, and that could be fixed, but after thinking about it some I still prefer this as it uses a primitive I like -- "count the number of contiguous non-None groups" -- which I prefer to simply "tell me whether or not there is more than one contiguous non-None group as soon as you can".

Many of the approaches to implement the last one rely on clever observations about the problem, like "if there's only one contiguous group of not-None objects, then if we scan until we find the first not-None object, and then scan through objects until we find the first non-None group if one exists, then whether anything's left is None gives us our answer." (Or something like that, which is part of my issue: I have to think about it.) To me that feels like using "implementation details" about the problem to solve it, and focuses on properties of the problem we can use to solve it, rather than simply specifying the problem to Python and letting Python do the work.

I'm a bear of very little brain, as the saying has it, and I like to avoid having to be clever, as in my experience it's a route littered with FAIL.

As always, everyone's mileage may vary, of course, and probably in proportion to their cleverness.


You could use something like itertools.groupby:

from itertools import groupbydef are_continuous(items):    saw_group = False    for group, values in groupby(items, lambda i: i is not None):        if group:            if saw_group:                return False            else:                saw_group = True    return True

This will iterate only until it sees a group twice. I'm not sure if you consider [None, None], so tweak it to your needs.