确定空列表条目是否为“连续"的Python方法 [英] Pythonic way to determine whether not null list entries are 'continuous'

查看:74
本文介绍了确定空列表条目是否为“连续"的Python方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种轻松确定列表中所有非无项目是否出现在单个连续切片中的方法.我将使用整数作为非无项目的示例.

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.

例如,列表[None, None, 1, 2, 3, None, None]满足我对连续整数条目的要求.相反,[1, 2, None, None, 3, None]连续的,因为整数之间没有None条目.

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.

连续:
[1, 2, 3, None, None]
[None, None, 1, 2, 3]
[None, 1, 2, 3, None]

Continuous:
[1, 2, 3, None, None]
[None, None, 1, 2, 3]
[None, 1, 2, 3, None]

不连续:
[None, 1, None, 2, None, 3]
[None, None, 1, None, 2, 3]
[1, 2, None, 3, None, None]

Not Continuous:
[None, 1, None, 2, None, 3]
[None, None, 1, None, 2, 3]
[1, 2, None, 3, None, None]

我的第一种方法是使用变量来跟踪我们是否遇到过None,以及我们是否曾经遇到过int-这最终导致了高度嵌套和很难遵循嵌入在for循环中的一系列if/else语句. (最丑陋的是,我承认我并没有在每种情况下都可以使用它.)

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?

推荐答案

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.

这里有几个例子.您可以使用next(seq)从迭代器获取下一项.我将在每个

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

示例1:

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

这篇关于确定空列表条目是否为“连续"的Python方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆