0

I would like to identify the sequences of continuous numbers in a vector, given:

x = [1,1,1,1,2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,2,2]

The numpy.unique returns --> [1,2,3] without including the repeated [1,2] at the end of the sequence x.

This what I have tried so far:

import numpy as np

y = np.unique(x)

The expected returns are:

[1,2,3,1,2]

The repeated [1,2] should be kept at the end of the returns just like clustering.

Fuad Numan
  • 74
  • 5

2 Answers2

0

Here's a simple way to approach this:

seen = None
y = []

for i in x:
    if i != seen:
        y.append(i)
        seen = i

print(y)

[1, 2, 3, 1, 2]
gold_cy
  • 13,648
  • 3
  • 23
  • 45
0

You can just do the following

y = [x[0]] + [y for (x, y) in zip(x, x[1:]) if x != y]
xashru
  • 3,400
  • 2
  • 17
  • 30