Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除紧跟在False之后的任意三个连续真值_Python_List_Remove - Fatal编程技术网

Python 删除紧跟在False之后的任意三个连续真值

Python 删除紧跟在False之后的任意三个连续真值,python,list,remove,Python,List,Remove,此函数的目标是删除假后立即出现的任何三个连续真值 下面是我编写的一段代码,但当第一个True唯一时,它不会删除第一个True。 请你怎么做 def remove_first_two(alist, n): if alist == []: return [] else: count = 0 while count < n: for ele in alist: if ele =

此函数的目标是删除假后立即出现的任何三个连续真值

下面是我编写的一段代码,但当第一个True唯一时,它不会删除第一个True。 请你怎么做

def remove_first_two(alist, n):
    if alist == []:
        return []
    else:
        count = 0
        while count < n:
            for ele in alist:
                if ele == True:
                    alist.remove(ele)
                    count += 1
    return alist

   alist = [True,True,True,False,False,True,True,False,False,True, True, True, True, True, False, True]
   print(remove_first_two(alist,4))
   [False, False, False, False, True, True, True, False, True]```

The result should be : [False,False,False,False,True,True,False]
def先移除两个(列表,n):
如果alist=[]:
返回[]
其他:
计数=0
当计数
您可以使用布尔值列表的字符串版本进行替换:

alist = [True,True,True,False,False,True,True,False,False,True, True, True, True, True, False, True]

blist = [c == "T" for c in "".join("FT"[a] for a in alist).replace("FTTT","F") ]

print(blist)
# [True, True, True, False, False, True, True, False, False, True, True, False, True]

在使用for循环遍历列表时,从列表中删除元素不是一个好方法。这个问题与Python(和许多其他语言)迭代列表中元素的方式有关

我的建议是使用临时列表在for循环中只添加符合条件的元素。退出for循环后,只需将临时列表分配给原始列表变量

现在让我们关注标准:删除紧跟在a
False
之后的任何连续三个
True

为了说明这个问题,我创建了这个状态机图,希望您熟悉它

让我们为状态机编写代码:

alist = [True,True,True,False,False,True,True,False,False,True, True, True, True, True, False, True]
tmp=list()
count=0 # counter for consecutive values of True
for ele in alist: # iterate over the original list
    if ele is False:
        tmp.append(ele) # add ele to the temporary list
        count=0 # reset the counter of consecutive values of True
        continue # iterate
    # ele is True
    count+=1 # count consecutive values of True
    if count>3: # if this is the 4th (or bigger) value of True
       tmp.append(ele) # add to the temporary list
alist=tmp # assign the temporary list to the variable of the original list

print(alist)
[False, False, False, False, True, True, False]

非常感谢你的回答:)但我想删除真实世界的前三个事件,它们发生在后面。列表:alist=[真,真,真,假,真,真,假,真,真,真,真,真]。结果应该是:[假,假,真,真]。真的三次出现在。。。在什么之后?删除紧跟在False之后的任意三个连续真值谢谢你的回答:)但是我想删除在True之后的前三个真值。列表:alist=[真,真,真,假,真,真,假,真,真,真,真,真]。结果应该是:[假,假,真,真]。我希望它能回答你的问题