Skip to content
Advertisement

How to replace equal consecutive numbers in a list to nan?

I’m trying to replace equal consecutive numbers in a list to nan. I having problems to replace all values when there is a odd number of equal consecutive numbers.

This is my code, very simple:

JavaScript

Another question, if I want to replace only those values that repeated more than 3 times, for example the numbers 1 or 2 that are repeated 5 and 4 times respectively.

This is what I want:

JavaScript

Advertisement

Answer

Use Counter to keep track of elements in list and use list comprehension with condition

JavaScript

Output:

JavaScript

Alternate solution

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement