Python中的滑动窗口

Python中的滑动窗口,python,sliding-window,Python,Sliding Window,我在标记化文本文档的滑动窗口中使用了以下著名代码: def window(fseq, window_size): "Sliding window" it = iter(fseq) result = tuple(islice(it, 0, window_size, round(window_size/4))) if len(result) == window_size: yield result for elem in i

我在标记化文本文档的滑动窗口中使用了以下著名代码:

def window(fseq, window_size):
    "Sliding window"
    it = iter(fseq)
    result = tuple(islice(it, 0, window_size, round(window_size/4)))
    if len(result) == window_size:
        yield result
    for elem in it:
        result = result[1:] + (elem,)
        result_list = list(result)
        yield result_list
当我想在窗口大小小于6的情况下调用我的函数时,一切都正常,但当我增加它时,文本的开头被剪切

例如:

c=['A','B','C','D','E', 'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
print(list(window(c, 4)))
print(list(window(c, 8)))
输出:

[('A', 'B', 'C', 'D'), ['B', 'C', 'D', 'E'], ['C', 'D', 'E', 'F'], ['D', 'E', 'F', 'G'], ['E', 'F', 'G', 'H'], ['F', 'G', 'H', 'I'],...

[['C', 'E', 'G', 'I'], ['E', 'G', 'I', 'J'], ['G', 'I', 'J', 'K'], ['I', 'J', 'K', 'L'], ['J', 'K', 'L', 'M']...
怎么了?为什么在第一个输出中,第一个元素在圆括号中

我对
打印(列表(窗口(c,8))
的预期输出是:

[['A','B','C', 'D', 'E', 'F','G','H'], ['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], ['E', 'F', 'G', 'H', 'I', 'K', 'L', 'M']...

你的版本不正确。它在
islice()
函数中添加了第四个参数(步长),用于限制第一个切片的大小:

result = tuple(islice(it, 0, window_size, round(window_size/4)))
对于
4
5
圆形(窗口大小/4)
产生
1
,默认步长。但是对于较大的值,这会产生一个步长,保证第一个窗口中的值会被忽略,因此下一个测试,
len(result)==window\u size
保证为false

删除步长参数,您将再次获得第一个窗口。另见

第一个结果在“圆括号”中,因为它是一个元组。如果您想要列表,请在代码中使用
list()
而不是
tuple()

如果希望窗口以大于1的步长滑动,则不应更改初始窗口。您需要在迭代过程中从窗口中添加和删除
step
size元素。使用
while
循环更容易做到这一点:

def window_with_larger_step(fseq, window_size):
    """Sliding window

    The step size the window moves over increases with the size of the window.
    """
    it = iter(fseq)
    result = list(islice(it, 0, window_size))
    if len(result) == window_size:
        yield result
    step_size = max(1, int(round(window_size / 4)))  # no smaller than 1
    while True:
        new_elements = list(islice(it, step_size))
        if len(new_elements) < step_size:
            break
        result = result[step_size:] + list(islice(it, step_size))
        yield result

你的版本不正确。它在
islice()
函数中添加了第四个参数(步长),用于限制第一个切片的大小:

result = tuple(islice(it, 0, window_size, round(window_size/4)))
对于
4
5
圆形(窗口大小/4)
产生
1
,默认步长。但是对于较大的值,这会产生一个步长,保证第一个窗口中的值会被忽略,因此下一个测试,
len(result)==window\u size
保证为false

删除步长参数,您将再次获得第一个窗口。另见

第一个结果在“圆括号”中,因为它是一个元组。如果您想要列表,请在代码中使用
list()
而不是
tuple()

如果希望窗口以大于1的步长滑动,则不应更改初始窗口。您需要在迭代过程中从窗口中添加和删除
step
size元素。使用
while
循环更容易做到这一点:

def window_with_larger_step(fseq, window_size):
    """Sliding window

    The step size the window moves over increases with the size of the window.
    """
    it = iter(fseq)
    result = list(islice(it, 0, window_size))
    if len(result) == window_size:
        yield result
    step_size = max(1, int(round(window_size / 4)))  # no smaller than 1
    while True:
        new_elements = list(islice(it, step_size))
        if len(new_elements) < step_size:
            break
        result = result[step_size:] + list(islice(it, step_size))
        yield result

你从哪里得到这个代码的?根据islise文档,它与我刚才添加的起点和te步骤上的解决方案有很大不同,但为什么?你不需要一个步长。将其设置为1。但我确实需要一个特定的步长,而不仅仅是1。它应该取决于窗口大小,因为您没有生成“著名的滑动窗口”功能。首先解释您的用例和预期输出。您的问题目前还不清楚。您从哪里获得此代码的?根据islise文档,它与我刚才添加的起点和te步骤上的解决方案有很大不同,但为什么?你不需要一个步长。将其设置为1。但我确实需要一个特定的步长,而不仅仅是1。它应该取决于窗口大小,因为您没有生成“著名的滑动窗口”功能。首先解释您的用例和预期输出。你的问题目前还很不清楚。