Python Sliding Window on sentence string

后端 未结 3 827
渐次进展
渐次进展 2021-01-15 10:53

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

3条回答
  •  忘掉有多难
    2021-01-15 11:34

    def token_sliding_window(str, size):
        tokens = str.split(' ')
        for i in range(len(tokens )- size + 1):
            yield tokens[i: i+size]
    

提交回复
热议问题