Python相当于Ruby的#each_cons?

时间:2011-05-04 04:06:28

标签: python ruby enumerable equivalent

Pythonic是否等同于Ruby的#each_cons

在Ruby中你可以这样做:

array = [1,2,3,4]
array.each_cons(2).to_a
=> [[1,2],[2,3],[3,4]]

8 个答案:

答案 0 :(得分:16)

我不认为有一个,我查看了内置模块itertools,这是我期望的。你可以简单地创建一个:

def each_cons(x, size):
    return [x[i:i+size] for i in range(len(x)-size+1)]

答案 1 :(得分:10)

对于这些事情,itertools是您应该关注的模块:

from itertools import tee, izip

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

然后:

>>> list(pairwise([1, 2, 3, 4]))
[(1, 2), (2, 3), (3, 4)]

对于更通用的解决方案,请考虑以下事项:

def split_subsequences(iterable, length=2, overlap=0):
    it = iter(iterable)
    results = list(itertools.islice(it, length))
    while len(results) == length:
        yield results
        results = results[length - overlap:]
        results.extend(itertools.islice(it, length - overlap))
    if results:
        yield results

这允许任意长度的子序列和任意重叠。用法:

>> list(split_subsequences([1, 2, 3, 4], length=2))
[[1, 2], [3, 4]]
>> list(split_subsequences([1, 2, 3, 4], length=2, overlap=1))
[[1, 2], [2, 3], [3, 4], [4]]

答案 2 :(得分:5)

我的列表解决方案(Python2):

import itertools
def each_cons(xs, n):
    return itertools.izip(*(xs[i:] for i in xrange(n)))

修改:使用Python 3 itertools.izip已不再使用,因此您使用普通zip

def each_cons(xs, n):
    return zip(*(xs[i:] for i in range(n)))

答案 3 :(得分:4)

快速单行:

a = [1, 2, 3, 4]

out = [a[i:i + 2] for i in range(len(a) - 1)]

答案 4 :(得分:4)

Python肯定可以做到这一点。如果您不想这么急切地使用,请使用itertool的islice和izip。此外,重要的是要记住正常切片将创建一个副本,因此如果内存使用很重要,您还应该考虑itertool等价物。

each_cons = lambda l: zip(l[:-1], l[1:])

答案 5 :(得分:3)

更新:不要在下面找回我的答案,只需使用toolz.itertoolz.sliding_window() - 它会做正确的事。


对于一个真正的惰性实现,当序列/生成器长度不足时保留Ruby each_cons的行为:

import itertools
def each_cons(sequence, n):
    return itertools.izip(*(itertools.islice(g, i, None)
                          for i, g in
                          enumerate(itertools.tee(sequence, n))))

示例:

>>> print(list(each_cons(xrange(5), 2)))
[(0, 1), (1, 2), (2, 3), (3, 4)]
>>> print(list(each_cons(xrange(5), 5)))
[(0, 1, 2, 3, 4)]
>>> print(list(each_cons(xrange(5), 6)))
[]
>>> print(list(each_cons((a for a in xrange(5)), 2)))
[(0, 1), (1, 2), (2, 3), (3, 4)]

请注意,izip参数上使用的元组解包应用于n的大小为itertools.tee(xs, n)的元组(即“窗口大小”),而不是我们想要的序列迭代。

答案 6 :(得分:1)

与elias的代码相同,但适用于python 2和3:

try:
    from itertools import izip  # python 2
except ImportError:
    from builtins import zip as izip  # python 3

from itertools import islice, tee

def each_cons(sequence, n):
    return izip(
        *(
            islice(g, i, None)
            for i, g in
            enumerate(tee(sequence, n))
        )
    )

答案 7 :(得分:1)

接近@Blender的解决方案,但已解决:

a = [1, 2, 3, 4]
n = 2
out = [a[i:i + n] for i in range(len(a) - n + 1)]
# => [[1, 2], [2, 3], [3, 4]]

a = [1, 2, 3, 4]
n = 3
out = [a[i:i + n] for i in range(len(a) - n + 1)]
# => [[1, 2, 3], [2, 3, 4]]