Answer the question
In order to leave comments, you need to log in
How to make it so that if there are 3 identical numbers in a row in the list, then they are deleted?
a = [1, 2, 3, 3, 3]
How to make it so that if the list contains at least 3 identical numbers in a row, then they are deleted? And you also need to record how many elements were deleted. It is desirable that the method be without using pip
Answer the question
In order to leave comments, you need to log in
Cycle. That's just a question, but if after deletion there are again 3 in a row, also delete?
For example,
Should it output a = [1, 1, 1] or a = []?
If the first, then something like this ...
a = [1, 1, 2, 2, 2, 1]
our_list = [1, 2, 3, 3, 3]
#идём от конца списка к началу - так удаление повлияет только на уже обработанные элементы списка.
for i in range(len(our_list) - 3, 0, -1):
if our_list[i:i+3] == [ our_list[i] ] * 3: #сравниваем фрагмент списка с повтором элемента трижды
del our_list[i:i+3]
print(our_list)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question