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

后端 未结 11 1224
渐次进展
渐次进展 2021-02-01 01:35

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

11条回答
  •  庸人自扰
    2021-02-01 02:03

    One liner:

    contiguous = lambda l: ' ' not in ''.join('x '[x is None] for x in l).strip()
    

    The real work is done by the strip function. If there are spaces in a stripped string, then they're not leading/trailing. The rest of the function converts the list to a string, which has a space for each None.

提交回复
热议问题