灵活的滑动窗口(Python)

编程入门 行业动态 更新时间:2024-10-20 05:44:38
本文介绍了灵活的滑动窗口(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题描述:我有兴趣查看文本窗口中的术语,例如,左边3个单词,右边3个单词。基本情况具有w-3 w-2 w-1项w + 1 w + 2 w + 3的形式。我想在我的文本上实现一个滑动窗口,我可以用它来记录每个术语的上下文单词。因此,每个单词都被视为一个术语,但是当窗口移动时,它会变成上下文单词等。但是,当该术语是第一个单词时,左边没有上下文单词(t w + 1) w + 2 w + 3),当它是第二个单词时,左边只有一个上下文单词,依此类推。因此,我对实现这个灵活的滑动窗口(在Python中)的任何提示感兴趣,而无需编写和单独指定每种可能的情况。

Problem description: I'm interested in looking at terms in the text window of, say, 3 words to the left and 3 to the right. The base case has the form of w-3 w-2 w-1 term w+1 w+2 w+3. I want to implement a sliding window over my text with which I will be able to record the context words of each term. So, every word is once treated as a term, but when the window moves, it becomes a context word, etc. However, when the term is the 1st word in line, there are no context words on the left (t w+1 w+2 w+3), when it's the 2nd word in line, there's only one context word on the left, and so on. So, I am interested in any hints for implementing this flexible sliding window (in Python) without writing and specifying separately each possible situation.

回顾:

输入示例:

[w1,w2,w3, w4,w5,w6,w7,w8,w9,w10]

["w1", "w2", "w3", "w4", "w5", "w6", "w7", "w8", "w9", "w10"]

输出 :

t1 w2 w3 w4

t1 w2 w3 w4

w1 t2 w3 w4 w5

w1 t2 w3 w4 w5

w1 w2 t3 w4 w5 w6

w1 w2 t3 w4 w5 w6

w1 w2 w3 t4 w5 w6 w7

w1 w2 w3 t4 w5 w6 w7

__ w2 w3 w4 t5 w6 w7 w8

__ w2 w3 w4 t5 w6 w7 w8

__ __等。

我目前的计划是为输出中的每一行提供一个单独的条件。

My current plan is to implement this with a separate condition for each line in the output.

推荐答案

如果你想要一个 n 字的滑动窗口,请使用一个最大长度的双端队列 n 实现一个缓冲区。

If you want a sliding window of n words, use a double-ended queue with maximum length n to implement a buffer.

这应该说明这个概念:

mystr = "StackOverflow" from collections import deque window = deque(maxlen=5) for char in mystr: window.append(char) print ( ''.join(list(window)) )

输出:

S St Sta Stac Stack tackO ackOv ckOve kOver Overf verfl erflo rflow

更多推荐

灵活的滑动窗口(Python)

本文发布于:2023-11-28 21:03:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643942.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:灵活   窗口   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!