-1

The objective of this function is to remove any three consecutive True values that come immediately after a False.

Below is a code I had written but it does not delete the first True when the True is unique. How do you please do it?

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]
Machavity
  • 30,841
  • 27
  • 92
  • 100
SLV
  • 23
  • 5

2 Answers2

0

You could do it using a replace on a string version of the list of boolean values:

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]
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • Thank you very musch for your answer :) But I would like to remove the first three occurrences for the True which are after. With the list : alist = [True,True,True,False,True,True,False,True, True, True, True, True]. The result should be : [False,False,True, True]. – SLV Feb 20 '21 at 20:46
  • 1
    *three occurrences for the True which are after ...* After what ? – Alain T. Feb 20 '21 at 20:48
  • Remove any three consecutive True values that come immediately after a False – SLV Feb 21 '21 at 02:19
0

It is not a good to remove elements from the list while iterating over it using a for loop. The issue has to do with the way Python (and many other languages) iterates over elements in a list.

My suggestion is that you use a temporary list to add only the elements that fits your criteria in the for loop. After you exit the for loop just assign the temporary list to the original list variable.

Now let's focus on the criteria: Remove any three consecutive True values that come immediately after a False.

To illustrate the problem, i've created this state-machine diagram, I hope you are familiar with it.

enter image description here

So let write the code for that state machine:

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]
Ouss
  • 2,912
  • 2
  • 25
  • 45
  • Thank you very musch for your answer :) But I would like to remove the first three occurrences for the True which are after. With the list : alist = [True,True,True,False,True,True,False,True, True, True, True, True]. The result should be : [False,False,True, True]. – SLV Feb 20 '21 at 20:48
  • I fixed my answer. I hope it answers your question – Ouss Feb 21 '21 at 00:04