Python 相当于 Ruby 的 #each_cons?

发布于 2024-11-05 02:50:56 字数 174 浏览 1 评论 0原文

是否有与 Ruby 的 #each_cons 等效的 Pythonic?

在 Ruby 中你可以这样做:

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

Is there a Pythonic equivalent to Ruby's #each_cons?

In Ruby you can do this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦毁影碎の 2024-11-12 02:50:57

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

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

Python can surely do this. If you don't want to do it so eagerly, use itertool's islice and izip. Also, its important to remember that normal slices will create a copy so if memory usage is important you should also consider the itertool equivalents.

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

九公里浅绿 2024-11-12 02:50:57

更新:不要介意我下面的答案,只需使用 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 参数上使用的元组解包应用于大小为 nitertools.tee(xs, n) 的结果(即“窗口大小”),而不是我们想要迭代的序列。

UPDATE: Nevermind my answer below, just use toolz.itertoolz.sliding_window() -- it will do the right thing.


For a truly lazy implementation that preserves the behavior of Ruby's each_cons when the sequence/generator has insufficient length:

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

Examples:

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

Note that the tuple unpacking used on the arguments for izip is applied to a tuple of size n resulting of itertools.tee(xs, n) (that is, the "window size"), and not the sequence we want to iterate.

紅太極 2024-11-12 02:50:57

接近@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]]

Close to @Blender's solution but with a fix:

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

Or

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]]
高速公鹿 2024-11-12 02:50:57

Elias 代码的 Python 3 版本

from itertools import islice, tee

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

$ ipython

...    

In [2]: a_list = [1, 2, 3, 4, 5]

In [3]: list(each_cons(a_list, 2))
Out[3]: [(1, 2), (2, 3), (3, 4), (4, 5)]

In [4]: list(each_cons(a_list, 3))
Out[4]: [(1, 2, 3), (2, 3, 4), (3, 4, 5)]

In [5]: list(each_cons(a_list, 5))
Out[5]: [(1, 2, 3, 4, 5)]

In [6]: list(each_cons(a_list, 6))
Out[6]: []

Python 3 version of Elias's code

from itertools import islice, tee

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

$ ipython

...    

In [2]: a_list = [1, 2, 3, 4, 5]

In [3]: list(each_cons(a_list, 2))
Out[3]: [(1, 2), (2, 3), (3, 4), (4, 5)]

In [4]: list(each_cons(a_list, 3))
Out[4]: [(1, 2, 3), (2, 3, 4), (3, 4, 5)]

In [5]: list(each_cons(a_list, 5))
Out[5]: [(1, 2, 3, 4, 5)]

In [6]: list(each_cons(a_list, 6))
Out[6]: []
挖鼻大婶 2024-11-12 02:50:57

这是使用collections.deque 的实现。这也支持任意生成器

from collections import deque

def each_cons(it, n):
    # convert it to an iterator
    it = iter(it)

    # insert first n items to a list first
    deq = deque()
    for _ in range(n):
        try:
            deq.append(next(it))
        except StopIteration:
            for _ in range(n - len(deq)):
                deq.append(None)
            yield tuple(deq)
            return

    yield tuple(deq)

    # main loop
    while True:
        try:
            val = next(it)
        except StopIteration:
            return
        deq.popleft()
        deq.append(val)
        yield tuple(deq)

用法:

list(each_cons([1,2,3,4], 2))
# => [(1, 2), (2, 3), (3, 4)]

# This supports generators
list(each_cons(range(5), 2))
# => [(0, 1), (1, 2), (2, 3), (3, 4)]

list(each_cons([1,2,3,4], 10))
# => [(1, 2, 3, 4, None, None, None, None, None, None)]

Here's an implementation using collections.deque. This supports arbitrary generators as well

from collections import deque

def each_cons(it, n):
    # convert it to an iterator
    it = iter(it)

    # insert first n items to a list first
    deq = deque()
    for _ in range(n):
        try:
            deq.append(next(it))
        except StopIteration:
            for _ in range(n - len(deq)):
                deq.append(None)
            yield tuple(deq)
            return

    yield tuple(deq)

    # main loop
    while True:
        try:
            val = next(it)
        except StopIteration:
            return
        deq.popleft()
        deq.append(val)
        yield tuple(deq)

Usage:

list(each_cons([1,2,3,4], 2))
# => [(1, 2), (2, 3), (3, 4)]

# This supports generators
list(each_cons(range(5), 2))
# => [(0, 1), (1, 2), (2, 3), (3, 4)]

list(each_cons([1,2,3,4], 10))
# => [(1, 2, 3, 4, None, None, None, None, None, None)]
空心空情空意 2024-11-12 02:50:56

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

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

I don't think there is one, I looked through the built-in module itertools, which is where I would expect it to be. You can simply create one though:

def each_cons(xs, n):
    return [xs[i:i+n] for i in range(len(xs)-n+1)]
机场等船 2024-11-12 02:50:56

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

For such things, itertools is the module you should be looking at:

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)

Then:

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

For an even more general solution, consider this:

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

This allows arbitrary lengths of subsequences and arbitrary overlapping. Usage:

>> 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]]
甜尕妞 2024-11-12 02:50:56

我的列表解决方案(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)))

My solution for lists (Python2):

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

Edit: With Python 3 itertools.izip is no longer, so you use plain zip:

def each_cons(xs, n):
    return zip(*(xs[i:] for i in range(n)))
粉红×色少女 2024-11-12 02:50:56

快速一句:

a = [1, 2, 3, 4]

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

A quick one-liner:

a = [1, 2, 3, 4]

out = [a[i:i + 2] for i in range(len(a) - 1)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文