我有一个这样的价值 list ,
lst = [1, 2, 3, 4, 5, 6, 7, 8]
Desired Output:
window size = 3
1 # first element in the list
forward = [2, 3, 4]
backward = []
2 # second element in the list
forward = [3, 4, 5]
backward = [1]
3 # third element in the list
forward = [4, 5, 6]
backward = [1, 2]
4 # fourth element in the list
forward = [5, 6, 7]
backward = [1, 2, 3]
5 # fifth element in the list
forward = [6, 7, 8]
backward = [2, 3, 4]
6 # sixth element in the list
forward = [7, 8]
backward = [3, 4, 5]
7 # seventh element in the list
forward = [8]
backward = [4, 5, 6]
8 # eight element in the list
forward = []
backward = [5, 6, 7]
假设窗口大小为4,现在是我想要的输出:
对于列表中的每个_元素,我希望前面有4个值,后面有4个值,忽略当前值.
我能够用它来获得滑动窗口的值,但这也没有给我正确的所需输出.
import more_itertools
list(more_itertools.windowed([1, 2, 3, 4, 5, 6, 7, 8], n=3))