5

I want to find all consecutive sub-sequences of length n in a sequence.

E.g. say n was 3 and the sequence was:

[0,1,7,3,4,5,10]

I want a function that would produce as output:

[[0,1,7],[1,7,3],[7,3,4],[3,4,5],[4,5,10]]

Thanks in advance!

WillJones
  • 907
  • 1
  • 9
  • 19
  • 3
    What have you tried? Seems to be quite straightforward actually. Iterate and take a subsequence of size n at each position. – Felix Kling Jul 12 '11 at 20:43

4 Answers4

19
>>> x = [0,1,7,3,4,5,10]
>>> n = 3
>>> zip(*(x[i:] for i in range(n)))
[(0, 1, 7), (1, 7, 3), (7, 3, 4), (3, 4, 5), (4, 5, 10)]

If you want the result to be a list of lists instead of list of tuples, use map(list, zip(...)).

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
14
>>> x = [0,1,7,3,4,5,10]
>>> [x[n:n+3] for n in range(len(x)-2)]
[[0, 1, 7], [1, 7, 3], [7, 3, 4], [3, 4, 5], [4, 5, 10]]
JBernardo
  • 32,262
  • 10
  • 90
  • 115
  • 6
    To make this general for any subsequence size: `[x[i:i+n] for i in range(len(x)-n+1)]` where `n` is the desired length of the subsequence. – Steven Rumbalski Jul 12 '11 at 20:57
2
def subseqs(seq, length):
    for i in xrange(len(seq) - length + 1):
        yield seq[i:i+length]

Use it ike this:

>>> for each in subseqs("hello", 3):
...     print each
...
hel
ell
llo

Of course it works also with lists:

>>> list(subseqs([1, 2, 3, 4, 5, 6, 7, 8], 3))
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8]]
pillmuncher
  • 10,094
  • 2
  • 35
  • 33
1

The following might probably be suit for you:

def subseqs(xs, n):
  all_seqs = (xs[i:j+1] for i, _ in enumerate(xs) for j, _ in enumerate(xs))
  return filter(lambda seq: len(seq) == n, all_seqs)

>>> xs = [1, 2, 3, 4, 5, 6] # can be also range(1, 7) or list(range(1, 7)) 
>>> list(subseqs(xs, 3))
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]

Or simply, for getting all of the sequences of a list named 'xs':

[xs[i:j+1] for i, _ in enumerate(xs) for j, _ in enumerate(xs)]

For getting the sequences of a list named 'xs' that are only from length n:

[xs[i:j+1] for i, _ in enumerate(xs) for j, _ in enumerate(xs) if len(xs[i:j+1]) == n]
Shaikovsky
  • 526
  • 5
  • 7