问题描述
我正在寻找一个由窗口大小为 N 的单词组成的字符串的滑动窗口拆分器.
输入:我喜欢食物,我喜欢喝",窗口大小 3
输出:[我爱食物"、爱食物和"、食物和我"、我喜欢".....]
窗口滑动的所有建议都是围绕字符串的序列,没有术语.有什么开箱即用的吗?
您可以使用具有不同偏移量的迭代器并将它们全部压缩.
>>>arr = "我喜欢食物.等等".split()>>>its = [iter(arr), iter(arr[1:]), iter(arr[2:])] #构造更长windowss的模式>>>拉链(*其)[('I', 'love', 'food.'), ('love', 'food.', 'blah'), ('food.', 'blah', 'blah')]您可能想要使用 izip
如果你的句子很长,或者可能是简单的旧循环(就像在另一个答案中一样).
I'm looking for a sliding window splitter of string composed with words with window size N.
Input: "I love food and I like drink" , window size 3
Output: [ "I love food", "love food and", "food and I", "and I like" .....]
All the suggestions of window sliding is around sequence of string, no terms. Is there something out of the box?
You can use iterator with different offsets and zip all of them.
>>> arr = "I love food. blah blah".split()
>>> its = [iter(arr), iter(arr[1:]), iter(arr[2:])] #Construct the pattern for longer windowss
>>> zip(*its)
[('I', 'love', 'food.'), ('love', 'food.', 'blah'), ('food.', 'blah', 'blah')]
You might want to use izip
if you have long sentences, or may be plain old loops (like in the other answer).
这篇关于句子字符串上的Python滑动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!