可变增量滑动窗口 - Python

发布于 2024-11-27 13:12:53 字数 659 浏览 3 评论 0原文

我正在尝试使用Python中的滑动窗口函数来比较很长的值列表。我找到的滑动窗口函数的代码如下:

from itertools import islice

idlist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list = []

def window(seq, n=2):
    "Returns a sliding window (of width n) over data from the iterable"
    "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
    it = iter(seq)
    result = tuple(islice(it, n))
    if len(result) == n:
        yield result    
    for elem in it:
        result = result[1:] + (elem,)
        yield result

for i in window(idlist, n=2):
    list.append(i)

print list

我的问题是,如何修改此代码,以便将窗口的增量(生成每个元组后移动的量)从 1 更改为更大的整数,说5还是50?我知道如何更改窗口的大小,但不知道增量。 谢谢!

I am trying to use the sliding window function in Python to compare a very long list of values. The code I have found for the sliding window function is below:

from itertools import islice

idlist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list = []

def window(seq, n=2):
    "Returns a sliding window (of width n) over data from the iterable"
    "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
    it = iter(seq)
    result = tuple(islice(it, n))
    if len(result) == n:
        yield result    
    for elem in it:
        result = result[1:] + (elem,)
        yield result

for i in window(idlist, n=2):
    list.append(i)

print list

My question is, how would I modify this code so I could change the increment of the window (the amount it moves after each tuple is generated) from 1 to a much greater integer, say 5 or 50? I know how to change the size of the window, but not the increment.
Thanks!

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

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

发布评论

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

评论(3

逆流 2024-12-04 13:12:53

您不必更改增量,您可以获取每个第 n 个元素:

# taking every 3rd element moves the start by 3
print list(islice(window(idlist, n=2),None,None,3))

未完全优化,但很简单。

You don't have to change the increment, you can take every n'th element:

# taking every 3rd element moves the start by 3
print list(islice(window(idlist, n=2),None,None,3))

Not fully optimized, but simple.

女皇必胜 2024-12-04 13:12:53

提示:next 函数可用于从迭代器获取下一个元素。您需要在每次迭代中获取并附加多个元素(我认为这就是困难;您肯定知道如何将窗口的另一端向前移动不同的量:))。

Hint: the next function can be used to obtain the next element from the iterator. You need to obtain and append multiple elements per iteration (I assume that's the difficulty; surely you see how to move the other end of the window forward a different amount :) ).

公布 2024-12-04 13:12:53

也许这可以解决问题

L=[1,2,3,4,5]

def window(L, n=2, jump=1):
    lenght = len(L)
    assert n <= lenght
    for i in range(0,lenght-n+1,jump):
        yield tuple(L[i:i+n])

A=[]
for i in window(L, n=3, jump=1):
    A.append(i)

print A

maybe this solve the problem

L=[1,2,3,4,5]

def window(L, n=2, jump=1):
    lenght = len(L)
    assert n <= lenght
    for i in range(0,lenght-n+1,jump):
        yield tuple(L[i:i+n])

A=[]
for i in window(L, n=3, jump=1):
    A.append(i)

print A
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文