Python相当于Ruby的#each_cons?

问题描述 投票:14回答:8

有没有Pythonic等同于Ruby的#each_cons

在Ruby中你可以这样做:

array = [1,2,3,4]
array.each_cons(2).to_a
=> [[1,2],[2,3],[3,4]]
python ruby enumerable equivalent
8个回答
11
投票

对于这样的事情,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]]

16
投票

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

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

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

4
投票

快速单行:

a = [1, 2, 3, 4]

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

4
投票

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

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


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)的元组(即“窗口大小”),而不是我们想要迭代的序列。


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

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]]
© www.soinside.com 2019 - 2024. All rights reserved.