找到序列中长度为n的所有连续子序列

时间:2011-07-12 20:38:25

标签: python list

我想在序列中找到长度为n的所有连续子序列。

E.g。说n是3,序列是:

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

我想要一个可以产生输出的函数:

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

提前致谢!

4 个答案:

答案 0 :(得分:16)

>>> 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)]

如果您希望结果是列表而不是元组列表,请使用map(list, zip(...))

答案 1 :(得分:13)

>>> 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]]

答案 2 :(得分:1)

def subseqs(seq, length):
    for i in xrange(len(seq) - length + 1):
        yield seq[i:i+length]

像这样使用:

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

当然它也适用于列表:

>>> 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]]

答案 3 :(得分:1)

以下可能适合您:

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]]

或简单地说,获取名为' xs'的列表的所有序列:

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

获取名为' xs'的列表的序列。这只是从长度n:

[xs[i:j+1] for i, _ in enumerate(xs) for j, _ in enumerate(xs) if len(xs[i:j+1]) == n]