简体   繁体   English

计算在 False 布尔值之间组合在一起的 True 布尔值的数量并在列表中返回

[英]Count the number of True booleans grouped together between False booleans and return in list

I have a list of booleans:我有一个布尔值列表:

[False, True, True, True, False, False, True, True, True, True, True, False, True, True, True, True, True, True, True, False]

I am looking for a way to count the number of True values grouped together, separated by False values.我正在寻找一种方法来计算组合在一起的 True 值的数量,由 False 值分隔。 For the above example, I would be looking for an output of [3, 5, 7] .对于上面的示例,我将寻找[3, 5, 7]的 output 。

I have tried using groupby, and various counting methods but all I have been able to get so far is the total number of True values or the number of groups of True there are.我尝试过使用 groupby 和各种计数方法,但到目前为止我所能得到的只是 True 值的总数或 True 组的数量。 My actual data will be a much longer list of booleans, is there a way to efficiently get this type of output?我的实际数据将是更长的布尔值列表,有没有办法有效地获得这种类型的 output?

Thank you!谢谢!

from itertools import groupby

bools = [False, True, True, True, False, False, True, True, True, True, True, False, True, True, True, True, True, True, True, False]

print([len(list(group)) for key, group in groupby(bools) if key])

Output: Output:

[3, 5, 7]
>>> 
list = [False, True, True, True, False, False, True, True, True, True, True, 
False, True, True, True, True, True, True,
        True, False]
groupList = []
flag = False
for i in list:
    if i:
        if flag is False:
            groupList.append(1)
            flag = True
        else:
            groupList[-1]+=1
    else:
        flag=False

print(groupList)

You can use groupby ;您可以使用groupby you just need to process the result.你只需要处理结果。

>>> x = [False, True, True, True, False, False, True, True, True, True, True, False, True, True, True, True, True, True, True, False]
>>> [sum(1 for _ in sg) for v, sg in groupby(x) if v]
[3, 5, 7]

The sequence produced by groupby consists of tuples of the key value and the subgroup corresponding to the key value. groupby产生的序列由键值的元组和键值对应的子组组成。 In this case, each list value is the key, so you get tuples like (False, _grouper(...)) .在这种情况下,每个列表值都是键,所以你会得到像(False, _grouper(...))这样的元组。 _grouper is an implementation detail of groupby ; _groupergroupby的实现细节; all you need to worry about is that it is an iterable of like values in the original list.您只需要担心它是原始列表中类似值的可迭代。

Once you have the groupby instance, you can unpack each tuple, test if the first element is True , then compute the length of the corresponding _grouper .一旦有了groupby实例,就可以解包每个元组,测试第一个元素是否为True ,然后计算相应_grouper的长度。 You can't use len , because an iterable isn't necessarily finite or pre-computed, so you use sum to add up a sequence of 1s for each element in the iterable.您不能使用len ,因为可迭代对象不一定是有限的或预先计算的,因此您可以使用sum为可迭代对象中的每个元素添加一个 1 序列。 (Alternatively, you could use len(list(sg)) .) (或者,您可以使用len(list(sg)) 。)

is this what you mean你是这个意思吗

booleans=[False, True, True, True, False, False, True, False, True, True, True, False, 
True, True, True, True, True, True, True, False]
counter=0
for i in range(1, len(booleans)-1):
    if(booleans[i-1]==booleans[i+1] and booleans[i+1]==False):
        counter=counter+1
print(counter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM