如何返回布尔值中最长连续出现的“ True”,并用False替换其他True?

时间:2019-05-25 05:22:56

标签: python numpy

我试图返回一个布尔值,该布尔值仅在原始布尔值中给出最长的“ True”出现,并将较短的“ True”块替换为“ False”。示例a = [True,True,False,True,True,True,False],我想返回[False,False,False,True,True,True,False]。

我尝试过more_itertools,它似乎有一些有趣的功能,但不确定如何为我的目的准确实现。

a=[True, True, False, True , True, True, False]

pred = lambda x: x in {True}

p=list(mit.run_length.encode(a))

>>>
Results in: (True,2),(False,1),(True,3),(False,1)

所以我最终想要自动获得的是(False,3),(True,3),(False,1)。有什么建议么? 谢谢您的帮助

2 个答案:

答案 0 :(得分:3)

使用more_itertools.run_length后,以下解决方案应该可以使用。

从本质上讲,逻辑是找到最长的True子序列的长度,以及该索引在result列表中的位置
然后计算该最长子序列之前和之后的总元素,然后相应地构建结果元组列表。

import more_itertools as mit

a=[True, True, False, True , True, True, False]

result = list(mit.run_length.encode(a))

#Find the length of longest subsequence of True, and the location if that index in result
max_true_count = -1
max_true_idx  = -1
for idx, (val, count) in enumerate(result):
    if val and max_true_count < count:
        max_true_count = count
        max_true_idx = idx

#Find total elements before and after the longest subsequence tuple
elems_before_idx = sum((idx[1] for idx in result[:max_true_idx]))
elems_after_idx = sum((idx[1] for idx in result[max_true_idx+1:]))

#Create the output list using the information
output = [(False, elems_before_idx), (True, max_true_count), (False, elems_after_idx)]
print(output)

输出将为

[(False, 3), (True, 3), (False, 1)]

答案 1 :(得分:1)

这是矢量化的-

def keep_longest_true(a):
    # Convert to array
    a = np.asarray(a)

    # Attach sentients on either sides w.r.t True
    b = np.r_[False,a,False]

    # Get indices of group shifts
    s = np.flatnonzero(b[:-1]!=b[1:])

    # Get group lengths and hence the max index group
    m = (s[1::2]-s[::2]).argmax()

    # Initialize array and assign only the largest True island as True.
    out = np.zeros_like(a)
    out[s[2*m]:s[2*m+1]] = 1
    return out

def island_info(a):
    '''' Get island tuple info
    '''

    # Attach sentients on either sides w.r.t array start and end
    b = np.r_[~a[0],a,~a[-1]]

    # Get group lengths and group start elements
    lens = np.diff(np.flatnonzero(b[:-1] != b[1:]))
    grpID = np.resize([a[0],~a[0]],len(lens))

    # zip those two info for final o/p
    return zip(grpID,lens)

样品运行-

In [221]: a
Out[221]: [True, True, False, True, True, True, False]

In [222]: keep_longest_true(a)
Out[222]: array([False, False, False,  True,  True,  True, False])

In [223]: island_info(keep_longest_true(a))
Out[223]: [(False, 3), (True, 3), (False, 1)]